RoR is famous for being “convention over configuration”, so it’s likely that my question is off — I don’t know the convention I should be following.
I have 2 model objects, ParseException and ParseResolution, and ParseResolution belongs_to ParseException.
I am simply making an admin panel off of the Rails scaffolding, and I want to add a new ParseResolution object, tied to a ParseException object.
After generating controllers for both models, I added a link to parse_resolution/new from parse_exception/index. Since ParseResolution needs to know which ParseException it is resolving (belongs_to), I am also passing it as a parameter:
<td><%= link_to 'Add Resolution', new_parse_resolution_path(:parse_exception => parse_exception) %></td>
So far so good.
In my ParseResolution controller, I have:
def new
@parse_resolution = ParseResolution.new
@parse_resolution.parse_exception = ParseException.find(params[:parse_exception])
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @parse_resolution }
end
end
So far so good, as I can access the ParseException object inside my ERB file and display its details.
However, when I press the “Create” button to add my new ParseResolution object, the save method complains that I’m not providing it with a ParseException, which is a required column.
I tried doing this with a hidden_field:
<%= hidden_field(:parse_resolution, :parse_exception) %>
But that just converts the actual ParseException object into a string:
<input id="parse_resolution_parse_exception" name="parse_resolution[parse_exception]" type="hidden" value="#<ParseException:0x1108a8930>" />
Which clearly won’t work.
OK, so I’m trying again not to hate Ruby on Rails for all of its voodoo-like convention. Would someone please enlighten me on the best way to do what I am trying to do?
If you want follow good practices, you should do something like this:
Your routes to new,create,destroy action should looks like:
in routes.rb it gonna be:
and then your path to new action should be something like that:
new_parse_exception_parse_resolution_path(@parse_exception)
(you also need to change path to create action in form)
Then in your ParseResolutionsController: