新手问题 部分参数出现 Unpermit

dccmmtop · July 27, 2017 · Last by jasl replied at July 27, 2017 · 1620 hits

这是后台要更新数据的方法

def update
   @node=Node.find_by(id:params[:id])
  if  @node.update(node_params_update)
     redirect_to(wechat_node_path(@node), notice: '修改成功')
   else 
     redirect_to(wechat_node_path(@node), alert: @node.errors.full_messages)
   end
 end

 def node_params_update
   params.require(:node).permit(:access_level,:tag,:name,:section_id,:summary)
 end

 def node_params
   params.require(:node).permit(:name,:section_id,:summary)
 end

这是前台要更新数据的页面

<div class="panel panel-default">
  <div class="panel-body">
    <%= simple_form_for([:wechat,@node]) do |f|%>
    <%= f.input :name%>
    <%= f.input :section_id , :collection => Section.all.collect {|s| [ s.name, s.id ] } %>
    <%= f.input :access_level,:collection=>{"私密"=>0,"半公开"=>1,"公开"=>2}, as: :radio_buttons,label: '状态'%>
    <%= f.input :tag,label: '标签'%>
    <%= f.input :summary ,as: :text%>
    <br>
    <%= f.button :submit, "修 改",class: 'btn  form-control  btn-success '%>
    <br>
    <br>
    <%= link_to "取 消",circle_more_wechat_nodes_path,class: 'btn  form-control  btn-info '%>
    <br>
    <br>
    <% end %>
  </div>
</div>

在后台处理数据更新的那一块代码里面,用到是健壮参数,由于在昨天我写 create action 时,用到的 node_params 这个健壮参数中没有 tag 和 access_level 字段。但是今天写 create action 时,需要更新 tag 和 access_level .就很愚蠢的又写了个 node_params_update.让执行 update action 是去 node_params_update 去找参数。结果就是无论如何 tag 和 access_lev 这两个字段无法更新,后台也没有报错。

后来又仔细看了一遍后台运行的日志,找到了一句 Unpermit tag access_level ,很是奇怪,为什么这两个字段回事 Unpermit 的呢,(ˇˍˇ) name 等其他的数据仍然可以正常更新。我尝试着把 node_params 里面也加上这两个字段

def node_params
  params.require(:node).permit(:access_level,:tag,:name,:section_id,:summary)
end

再次刷新页面,果然可以正常更新了.(其实都用一个 node_params) 就可以了。

但是我不理解的是为什么在执行 update action 时,尽管我已经指定它使用的是 (node_params_update),还是会出现 Unpermit 的情况??

发日志里 parameters: 那段来

You need to Sign in before reply, if you don't have an account, please Sign up first.