collection_select未在數據庫中保存選定的選項

[英]collection_select not saving selected option in database


I have collection_select in one of my views, which properly creates a HTML <select> menu, but when I select an option it does not save in the database. In the same view I have other fields from the same db table and they save up. Here are my models:

我在其中一個視圖中有collection_select,它正確地創建了一個HTML

class TrainingPart < ActiveRecord::Base
  belongs_to :activity
  belongs_to :training  
  accepts_nested_attributes_for :activity, :allow_destroy => true
end

class Activity < ActiveRecord::Base
  has_many :training_parts 
end

The partial is:

部分是:

<div class="part">
  <%= f.label :activity, "Activity" %>
  <%= collection_select :training_part, :activity_id, Activity.all, :id, :name %>
  <%= f.text_field :activity_id %>
  <%= f.text_field :amount %>  
</div> 

The amount field works fine.

金額字段工作正常。

EDIT: I don't use attr_accessible in any model, so all of the fields in all tables are accessible. (reference: Rails mass assignment definition and attr_accessible use)

編輯:我不在任何模型中使用attr_accessible,因此所有表中的所有字段都是可訪問的。 (參考:Rails質量分配定義和attr_accessible使用)

3 个解决方案

#1


1  

I managed to do it. It turned out that I had 2 mistakes:

我設法做到了。原來我犯了2個錯誤:

1) I had to remove <%= f.text_field :activity_id %> from the view as this was the field, which I was trying to set with the select menu.

1)我不得不從視圖中刪除<%= f.text_field:activity_id%>,因為這是我嘗試使用選擇菜單設置的字段。

2) I should've used f.collection_select instead of collection_select

2)我應該使用f.collection_select而不是collection_select

#2


0  

Allow mass-assignment to whatever attributes you want settable from the view:

允許批量分配到您希望從視圖中設置的任何屬性:

attr_accessible :activity_id, :amount # ...any others...

"Mass-assignment" means setting many attributes at once with data from the view, such as from forms. These attributes are protected by default to prevent users from injecting their own values (eg. admin = true).

“質量分配”意味着使用視圖中的數據(例如來自表單)一次設置許多屬性。默認情況下,這些屬性受到保護,以防止用戶注入自己的值(例如,admin = true)。

Use attr_accessible to declare which of your model's attributes are mass-assignable.

使用attr_accessible聲明模型的哪些屬性是可批量分配的。

Also, since you are accepting nested attributes for Activity, you must include :activity_attributes among your list of accessible attributes.

此外,由於您接受Activity的嵌套屬性,因此必須在可訪問屬性列表中包含:activity_attributes。

#3


0  

For me the problem was that I didn't whitelist the field in my controller.

對我來說問題是我沒有將控制器中的字段列入白名單。

Say your select menu has blog posts as options so you can choose what blog a comment belongs to

假設您的選擇菜單包含博客文章作為選項,以便您可以選擇評論所屬的博客

f.collection_select :post_id, Post.all, :id, :name

Then you'll need to permit post_id in your CommentsController

然后你需要在CommentsController中允許post_id

def update
  # ...
  if @comment.update_attributes(post_params)
  # ...
end

def post_params
  params.require(:comment).permit(:name, :post_id)  # Make sure the field used with your select menu is permitted! 
end

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/02/16/7258f1da5b02fb2631d1dbe8984faa8d.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com