问题的形式

发布于 2024-10-07 20:05:19 字数 805 浏览 0 评论 0原文

我想在“帖子视图”中制作“评论”表单,

但是这个助手无法工作

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

rvm 1.9.2 Rails 3.0.3

编辑 1: 错误:

undefined method `model_name' for NilClass:Class

编辑 2 提取的源代码(第 #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 %>

应用程序跟踪

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'

信息列表|有很多项目。 项目|属于列表

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

肥爪爪 2024-10-14 20:05:20

好吧,我做了什么。

路由

  resources :lists do
    resources :items
  end

列表控制器

  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 %>

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 %>
森末i 2024-10-14 20:05:19

看看这是否有效

<% 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 %>

您可以检查 http://guides.rubyonrails.org/form_helpers.html (请参阅第 7.3 节使用表单助手)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文