Creating view for adding nested resource in Ruby on Rails app
I'm making a RoR app that has 3 resources, that are nested in this order: Projects -> Feeds -> XML_Fields. Projects has_many Feeds, Feeds has_many XML_Fields. My problem is that I am getting stuck on adding to the Show Feeds view the ability to add a new XML_Field. My code is below:
<h2>Add an XML field:</h2>
<%= form_for ([@feed, @feed.xml_fields.build]) do |f| %>
<div class="field">
<%= f.text_area :tag %>
</div>
<div class="actions">
<%= f.submit %>
</div>开发者_如何转开发
<% end %>
Executing this returns a NoMethodError: undefined method 'tag'
. Tag is the name of the column in the xml_fields table I created. My xml_fields_controller has this create method:
def create
@xml_field = Feed.find(params[:feed_id]).xml_fields.build(params[:xml_field])
respond_to do |format|
if @xml_field.save
format.html { redirect_to( :back, :notice => 'XML Field was successfully created.') }
format.xml { render :xml => @xml_field, :status => :created, :location => [@xml_field.feed, @xml_field] }
else
format.html { render :action => "new" }
format.xml { render :xml => @xml_field.errors, :status => :unprocessable_entity }
end
end
end
And my routing file looks like this:
resources :projects do
resources :feeds
end
resources :feeds do
resources :xml_fields
end
Any suggestions to make this NoMethodError go away? Thanks.
model XmlField doesn't have tag field in database ;)
精彩评论