在views / cards / _form中的我的rails应用程序中,我有f.select字段的代码:
<%= f.select :tag_speciality, options_for_select(@subdomain.tag_speciality), {}, {class: 'form-control'} %>
这给了我输出:
<select class="form-control" name="card[tag_speciality]" id="card_tag_speciality">
<option value="Professor de Yoga">Professor de Yoga</option>
<option value="Professora de Yoga">Professora de Yoga</option>
<option value="Estúdio de Yoga">Estúdio de Yoga</option>
</select>
在我的迁移文件中,我有:
add_column :cards, :tag_speciality, :string, array: true
当我转到表格并选择任何选项时,例如&#34; de Yoga&#34;教授,并保存,我得到结果:
[]
而不是:
["Professor de Yoga"]
这是我的控制者:
def index
@cards = Card.all
end
def create
@card = @user.cards.new card_params
respond_to do |format|
if @card.save
format.html { redirect_to cards_from_subdomain_path(@subdomain.id), notice: 'Card was successfully created.' }
format.json { render :show, status: :created, location: @card }
else
format.html { render :new }
format.json { render json: @card.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @card.update(card_params)
format.html { redirect_to cards_from_subdomain_path(@subdomain.id), notice: 'Card was successfully updated.' }
format.json { render :show, status: :ok, location: @card }
else
format.html { render :edit }
format.json { render json: @card.errors, status: :unprocessable_entity }
end
end
end
# Never trust parameters from the scary internet, only allow the white list through.
def card_params
params.require(:card).permit(
:name,
:phone,
:email,
:cover,
:is_published,
:subdomain_id,
:domain_id,
:profile_id,
:is_solidarity,
:tag_speciality,
:tag_location,
user_id: []
)
end
end
&#13;
我有什么遗失的吗? 感谢
答案 0 :(得分:1)
由于您的tag_speciality是一个数组列,您需要在控制器中使用数组方法分配数据。
@card.tag_speciality.push(card_params[:tag_speciality])
@card.save
当然,您还需要修改其他属性的分配。顺便说一下,您可以看到服务器日志中出现了什么问题。在这种情况下,您应该看到数据库拒绝提交。
答案 1 :(得分:1)
同上@ EJ2015。
添加
@card.tag_speciality.push(card_params[:tag_speciality])
@card.save
到create
。
respond_to do |format|
if @card.tag_speciality.push(card_params[:tag_speciality]) && @card.save
format.html { redirect_to cards_from_subdomain_path(@subdomain.id), notice: 'Card was successfully created.' }
format.json { render :show, status: :created, location: @card }
else
format.html { render :new }
format.json { render json: @card.errors, status: :unprocessable_entity }
end
end