I have a very common situation and a solution, but I would like to ask the Rails experts out there if it can be improved.
I have a very typical RESTful controller where the user supplies some of the object attributes upon creation. There is a thing
model, a ThingsController
and various views, including new
, create
, and a _form
partial.
A thing has two attributes,
- a color, which is set when they hit a link to create it (i.e. a “Create Red Thing” link that encodes the ID of red as a URL parameter)
- a description, which is entered by the user in the form view
I’m happy with the approach for dealing with an attribute like the description that that a user specifies in the form, but less confident about the way that I handle an attribute that is passed through via the URL parameters associated with the first click.
This is what I am doing right now (note that I have omitted error checking to simplify matters). First, my new
and create
methods in the controller are as follows:
def new
@thing = Thing.new
@thing.color = Color. find(params[:color])
end
def create
@thing = Thing.new(params[:thing])
@thing.color = Color. find(params[:color])
if @thing.save
flash[:notice] = "Successfully created thing."
redirect_to somewhere_url
else
render :action => 'new'
end
end
The new
view just invokes the _form
partial, which looks as follows:
<% form_for @thing do |f| %>
<%= f.error_messages %>
<%= hidden_field_tag "color", @thing.color.id %>
<%= f.label :description %>
<%= f.text_area :description %>
<%= f.submit "Submit" %>
<% end %>
It seems a little messy to be passing the color ID through to the create
method as a URL parameter by putting a hidden field in the form. Does this look reasonable, or is there another approach that would be better?