我有一些复选框,点击后会更新关联的模型。 强制参数允许参数,但不更新模型。
'个人资料'模特有一排伦敦'伦敦'模型。
<form role="form" action="/profiles" method="post">
<div class="form-group">
<input type="hidden" name="authenticity_token" value="<%= form_authenticity_token %>">
<input class="form-control" type="text" name="profile[bio]">
<div class="checkbox">
<label> <input type="checkbox" name="london[south]" value="south">South London</label>
</div>
<div class="checkbox">
<label> <input type="checkbox" name="london[central]" value="central">Central London</label>
</div>
<div class="checkbox">
<label> <input type="checkbox" name="london[north]" value="north">North London</label>
</div>
<div class="checkbox">
<label> <input type="checkbox" name="london[west]" value="west">West London</label>
</div>
<div class="checkbox">
<label> <input type="checkbox" name="london[east]" value="east">East London</label>
</div>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>
我的参数显示在rails服务器日志中:
&#34;个人资料&#34; =&gt; {&#34;生物&#34; =&gt;&#34;&#34;},&#34;伦敦&#34; =&gt; {&# 34;南&#34; =&gt;&#34;南&#34;,&#34;中心&#34; =&gt;&#34;中心&#34;,&#34;北&#34; =&gt;& #34;北&#34;}}
我的控制器:
class ProfilesController < ApplicationController
def new
@profile = Profile.new
end
def create
@profile = Profile.new(profile_params)
if @profile.save
redirect_to '/profiles'
else
render '/profiles'
end
end
private
def profile_params
params.require(:profile).permit(:bio, londons_attributes: [:south, :north, :east, :west, :central])
end
end
答案 0 :(得分:1)
尝试使用
def profile_params
params[:profile].permit!
end