I got a routing error when trying to create a note object from inside a
property object:
Routing Error
property_url failed to generate from {:action=>"show", :controller=>"properties", :id=>nil}, expected: {:controller=>"properties", :action=>"show"}, diff: {:id=>nil}
Here's what's going on:
# Show action from the properties controller
def show
@property = Property.find(params[:id])
@note = @property.notes.new
respond_to do |format|
format.html
format.xml { render :xml => @property }
end
end
# Create action from the notes controller
def create
@note = Note.new(params[:note])
@note.user_id = current_user.id
respond_to do |format|
if @note.save
flash[:notice] = 'Note was successfully created.'
format.html { redirect_to property_path @note.property_id }
format.xml { render :xml => @note, :status => :created, :location => @note }
else
format.html { render :action => "new" }
format.xml { render :xml => @note.errors, :status => :unprocessable_entity }
end
end
end
# show.html.erb for properties
<% form_for @note do |f| %>
<%= f.error_messages %>
<p>
<%= f.label 'New note:' %><br />
<%= f.text_area :text %>
</p>
<p>
<%= f.submit 'Save note' %>
</p>
<% end %>
The form displays correctly, but gives a routing error when saving. I'm
not sure what the error is trying to tell me.
This works fine from the console, insofar as I can call
"@note=@property.note.new; @note.save" and have it save successfully.
How can I do it from a view?
Ultimately, the goal is to populate notes linked to the property_id
without resorting to either hidden fields (insecure) or session
variables. I thought a form was the way to go, but I'm really stuck.
The form displays correctly, but gives a routing error when saving. I'm
not sure what the error is trying to tell me.
The error is telling you that you called property_path(nil) - for
whatever reason @note.property_id isn't set.
Fred
Thanks. I had suspected as much, but still found the error rather
opaque. Still, I don't understand *why* it's being unset, since I'm
setting it explicitly in the property controller.
Rather than guessing at why, though, what I'd really like is a reliable
mechanism to populate @note.property_id from the properties
controller--or perhaps from the notes controller if I can somehow get at
the referrer.
Is there a way to call Note.new with arbitrary values?
> The error is telling you that you called property_path(nil) - for
> whatever reason @note.property_id isn't set.
Thanks. I had suspected as much, but still found the error rather
opaque. Still, I don't understand *why* it's being unset, since I'm
setting it explicitly in the property controller.
You're not really - you're setting it in the new method, but not in
the create one (or are you using the nested attributes stuff) ?
Fred
If I call
@note = @property.notes.new
from the console, it properly populates the property_id and passes it
onto the note object. I've been trying this different ways, and maybe I
don't really understand what's being called when I do it via the web.
At present, I'm calling this from the properties controller, rather than
the notes controller, in the show action (which is where I have embedded
the New Note form). I'm not sure how to do it in notes.new or
notes.create, since I don't understand how to propogate the parent ID to
the notes controller from the property controller that way.
Can you offer a practical snippet for me to try?