Rails 3 form_for 嵌套路由

发布于 2024-11-12 05:14:06 字数 1906 浏览 3 评论 0原文

我正在尝试按照 Ruby Guide(http://guides.rubyonrails.org/getting_started.html#adding-a-route-for-comments) 中的博客和评论的示例,使用嵌套路由执行 form_for 。

我正在开发一个应用程序来创建包含多种问题的调查,这些问题位于一组中,每个问题都有一个或多个答案选项。

这是 reoutes.rb

  resources :groups do
    resources :questions do
      resource :answers
    end
  end

控制器运行良好,当我显示创建的组时,可以看到这些问题并使用嵌套的 form_for 创建问题:

groups/show.html.erb

<h2>Group: <%= @group.desc %> </h2>
<h3>Questions</h3>
<% @group.questions.each do |q| %>
   <%= q.desc%> <%= link_to 'Destroy question', [@group, q], :confirm => 'Are you sure?', :method => :delete %> <br/>
<%end%>

<h4>New question</h4>
<%= form_for([@group, @group.questions.build]) do |f| %>
  <div class="field">
    <%= f.label 'Label: '%>
    <%= f.text_field :desc, :size => 100%>
    <%= f.submit 'Create question'  %>
  </div>
<% end %>
<br />

现在我需要展示答案以及某种方式插入该问题的答案。要显示答案,可以使用 @group.questions.each 块中的 q.answers.each 来很好地显示答案。但我要做的是制作一个 form_for 作为答案,我尝试了下面的代码,但不起作用:

groups/show.html.erb

...
<% @group.questions.each do |q| %>
   <%= q.desc%> <%= link_to 'Destroy question', [@group, q], :confirm => 'Are you sure?', :method => :delete %> <br/>
  <!-- New answer -->
  <%= form_for([q, q.answers.build]) do |f| %>
  <div class="field">
    <%= f.label 'Label: '%>
    <%= f.text_field :desc, :size => 100%>
    <%= f.submit 'Create answer'  %>
  </div>
<% end %> 

<%end%>
<h4> New question<h4>
...

Rails 给出了一个错误:

未定义的方法 `question_answers_path'

当我尝试使用 form_for([q, q.answers.build]) 时,

。有什么帮助吗?

I`m trying to do a form_for with nested routes following the example of Blog and Comments from Ruby Guide(http://guides.rubyonrails.org/getting_started.html#adding-a-route-for-comments).

I`m doing an application to create surveys with a lot of kind of questions, the questions are in a group and each question has one o more options of answers.

This is the reoutes.rb

  resources :groups do
    resources :questions do
      resource :answers
    end
  end

The controllers are working well, and when I show a group created there is possible to see thes questions and create questions with the nested form_for bellow:

groups/show.html.erb

<h2>Group: <%= @group.desc %> </h2>
<h3>Questions</h3>
<% @group.questions.each do |q| %>
   <%= q.desc%> <%= link_to 'Destroy question', [@group, q], :confirm => 'Are you sure?', :method => :delete %> <br/>
<%end%>

<h4>New question</h4>
<%= form_for([@group, @group.questions.build]) do |f| %>
  <div class="field">
    <%= f.label 'Label: '%>
    <%= f.text_field :desc, :size => 100%>
    <%= f.submit 'Create question'  %>
  </div>
<% end %>
<br />

Now I need to show the answers and some way to insert the anwsers to that question. To show the answers works good with q.answers.each inside @group.questions.each block. But what I have to do is to make a form_for for answer, I tried the code bellow but does`nt works:

groups/show.html.erb

...
<% @group.questions.each do |q| %>
   <%= q.desc%> <%= link_to 'Destroy question', [@group, q], :confirm => 'Are you sure?', :method => :delete %> <br/>
  <!-- New answer -->
  <%= form_for([q, q.answers.build]) do |f| %>
  <div class="field">
    <%= f.label 'Label: '%>
    <%= f.text_field :desc, :size => 100%>
    <%= f.submit 'Create answer'  %>
  </div>
<% end %> 

<%end%>
<h4> New question<h4>
...

The Rails gives a error:

undefined method
`question_answers_path'

when I try to use form_for([q, q.answers.build]) .

Any help?

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

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

发布评论

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

评论(1

高跟鞋的旋律 2024-11-19 05:14:06

按照您在表单中指定对象的顺序,您可以将资源嵌套在组下,然后是问题,最后是答案。您需要使用类似于 form_for [g,q,q.answers.build] 的内容。如果这不起作用,请编辑您的帖子以包含 rake paths 的内容,我们可以从那里开始。

The order you specify the objects in the form for matters, you have the resources nested under groups then questions then finally answers. You need to use something like form_for [g,q,q.answers.build]. If that dosnt work edit your post to include the contents of rake routes and we can go from there.

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