关于嵌套我的社区和评论感到困惑

史蒂文·奥索里奥

我可以选择将评论模型用于用户在个人资料页面和社区页面中发布。目前,我正在社区中工作,由于困惑,我想寻求一些指导。

我得到的当前错误是我的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

观看次数

/社区/显示

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


<%= render "comments/new" %>   

/ comments / _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'
理查德·派克

好的。

我的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以更好地了解其工作原理。


多态的

您还有另一个可以通过多态关联解决的问题

在此处输入图片说明

简而言之,这使您可以将模型与任何其他模型关联。

在您的实例中,您可以在users上进行注释communities,该功能可以很好地发挥作用:

#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表中,但是之后,上面的代码应该可以工作了。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

关于嵌套我的社区和评论感到困惑

来自分类Dev

AjaxEditableLabel和Validator的行为使我感到困惑

来自分类Dev

AjaxEditableLabel和Validator的行为使我感到困惑

来自分类Dev

关于Textview和Listview显示列表感到困惑

来自分类Dev

输出| ...使我感到困惑

来自分类Dev

叉子让我感到困惑

来自分类Dev

QT让我感到困惑

来自分类Dev

Kotlin和泛型构造函数让我感到困惑

来自分类Dev

我对'n'和print('n')之间的区别感到困惑

来自分类Dev

我对Big O表示法和规则感到困惑

来自分类Dev

我对Java中的类和对象的概念感到困惑

来自分类Dev

我对比较和跳装配感到困惑

来自分类Dev

我对行高和边距感到困惑

来自分类Dev

我对Big O表示法和规则感到困惑

来自分类Dev

提交事务时,我在hibernate和mysql中感到困惑

来自分类Dev

关于Linux上的SSH密钥感到困惑

来自分类Dev

关于从片段启动YoutubePlayerView感到困惑

来自分类Dev

关于为什么Git认为我的分支技巧落后于原点感到困惑

来自分类Dev

关于抽象类的两个概念让我感到困惑

来自分类Dev

递归调用使我感到困惑

来自分类Dev

我对SSD优化感到困惑

来自分类Dev

我对super()用法感到困惑

来自分类Dev

重击逃逸使我感到困惑

来自分类Dev

AngularJS $ scope使我感到困惑

来自分类Dev

递归调用使我感到困惑

来自分类Dev

对我的 if 语句的逻辑感到困惑

来自分类Dev

我对位掩码感到困惑

来自分类Dev

我对 WordPress 结构感到困惑

来自分类Dev

对教授关于类和类参数的说明感到非常困惑 - C++ 课程介绍