我能夠創建一個新的帖子,可以傳遞將新帖子關聯到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
我認爲你不應該使用'params.require(:post).permit(feeling_id:params [:feeling_ids])'來構建user_feelings – 2014-12-02 10:55:24
你可以粘貼你的user,feeling和user_feeling模型嗎? – 2014-12-02 11:08:46
嘿託尼,謝謝你的回覆。是的,我擔心這可能是錯誤的編碼練習。但無論如何,我已經附加了模型。感謝您的幫助。 – chickensmitten 2014-12-02 11:22:52