我有兩個模型,Posts
和Teams
,當用戶創建一個帖子時,我希望用戶選擇它應該屬於哪個團隊。我想用select_tag
來做。 (我用HAML
)我現在擁有的是:Rails 4 - select_tag傳遞參數給控制器
= f.label :team_id, "Select team"
= select_tag :team_id, options_from_collection_for_select(current_user.teams, :id, :name)
在我的控制器:
def create
# I want to pass :team_id here, but I'n not sure how...
@team = current_user.teams.find post_params[:team_id]
@post = @team.posts.build post_params
@post.user = current_user
這給了我下面的錯誤:
Couldn't find Team without an ID
注意
我以前使用的單選按鈕來實現這一點,它看起來像這樣(和工作):
- current_user.teams.each do |team|
= f.radio_button 'team_id', team.id
= team.name
那麼,如何才能實現我使用select_tag
是一回事嗎?
你能向我們展示你的'post_params'方法? – JTG