开发者

form_for problem

i want to make a 'comment' form inside 'post view'

But this helper couldn't work

 <%= form_for([@post, @comment]) do |f| %>
   ...
 <% end %>

rvm 1.9.2 rails 3.0.开发者_开发百科3

Edit 1: the error:

undefined method `model_name' for NilClass:Class

Edit 2 Extracted source (around line #23):

20: <% end %>
21: </ul>
22: 
23: <%= form_for [@list,@item] do |form| %>
24:     
25: <%= form.text_field :due %>
26: <p><%= form.text_field :title %>

Application trace

app/views/lists/show.html.erb:23:in `_app_views_lists_show_html_erb___3300490552675426158_2162821280_4216612080991561324'
app/controllers/lists_controller.rb:22:in `show'

info list| has_many items. item| belongs_to list


See if this works

<% form_for @post, :url => { :action => "create" } do |post_form| %>
  ...
  <% post_form.fields_for :comments do |comment_fields| %>
     Comment ID: <%= comment_fields.text_field :id %>
   <% end %>
<% end %>

You can check http://guides.rubyonrails.org/form_helpers.html (refer section 7.3 Using Form Helpers)


ok here what i did.

routes

  resources :lists do
    resources :items
  end

list controller

  def show
    @list = List.find(params[:id])
    @item = @list.items.new


    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @list }
    end
  end

show.html.erb

<%= form_for([@list, @item]) do |form| %>
 <p><%= form.text_field :title %>
 <%= form.submit %></p>
<% end %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜