rails(部分)数据更新不成功 出现Unpermit

问题

从前台传到后台update方法是部分参数出现 Unpermit

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

   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的情况??

posted @ 2017-07-26 19:50  -梦里不知身是客  阅读(278)  评论(0编辑  收藏  举报