我正在将 3.2.2 的rails应用程序升级到rails 5.1.4 。 我遇到了与attr_accessor强烈对比的问题。
控制台参数输出:为什么我被允许:false
<ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"8/6yMCzyPw==", "journal"=><ActionController::Parameters {"user_id"=>"1", "subject"=>"sdf",
"body"=>"sdf", "recipients"=>["zzzzz@yahoo.com"]} permitted: false>, "commit"=>"Submit", "controller"=>"journals", "action"=>"create", "assignment_id"=>"9081"} permitted: false>
这是我的控制器创建操作: journals_controller.rb
def create
@facility = Facility.find(params[:facility_id])
@journal = @facility.journals.new(journal_params)
@journal.save
end
private
def journal_params
params.require(:journal).permit(:user_id, :body, :recipients, :subject)
end
journal.rb
attr_accessor :recipients
轴颈/视图/ _form.html.erb
<%= form_for [@assignments, @journal] do |f| %>
<%= form_legend %>
<%= f.error_messages %>
<p>
<%= f.label :subject %><%= required_field %><br />
<%= f.text_field :subject, :size => 80 %>
</p>
<p>
<%= f.label :body %><%= required_field %><br />
<%= f.text_area :body, :cols => 80, :rows => 5 %>
</p>
<% unless params[:action] == 'edit' %>
<fieldset>
<table class="list">
<% for group in Journal.possible_recipients.in_groups(3, false) %>
<td>
<% for recipient in group %>
<%= check_box_tag "journal[recipients][]", recipient.try(:email) %> <%= bold_assignees_helper recipient %><br />
<% end %>
</td>
<% end %>
</table>
</fieldset>
<% end %>
<p>
<%= f.submit 'Submit' %>
</p>
<% end %>
允许使用强制参数,但仍然允许:在控制台上显示为
答案 0 :(得分:1)
将您允许的参与号更改为允许array of recipients
,如下所示:
def journal_params
params.require(:journal).permit(:user_id, :body, recipients: [], :subject)
end
如果错误仍然存在,请告诉我,我会尽力帮助您
答案 1 :(得分:0)
permitted = false
以允许哈希中的所有参数,否则 permit!
是默认值。但是它会破坏强参数的使用。