2014-12-02 94 views
0

我能夠創建一個新的帖子,可以傳遞將新帖子關聯到current_user和feeling_ids的帖子參數。無法將參數傳遞到表

但是,與此同時,當創建新帖子時,我還想將feeling_ids和current_user關聯在user_feeling表中。感覺和用戶有一個has_many:通過關聯。

到目前爲止,我能夠建立新的行,從而輸入user_id,但無法將feeling_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 

UPDATE:模型中加入導讀

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 
+0

我認爲你不應該使用'params.require(:post).permit(feeling_id:params [:feeling_ids])'來構建user_feelings – 2014-12-02 10:55:24

+0

你可以粘貼你的user,feeling和user_feeling模型嗎? – 2014-12-02 11:08:46

+0

嘿託尼,謝謝你的回覆。是的,我擔心這可能是錯誤的編碼練習。但無論如何,我已經附加了模型。感謝您的幫助。 – chickensmitten 2014-12-02 11:22:52

回答

0

當你有很多-to-many關聯,你應該使用關聯的模型(感覺),而不是直接的中間關係模型(UserFeeling)在多數情況下。 Rails提供了許多方法來實現這一點:has-many-association-reference

在你的情況,你可以使用集合=對象

# 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 =入侵

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

(這些片段只是一個例子,你可以參照到)

兩種方法都會使收集n只包含提供的對象,通過適當添加和刪除

+0

謝謝。幫助突破這一工作的精神障礙。讓我們只是說,如果每次創建一個新的狀態和它的關聯感覺,我只想添加更多的條目,而不是添加新的和刪除舊的。如何才能做到這一點? – chickensmitten 2014-12-03 05:58:27

+0

什麼是新狀態?你的意思是你想在同一個動作中創造新的感受嗎? – 2014-12-03 06:01:31

+0

對不起,我的意思是發佈。 隨着你提供的代碼,我正在取代舊的感覺。不過,我想補充一下新的感受。例如, 當前在user_feelings表中存在user_id = 1且feeling_id = 4。使用你的代碼,當user_id 1添加一個新的感受feel_id 4和1的新帖子時,舊的feeling_id 4將被4和1替換,total_count的感覺是2.我反而希望user_id 1擁有4的feeling_ids, 4和1與total_count的感覺是3. – chickensmitten 2014-12-03 06:11:32