form_for提交为PostsController #show而不是创建注释时创建(嵌套)

时间:2014-10-08 19:54:32

标签: ruby-on-rails forms ruby-on-rails-4 controller partials

我试图找出我的表单提交错误的原因。我试图在帖子和表单呈现下实现嵌套注释,但提交错误操作。我尝试过一些东西,但无法让它发挥作用。我可以通过rails控制台创建注释,但不能通过我的表单创建注释。我正在通过一个railscast,他使用旧版本的rails。我认为主要区别在于强大的参数,但它似乎并没有起作用,我无法弄清楚我在这里缺少什么。感谢。

这是我的日志

Started GET "/posts/1?utf8=%E2%9C%93&authenticity_token=PJmmRV6hnY%2Bgm4cVe5LSdALHezbI3ehMkud0yYTaA%2FQ%3D&comment%5Bname%5D=Mark&comment%5Bemail%5D=hustada80%40gmail.com&comment%5Bcontent%5D=this+is+a+comment&commit=Create+Comment" for 127.0.0.1 at 2014-10-08 14:34:53 -0500
Processing by PostsController#show as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"PJmmRV6hnY+gm4cVe5LSdALHezbI3ehMkud0yYTaA/Q=", "comment"=>{"name"=>"Mark", "email"=>"hustada80@gmail.com", "content"=>"this is a comment"}, "commit"=>"Create Comment", "id"=>"1"}
  Post Load (0.2ms)  SELECT  "posts".* FROM "posts"  WHERE "posts"."id" = ? LIMIT 1  [["id", 1]]
  Rendered comments/_form.html.erb (4.2ms)
  Rendered comments/_comments.html.erb (0.1ms)
  Rendered posts/show.html.erb within layouts/application (53.1ms)
  Rendered layouts/_header.html.erb (0.5ms)
  Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 524ms (Views: 519.7ms | ActiveRecord: 0.2ms)

这是我的表格(部分_表格)

<div class="well">

    <h4>Leave a comment</h4>

  <form role="form" class="clearfix">

<%= form_for([@commentable, @comment]) do |f| %>
 <div class="form-group">
    <%= f.label :name %>
    <%= f.text_field :name, class: 'form-control' %>
  </div>

  <div class="form-group">
    <%= f.label :email %>
    <%= f.text_field :email, class: 'form-control', required: true %>
  </div>

  <div class="form-group">
    <%= f.label :content %>
    <%= f.text_area :content, class: 'form-control', required: true %>
  </div>

  <%= f.submit class: 'btn btn-primary' %>
<% end %>
</div>

帖子控制器

class PostsController < ApplicationController

  def new
    @post = Post.new
  end

  def show
    @post = Post.find(params[:id])
    @commentable = @post
    @comments = @commentable.comments
    @comment = Comment.new
  end

  def index
    @post = Post.all
    @posts = Post.order('created_at DESC')
    ##@posts_by_month = Post.find(:all, :order => 'created_at DESC').group_by { |post| post.created_at.strftime("%B %Y") }
  end


  def month_count
    @posts_by_month = Post.find(:all, :order => 'created_at DESC').group_by { |post| post.created_at.strftime("%B %Y") }
  end

  def create
    @post = Post.new(post_params)
    @post.save
    redirect_to @post
  end

  private
  def post_params
    params.require(:post).permit(:title, :text)
  end

评论控制器

class CommentsController < ApplicationController
  before_action :load_commentable


  def index
    @comments = @commentable.comments
  end

  def new
    @comment = @commentable.comments.new
  end

  def create
    @comment = @commentable.comments.new(comments_params)
      if @comment.save
        redirect_to @commentable, notice: "Comment created."
      else
        render :new
      end
  end

  def destroy
  end




  private

  # def load_commentable
  #   resource, id = request.path.split('/')[1, 2]
  #   @commentable = resource.singularize.classify.constantize.find(id)
  # end

  def load_commentable
    klass = [Post].detect { |c| params["#{c.name.underscore}_id"]}
    @commentable = klass.find(params["#{klass.name.underscore}_id"])
  end


  def comments_params
    params.require(:comment).permit(:content, :email, :name)
  end

路由

Rails.application.routes.draw do
root 'posts#index'
get "sign_up" => "users#new", :as => "sign_up"
get "/log-in" => "sessions#new"
post "/log-in" => "sessions#create"
get "/log-out" => "sessions#destroy", as: :log_out


resources :posts do
  resources :comments
end
resources :users
resources :sessions

1 个答案:

答案 0 :(得分:0)

尝试类似

的内容
<%= form_for([@commentable, @comment], url: post_comments_path, method: :post) do |f| %>