【问题标题】:Confused in regards to nesting my community and comment对嵌套我的社区和评论感到困惑
【发布时间】:2016-01-23 14:15:10
【问题描述】:

我将有一个选项,评论模型可用于用户在个人资料页面和社区页面中发帖。目前我正在社区工作,并且在我感到困惑时想要一些方向。

我得到的当前错误是我的 CommentsController#Create 的 ActiveModel::ForbiddenAttributesError。如果可能的话希望得到帮助或帮助我指出纠正错误的方向。

关于人们看到评论的观点的问题是 /communities/show

模型

用户

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

观看次数

/communities/show

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


<%= render "comments/new" %>   

/cmets/_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 %>

感谢所有帮助解释我在哪里犯错误的人,如果我可能需要询问您可能对我提出的要求,请提前表示歉意。如有其他问题,请询问。

更新

我在控制台中看到的是

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'

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 model-view-controller controller nested


    【解决方案1】:

    好的。

    我的 CommentsController#Create 的 ActiveModel::ForbiddenAttributesError

    这基本上意味着您不允许在 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 解决:

    简单地说,这允许您将模型与任意数量的其他模型相关联。

    在您的实例中,您可以对 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_idcommentable_type 列迁移到您的 comments 表中,但之后上面的代码应该可以工作。

    【讨论】:

      猜你喜欢
      • 2017-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-08
      • 1970-01-01
      • 1970-01-01
      • 2016-11-18
      • 1970-01-01
      相关资源
      最近更新 更多