2014-10-08 56 views
-3

我想弄清楚爲什麼我的表單提交錯誤。我試圖在帖子和表單渲染下實現嵌套註釋,但是提交給錯誤的操作。我已經嘗試了一些東西,但無法讓它工作。我可以通過rails控制檯創建評論,但不能通過我的表單。我正在瀏覽一個使用舊版rails的railscast。我想主要的區別是強大的參數,但它似乎並沒有工作,我不知道我在這裏失蹤。謝謝。form_for submitting作爲PostsController#顯示而不是創建時創建註釋(嵌套)

這是我的日誌

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"=>"[email protected]", "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) 

這是我的形式(部分_form)

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

Posts控制器

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 

回答

0

試着這麼做

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

感謝您的答覆。但是我仍然得到相同的行爲。 – 2014-10-09 00:53:04