0

私はモデルを持っています

class Rcomment < ActiveRecord::Base
  attr_accessible :comment, :rating

  belongs_to :recipe
  belongs_to :user
end

レシピの表示ビューからコメントを追加しようとしています。rcomment テーブルにダミー データを入力しましたが、次の方法で問題なく表示されます。

@recipe = Recipe.find(params[:id])
@comments = @recipe.rcomments

だから私は @newcomments = @recipe.rcomments.build(params[:recipe]) を試しました

しかし、エラーでまったく機能しません: #<#:0x25ccd10> のメソッド `rcomments_path' が未定義です。

使用可能な form_for を表示するにはどうすればよいですか?

%= form_for(@newcomments) do |f| %>

      <%= f.label :Comments %>
      <%= f.text_field :comment%>

      <%= f.label :ratings %>
      <%= f.text_field :rating %>

      <%= f.submit "Submit Comment", class: "btn btn-large btn-primary" %>
    <% end %>
4

1 に答える 1

0

Rcomment作成用のフォームを作成しましたが、routes.rbにrcommentsエントリがありません。ただし、問題に最も適しているのは、レシピを使用してコメントを作成することだと思います。accepts_nested_attributes_forとでこれを行うことができますfields_for

http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

だから、このようなもの-

レシピモデルに、次を追加します。

attr_accessible :rcomments_attributes
accepts_nested_attributes_for :rcomments

そして、あなたのレシピ#ショービューで:

<%= form_for(@recipe) do |f| %>
  <% f.fields_for :rcomments do |cf| %>
    <%= cf.label "Comments" %>
    <%= cf.text_field :comment%>

    <%= cf.label "Ratings" %>
    <%= cf.text_field :rating %>
  <% end %>

  <%= f.submit "Submit Comment", class: "btn btn-large btn-primary" %>
<% end %>
于 2013-01-14T03:36:27.063 に答える