2013-09-26 59 views
0

我跟着Railscast episode #262祖先教程。但是,當我提交表單,鐵軌服務器日誌說parent_id是空的:Rails and ancestry - 表格提交'parent_id'空

rails server日誌:

Started POST "/posts/1/comments" for 127.0.0.1 at 2013-09-26 16:14:59 +0200 
Processing by CommentsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"9+9U/etsazbrJxwWah/eRD9v3fKBnjpy+y5s+g7N/Bw=", "comment"=>{"parent_id"=>"", "author"=>"some name", "author_email"=>"[email protected]", "author_url"=>"", "content"=>"banane"}, "commit"=>"Post Comment", "post_id"=>"1"} 
    Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", "1"]] 
    (0.1ms) begin transaction 
    SQL (0.4ms) INSERT INTO "comments" ("author", "author_email", "author_url", "content", "created_at", "post_id", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?) [["author", "some name"], ["author_email", "[email protected]"], ["author_url", ""], ["content", "banane"], ["created_at", Thu, 26 Sep 2013 14:14:59 UTC +00:00], ["post_id", 1], ["updated_at", Thu, 26 Sep 2013 14:14:59 UTC +00:00]] 
    (38.6ms) commit transaction 
    (0.1ms) begin transaction 
    (0.1ms) commit transaction 
Redirected to http://0.0.0.0:3000/posts/1 
Completed 302 Found in 49ms (ActiveRecord: 39.4ms) 

comments_controller.rb

def new 
    @comment = Comment.new 
    @comment.parent_id=params[:parent_id] 
end 

def create 
    @post = Post.find(params[:post_id])  
    @comment = @post.comments.create(params[:comment].permit(:author, :author_email, :author_url, :content, :parent_id)) 

    respond_to do |format| 
    if @comment.save 
     stuff 
    else 
     other stuff 
    end 
    end 
end 

def comment_params 
    params.require(:comment).permit(...some stuff..., :parent_id) 
end 

comment.rb

class Comment < ActiveRecord::Base 
    belongs_to :post 
    has_ancestry 
end 

post.rb

class Post < ActiveRecord::Base 
    belongs_to :user 
    has_many :comments, :dependent => :destroy 
    accepts_nested_attributes_for :comments 
end 

views/posts/show.html.erb

<% @post.comments.each do |comment| %> 
    <%= show some stuff %> 
    <%= link_to (post_path(:anchor => "respond", :parent_id => comment)) do%> 
    <%= "Reply"%> 
    <% end %> 
<% end %> 
<%= render 'comments/comment_form'%> 

_comment_form.html.erb

<%= form_for [@post, @post.comments.build], html: { :id => "commentform"} do |f| %> 
<%= f.hidden_field :parent_id %> 
<%= some fields %> 
<%= f.submit "Post Comment"%> 

Rails的調試信息:

--- !ruby/hash:ActionController::Parameters 
parent_id: '17' 
action: show 
controller: posts 
id: '1' 

我想有些事情錯了我的創建方法CommentsController,但我無法弄清楚缺少的東西。所以,我得到了這個工作。我從我的posts/show視圖提交表單,所以我必須在後控制器的show動作中調用@comment = Comment.new(:parent_id => params[:parent_id])

+0

這行看起來有點不可思議 - 這裏的'comment'變量是什麼? '<%= link_to(post_path(:anchor =>「respond」,:parent_id => comment))do%>' – dax

+0

我在views/posts/show.html.erb中添加了一些行。我認爲鏈接工作正常:「0.0.0.0:3000/posts/1?parent_id=18#respond」我看到不同的評論id不同的 – beo

+0

你如何在你的新行動中獲得params [:parent_id]?看來這沒有被正確設置。 – BroiSatse

回答

1

如果您使用Rails> 3.0.0試試這個在您的意見控制器:中

@comment = Comment.new 
@comment.parent_id=params[:parent_id] 

代替

@comment = Comment.new(:parent_id => params[:parent_id]) 
1

添加:parent_idcomment_params在評論控制器。