【问题标题】:Allow users to comment on posts, can't mass-assign post允许用户评论帖子,不能批量分配帖子
【发布时间】:2013-01-15 12:48:26
【问题描述】:

我是编程新手,正在为我在应用中实现的一项新功能而苦苦挣扎。我希望用户能够评论他人的微博。

我收到错误消息:无法批量分配微博

用户模型:

attr_accessible :name, :email, :password, :password_confirmation #is this secure with password there?
attr_protected :admin   #attr_protected necessary?
has_many :microposts, dependent: :destroy
has_many :comments, :through => :microposts, dependent: :destroy

微博模型:

attr_accessible :comment #basically the content of the post
attr_protected :user_id
has_many :comments, dependent: :destroy

评论模型:

attr_accessible :content, :micropost
belongs_to :user
belongs_to :micropost
validates :user_id, presence: true
validates :micropost_id, presence: true
validates :content, presence: true
default_scope order: 'comments.created_at ASC'   #is this necessary?

评论控制器:

def create
  @micropost = Micropost.find_by_id(params[:id])   #is this necessary?
  @comment = current_user.comments.create(:micropost => @micropost)
  redirect_to :back
end

用户控制器:

def show
  @user = User.find_by_id(params[:id])
  @microposts = @user.microposts.paginate(page: params[:page])
  @micropost  = current_user.microposts.build
  @comments = @micropost.comments
  @comment = current_user.comments.create(:micropost => @micropost) #build, new or create??
end

view/cmets/_form:(这个partial在每条微博结束时调用)

<span class="content"><%= @comment.content %></span>
<span class="timestamp">Said <%= time_ago_in_words(@comment.created_at) %> ago.</span
<%= form_for(@comment) do |f| %>
  <%= f.text_field :content, placeholder: "Say Something..." if signed_in? %>
<% end %>

路线:

resources :users 
resources :microposts, only: [:create, :destroy] 
resources :comments, only: [:create, :destroy]

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    你应该把你的属性微博放在 attr_accessible 上

    attr_accessible :content, :micropost
    

    默认情况下,所有属性都是不可访问的。您必须在 attr_accessible 上定义您的可访问属性。

    更多信息here

    【讨论】:

    • 所以这意味着我应该删除所有 attr_protected 的?微博也不属于微博架构的一部分。我添加了您的建议,但遇到了同样的问题。
    • 我相信你必须在 Micropost 模型上有 "attr_accessible :comment_id"。
    • 是的,它可以工作,但我现在从同一个文件中使用的时间戳中得到了一些奇怪的错误(我已经更新了代码),所以我将把它作为不同的问题重新发布。谢谢你的帮助。如果您有兴趣,我会将链接放在评论中
    【解决方案2】:

    JMarques 是对的,但我喜欢在 attr_accessible 上使用 _id 以保持一致。考虑到这一点,您可以使用

     # comment.rb
     attr_accessible :content, :micropost_id
    
     # controller
     current_user.comments.create(:micropost_id => @micropost.try(:id))
    

    刚刚添加了一个try,以防find_by_id返回nil

    【讨论】:

      【解决方案3】:

      根据rails4可以使用强参数:

          def create
            @micropost = Micropost.find_by_id(micropost_params)
           ................
      
          end
      
         private
          def micropost_params
          params.require(:micropost).permit(:id)
          end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-07
        • 2016-05-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多