How can we make the urls from a resource based routing nicer?
E.g. I might have a TourController that represents tours in different cities. From the default REST I get the following url:
.../tours/1
of course I would much more like something like
.../tours/boston/some-unique-name
Can we get this somehow from the defaults or do we have to define the routes on ourselves for this?
To do that, you would need to use some system for using permalinks for your resources, such as the friendly_id gem: http://agilewebdevelopment.com/plugins/friendly_id -- I am not actually so sure how well friendly_id plays with RESTful resources, but it should work.
I have an id in a hidden field of my form. The id is validated in the model against uniqueness in a certain scope. If the validation fails I provide a custom error message. BUT: The field is hidden, so the error message never appears (you just see that there was an error). What I would need is some way to pass the error message to base (errors.add_to_base) but of course I don't want to hand-write the uniqueness validation in a custom validation (that would somehow break the DRY principle...). How would I do that?
Um, does it make sense to return validation-style message on a hidden field? I don't think so. I think for that you should transfer the message to the Flash, which is kind of gross, but the basis of what you're doing seems odd to me.
Can I somehow "automatically" set values in a join table?
E.g. I have a children table and a playdate table and they are connected through children_playdates (has_many playdates :through children_playdates).
Now what I want to do is create a playdate for a child like this:
child.playdates.build(params[:playdate])
So far so good. Now assume, I have a mandatory attribute on children_playdates (say wish_from_child). Is there some way to structure params, so I can still say
child.playdate.build(params[:playdate])
and the value for wish_from_child would automatically be set in the join table (without me explicitely building it)?
I thought about that too. The problem is that I save the Playdate to the DB. If the ChildrenPlaydate fails (e.g. because wish_from_child is nil) I don't want to save any of the two (Playdate and ChildrenPlaydate). I could go around this by packaging everything into a transaction block I guess. Is that the way to go?
How can we make the urls from a resource based routing nicer?
E.g. I might have a TourController that represents tours in different cities. From the default REST I get the following url:
.../tours/1
of course I would much more like something like
.../tours/boston/some-unique-name
Can we get this somehow from the defaults or do we have to define the routes on ourselves for this?
To do that, you would need to use some system for using permalinks for your resources, such as the friendly_id gem: http://agilewebdevelopment.com/plugins/friendly_id -- I am not actually so sure how well friendly_id plays with RESTful resources, but it should work.
There are a number of gems/plugins for this kind of thing: http://www.ruby-toolbox.com/categories/rails_permalinks___slugs.html
This article is a bit old, but it still works well (by overriding to_param), and I prefer it to permalinks:
http://www.jroller.com/obie/entry/seo_optimization_of_urls_in
It is the same format that 37signals uses, for example.
A tricky question about forms:
I have an id in a hidden field of my form. The id is validated in the model against uniqueness in a certain scope. If the validation fails I provide a custom error message. BUT: The field is hidden, so the error message never appears (you just see that there was an error). What I would need is some way to pass the error message to base (errors.add_to_base) but of course I don't want to hand-write the uniqueness validation in a custom validation (that would somehow break the DRY principle...). How would I do that?
Um, does it make sense to return validation-style message on a hidden field? I don't think so. I think for that you should transfer the message to the Flash, which is kind of gross, but the basis of what you're doing seems odd to me.
Can I somehow "automatically" set values in a join table?
E.g. I have a children table and a playdate table and they are connected through children_playdates (has_many playdates :through children_playdates).
Now what I want to do is create a playdate for a child like this:
child.playdates.build(params[:playdate])
So far so good. Now assume, I have a mandatory attribute on children_playdates (say wish_from_child). Is there some way to structure params, so I can still say
child.playdate.build(params[:playdate])
and the value for wish_from_child would automatically be set in the join table (without me explicitely building it)?
@Gabriel Hase
I'm not sure about that.
Your sequence is something like this:
pd = Playdate.create!( . . .)
ChildrenPlaydate.create!( :child_id => child.id, :playdate_id = pd.id, :wish_from_child => 'something')
That is, since you want to mess with the join table Model, you might be best off managing it directly.
I thought about that too. The problem is that I save the Playdate to the DB. If the ChildrenPlaydate fails (e.g. because wish_from_child is nil) I don't want to save any of the two (Playdate and ChildrenPlaydate). I could go around this by packaging everything into a transaction block I guess. Is that the way to go?
@Gabriel Hase
Yes, put it in a transaction.
You can use any model class. E.g.,
User.transaction do
-- update a variety of models
end