1

我將有一個選項是評論模型可用於用戶發佈在個人檔案頁面和社區頁面。目前我正在社區工作,並且因爲我感到困惑而想要一些方向。困惑在於嵌套我的社區和評論

我得到的當前錯誤是我的CommentsController#Create的ActiveModel :: ForbiddenAttributesError。如果可能的話,請幫助我,或者幫助我指出解決我的錯誤的方向。

問題的問候查看哪些人看到的評論是/社區/顯示

模式

用戶

has_one :profile 
has_many :communities 
has_many :comments, dependent: :destroy 

社區

extend FriendlyId 
friendly_id :title, use: [:slugged, :finders] 

has_many :comments, dependent: :destroy 
belongs_to :user 

評論

belongs_to :user 
belongs_to :community 

路線

resources :communities do 
resources :comments 
end 

控制器

社區

def show 
@community = Community.friendly.find(params[:id]) 
@current_user = User.find(session[:user_id]) 
@comment = Comment.new 
end 

評論

before_filter :load_community 
def create 
    @comment = @community.comments.build(params[:comment]) 
    @comment.user_id = current_user.id 
    if @comment.save 
    redirect_to :back 
    else 
    redirect_to "/" 
    end 

    # @comment = Comment.new(comment_params) 
    # @comment.user_id = session[:user_id] 

    # if @comment.save && @comment.community_id 
    # flash[:notice] = "Comment has been posted" 
    # else 
    # flash[:alert] = @comment.errors.full_messages 
    # end 
end 

private 
def load_community 
@community = Community.friendly.find(params[:community_id]) 
end 

def comment_params 
params.require(:comment).permit(:text, :user_id, :community_id, :profile_id) 
end 

查看

/社區/顯示

<%= render "profiles/index" %> 
<h4><%= @community.title.capitalize! %></h4> 
<%= @community.bio %> 


<%= render "comments/new" %> 

/評論/ _new

<%= form_for ([@community, @comment]) do |f| %> 
<%= f.text_area :text, placeholder: "Enter New Comment Here ...", :cols => 50, :rows => 3, :class => 'text_field_message', :id => 'new_comment' %> 
<%= f.submit :class => 'new_comment_button' %> 
<% end %> 

謝謝大家誰誰幫助解釋我犯了我的錯誤,也提前抱歉,如果我可能需要問你可能要求我什麼。有關更多問題,請詢問。

UPDATE

我在我的控制檯中看到的是

Started POST "/communities/dang/comments" for 127.0.0.1 at 2015-10-23 18:38:47 -0400 
Processing by CommentsController#create as HTML 
Parameters: {"utf8"=>"✓", "authenticity_token"=>"us8KNTLUZUdao13GK4OQId0YoUqf+CeLFIGjydnyWtI=", "comment"=> {"text"=>"www"}, "commit"=>"Create Comment", "community_id"=>"dang"} 
Community Load (0.1ms) SELECT "communities".* FROM "communities" WHERE "communities"."slug" = 'dang' ORDER BY "communities"."id" ASC LIMIT 1 
Completed 500 Internal Server Error in 11ms 

ActiveModel::ForbiddenAttributesError (ActiveModel::ForbiddenAttributesError): 
app/controllers/comments_controller.rb:17:in `create' 

回答

2

好。

::加載ActiveModel爲ForbiddenAttributesError我CommentsController#創建

這基本上意味着你不允許你create方法所需要的屬性。

這是你所需要的:

#app/controllers/comments_controller.rb 
class CommentsController < ApplicationController 
    def create 
     @comment = @community.comments.new comment_params 
     @comment.save 
    end 

    private 

    def comment_params 
     params.require(:comment).permit(:text).merge(user_id: current_user.id) 
    end 
end 

您應該strong params讀了更好的理解它是如何工作。


多態性

也有其可以用polymorphic association來解決另一個問題:

enter image description here

簡單地說,這可以讓你的模型與任意數量的其他關聯。

在您的實例,在那裏你可以userscommunities評論,這個功能將更好地服務:

#app/models/comment.rb 
class Comment < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :commentable, polymorphic: true 
end 

#app/models/user.rb 
class User < ActiveRecord::Base 
    has_many :sent_comments, class_name: "Comment", foreign_key: :user_id 
    has_many :comments, as: :commentable 
end 

#app/models/community.rb 
class Community < ActiveRecord::Base 
    has_many :comments, as: :commentable 
end 

這將允許您執行以下操作:

@user = User.find params[:id] 
@user.comments.new comment_params 

def comment_params 
    params.require(:comment).permit(:text).merge(user_id: current_user.id) #-> current_user from Devise 
end 

這可以讓你使用單個關聯使用@user.comments@community.comments

你必須在commentable_id & commentable_type列遷移到您的comments表,但之後,上面的代碼應該工作。