无法将参数传递到表中

时间:2014-12-02 08:39:35

标签: ruby-on-rails ruby ruby-on-rails-4

我可以创建一个新帖子,可以传递将新帖子与current_user和feeling_ids相关联的帖子参数。

但是,同时,在创建新帖子时,我还想在user_feeling表中将feeling_ids和current_user关联在一起。感受和用户有一个has_many:通过关联。

到目前为止,我能够构建新行,从而输入user_id,但无法将feel_ids数组传递/输入到user_feeling表中。

有关我如何做到这一点的任何想法?提前感谢您抽出时间来看看这个。

    From: /Users/shengyeong/Desktop/emotion/app/controllers/posts_controller.rb @ line 19 PostsController#create:

        18: def create
     => 19:   binding.pry
        20:   @post = Post.new(post_params)
        21:   @post.creator = current_user
        22:   @user = current_user.user_feelings.build(params.require(:post).permit(feeling_id: params[:feeling_ids]))
        23:   if @post.save && @user.save
        24:     @post.create_activity :create, owner: current_user #from Public Activity gem. Using Common in Post Model.
        25:     flash[:notice] = "Your post was created."
        26:     redirect_to posts_path
        27:   else
        28:     render :new
        29:   end
        30: end 

    [1] pry(#<PostsController>)> @user = current_user.user_feelings.build(params.require(:post).permit(feeling_id: params[:feeling_ids]))
    Unpermitted parameters: feeling_ids, description, url
    => #<UserFeeling id: nil, user_id: 5, feeling_id: nil, created_at: nil, updated_at: nil>
    [2] pry(#<PostsController>)> @user = current_user.user_feelings.build(feeling_id: params[:feeling_ids])
    => #<UserFeeling id: nil, user_id: 5, feeling_id: nil, created_at: nil, updated_at: nil>
    [3] pry(#<PostsController>)> @user = current_user.user_feelings.build(params.require(:post).permit(feeling_ids: [])
    [3] pry(#<PostsController>)* )  
    Unpermitted parameters: description, url
    ActiveRecord::UnknownAttributeError: unknown attribute: feeling_ids
    from /Users/shengyeong/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.6/lib/active_record/attribute_assignment.rb:50:in `rescue in _assign_attribute'
    [4] pry(#<PostsController>)> params
    => {"utf8"=>"✓",
     "authenticity_token"=>"CNBz5r/549wuGjwm45KlNs38hVCASc5nemUatpVedYY=",
     "post"=>{"feeling_ids"=>["3", ""], "description"=>"I hope this works.",       "url"=>"www.hopes.com"},
     "commit"=>"Create Post",
     "action"=>"create",
     "controller"=>"posts"}


      def post_params
        params.require(:post).permit(:url, :description, feeling_ids: [])
      end

更新:增加了审核模型

class UserFeeling < ActiveRecord::Base
  belongs_to :user
  belongs_to :feeling
end

class User < ActiveRecord::Base
  has_many :posts, dependent: :destroy
  has_many :user_feelings, dependent: :destroy
  has_many :feelings, through: :user_feelings
end

class Post < ActiveRecord::Base
  belongs_to :creator, foreign_key: 'user_id', class_name: 'User'
  has_many :post_feelings
  has_many :feelings, through: :post_feelings
  validates :description, presence: true
  validates :url, presence: true, uniqueness: true
end

class Feeling < ActiveRecord::Base
  has_many :post_feelings
  has_many :posts, through: :post_feelings
  has_many :user_feelings, dependent: :destroy
  has_many :users, through: :user_feelings  
  validates :name, presence: true
end

1 个答案:

答案 0 :(得分:0)

当您有多对多关联时,在大多数情况下,您应该直接使用关联模型(Feeling)而不是中间关系模型(UserFeeling)。 Rails提供了许多实现此目的的方法:has-many-association-reference

在您的情况下,您可以使用 collection = objects

# Just pass ids of feeling you need in
feeling_ids = params[:post][:feeling_ids]
feelings = Feeling.where(id: feeling_ids)
current_user.feelings = feelings

或只是 collection_singular_ids = ids

feeling_ids = params[:post][:feeling_ids]
current_user.feeling_ids = feeling_ids

(这些片段只是示例,您可以参考)

两种方法都使集合仅包含提供的对象,通过适当添加和删除