Ruby on rails params键不可访问-我将参数内容从窗体丢失到控制器
我不明白,我认为这是一个愚蠢的问题,但我不明白为什么。我有返回2个参数的表单,但我无法检索它们 我就是不知道怎么了Ruby on rails params键不可访问-我将参数内容从窗体丢失到控制器,ruby-on-rails,Ruby On Rails,我不明白,我认为这是一个愚蠢的问题,但我不明白为什么。我有返回2个参数的表单,但我无法检索它们 我就是不知道怎么了 <%= form_for @template do |t| %> <%= t.label :description, "Description" %><br> <%= t.text_field :description %> <%= t.trix_editor :text %> <%= t.subm
<%= form_for @template do |t| %>
<%= t.label :description, "Description" %><br>
<%= t.text_field :description %>
<%= t.trix_editor :text %>
<%= t.submit 'Enregistrer', class: 'Btn' %>
<% end %>
因此,在控制台中:
>> params
=> <ActionController::Parameters {"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"J0pCf1KluTo2uXnpmD9uBFi6bE7FtxI2Cp1gB1KAOFhsR8Sli2lN2vQuXOv/I816vHBT+BYvNb5D7eOdv4CQCQ==", "template"=><ActionController::Parameters {"description"=>"ds", "text"=>"<div>ds s</div>"} permitted: false>, "commit"=>"Enregistrer", "controller"=>"templates", "action"=>"update", "id"=>"2"} permitted: false>
>> params.has_key?(:text)
=> false
>> params.require(:template).permit(:description, :text)
=> <ActionController::Parameters {"description"=>"ds", "text"=>"<div>ds s</div>"} permitted: true>
>> params.has_key?(:text)
=> false
>参数
=> "✓", "_方法“=>”补丁“,”真实性“=>”J0pCf1KluTo2uXnpmD9uBFi6bE7FtxI2Cp1gB1KAOFhsR8Sli2lN2vQuXOv/I816vHBT+BYvNb5D7eOdv4CQCQ==”,”模板“=>”ds“,”文本“=>”ds“}允许:false>,”提交“=>”注册者“,”控制器“=>”模板“,”操作“=>”更新“,”id“=>”2}允许:false>
>>参数是否有_键?(:text)
=>错误
>>参数要求(:模板)。允许(:说明,:文本)
=>“ds”,“text”=>“ds s”}允许:true>
>>参数是否有_键?(:text)
=>错误
任何帮助都将被上诉
greg您需要访问正在发送的参数的“根”,在这种情况下,由于您使用表单作为模板的方式
如果您执行params[:template]
操作,您将获得其中的内容:
<ActionController::Parameters {"description"=>"ds", "text"=>"<div>ds s</div>"} permitted: false>
顺便说一句,描述也是一样:-(我相信你必须使用params[:template]。是否有密钥?(:text)
你是对的,这是“访问路径”它正在工作,你是对的,谢谢你的回答!我只是不明白这次我必须以这种方式访问params,而不是其他形式,我会打个小赌来了解我的错误/误解是什么
params[:template].key?(:text) # true
params[:template][:text] # "<div>dss</div>"
params[:template][:description] # "ds"