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.