2

Ruby on Rails は初めてで、現在、単純なブログ プラットフォームを作成しようとしています。act_as_commentable_with_threading gemでコメント機能を実装したいと考えています。私はフォームのネストに慣れていませんが、ここに私の試みがあります:

コメント_コントローラー.rb

class CommentsController < ApplicationController
  before_filter :get_spot

  # GET /comments/new
  def new
    @comment = Comment.new

    respond_to do |format|
      format.html
    end
  end

  private
    def get_spot
      @spot = Spot.find(params[:spot_id])
    end    
end

_form.html.erb

<%= form_for(@comment) do |f| %>
  ... form elements ...
<% end %>

しかし、それは私にundefined method 'comments_path'エラーを与えます。私はそれを非常に間違った方法で行っていると確信してい2.xます3.x。なぜなら、私はいくつかの良い例、より良い宝石を私に指摘するか、単にそれがどのように行われるべきかを書いてくれるなら、本当に感謝しています. :) 前もって感謝します!

4

1 に答える 1

1

Make sure that you have set up the routes for comments controller.

# routes.rb
...
resources :comments
...

Update:

The routes are nested under posts, based on the comment below. In this case, you will get a route like "#{RAILS_ROOT}/posts/1/comments" to which you can (http) POST a comment. However, your form is trying to POST to "#{RAILS_ROOT}/comments", hence the error. In this case, you need to add a form builder in posts/show.html.erb:

<p>
  <b>Name:</b>
  <%= @post.name %>
</p>

<p>
  <b>Title:</b>
  <%= @post.title %>
</p>

<p>
  <b>Content:</b>
  <%= @post.content %>
</p>

<h2>Add a comment:</h2>
<%= form_for([@post, @post.comments.build]) do |f| %>
  <div class="field">
    <%= f.label :commenter %><br />
    <%= f.text_field :commenter %>
  </div>
  <div class="field">
    <%= f.label :body %><br />
    <%= f.text_area :body %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

See Rails getting started guide from where this code is extracted.

于 2012-04-27T06:51:27.217 に答える