简单!由于错误的参数没有更新属性,为什么?轨道

时间:2014-07-13 13:12:43

标签: ruby-on-rails ruby-on-rails-4 parameters routing

我有一个对象列表,每个对象下面都有一个按钮来接受'或者'拒绝'。此按钮可更改' workflow_state'对象的属性。但是,当我按下按钮时,我留下了以下错误

param is missing or the value is empty: photo

Extracted source (around line #41):



  def params_photo
    params.require(:photo).permit!
  end

  def get_photos

Request

Parameters:

{"utf8"=>"✓",
 "_method"=>"patch",
 "authenticity_token"=>"qcFgYRM6+O7VpRo+jdVyE1OCTtHiFWpaQGJuC2qNQjw=",
 "button"=>"rejected",
 "id"=>"9"}

服务器输出

Started PATCH "/admin/entries/9" for 127.0.0.1 at 2014-07-13 15:05:56 +0100
Processing by Admin::EntriesController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"qcFgYRM6+O7VpRo+jdVyE1OCTtHiFWpaQGJuC2qNQjw=", "button"=>"rejected", "id"=>"9"}
Completed 400 Bad Request in 1ms

ActionController::ParameterMissing (param is missing or the value is empty: photo):
  app/controllers/admin/entries_controller.rb:41:in `params_photo'
  app/controllers/admin/entries_controller.rb:15:in `update'

对象初始状态始终为'待定'。我只想要一个简单的方法来改变状态,但我正在努力实现它。帮助将不胜感激。

这是相关的控制器

class Admin::EntriesController < ApplicationController

  expose(:entries){@entries}


  def index
    @entries = Photo.with_pending_state 

  end

  def show
  end

  def update
    @entry.update_attributes(params_photo)
    if @entry.save
      return redirect_to(admin_entries_path)
    else
      return render("edit")
    end
  end



  def approved 
    @entries = Photo.with_approved_state
  end

  def pending
    @entries = Photo.with_pending_state 
  end

  def rejected
    @entries = Photo.with_rejected_state
  end


  private

  def params_photo
    params.require(:photo).permit!
  end

  def get_photos
    @entries = Entry.all
  end

end

带切换按钮的相关视图(目前只有一个,想让该死的东西先工作!)

<% if entries.any? %>
  <% entries.each do |photo| %>
    <%= photo.firstname %>
    <%= image_tag photo.attachment.url %>
    <%= form_for photo, url: admin_entry_path(photo) do |t| %>
      <%= t.button :workflow_state, :value=>"rejected" %>
    <% end %>
  <% end %>
<% end %>

为了进一步明确我的路线(这些事情让我疯了)

  namespace :admin do
    resources :entries do
      get 'toggle_approve', :on => :member  
      collection do
        get :pending
        get :rejected
        get :approved

      end
    end
  end

这感觉好像应该相对简单,但我已经绕圈试图让它发挥作用。非常感谢帮助。

提前谢谢。

1 个答案:

答案 0 :(得分:0)

<强> PARAMS

您的问题可能是因为您在require

中使用了strong_params方法

虽然这很好但是正确的(form_for发送嵌入你想要发送的对象名称中的参数),看起来你的参数看起来不像。


<强>预期

基本上,对于strong_params,您的require适用于&#34;父母&#34; key&#34;,permit属性适用于&#34; child&#34;键。您当前的方法是期待这些参数:

"params" => {
    "photo" => {
         "button" => "rejected"
     }
}

<强>实际

{"utf8"=>"✓",
 "_method"=>"patch",
 "authenticity_token"=>"qcFgYRM6+O7VpRo+jdVyE1OCTtHiFWpaQGJuC2qNQjw=",
 "button"=>"rejected",
 "id"=>"9"}

因此,当您收到有关&#34;照片&#34;的错误时param - 它是一个例外,因为你要求该参数存在,因此如果不存在,Rails只能停止。

修复将是仅允许button参数,或设置正确的form_for方法以确保传递正确的参数:

params.permit(:button)

#app/controllers/photos_controller.rb
Class PhotosController < ApplicationController
    def new
        @photo = Photo.new
    end
end

<%= form_for @photo do |f| %>
    ...
<% end %>