“未知密钥”参数错误

发布于 2024-09-27 22:05:45 字数 3108 浏览 3 评论 0原文

我正在为我的应用程序开发一个基于基本脚手架结构的调节功能。我需要的是使用布尔参数 published on false 编辑多条记录。在 moderate.html 中,我获取了所有未发布条目的列表,并且能够更改其参数 whichwhatpublished。当我尝试通过 complete 操作保存更改时,出现错误。

NamesController#complete 中的参数错误

未知密钥:7、1、4

“7, 1, 4”是我未发布记录的 ID。

以下是我的代码部分:

#names_controller.rb

    def moderate
     @names = Name.find(:all, params[:name_ids], :conditions => {:published => false})
      respond_to do |format|
        format.html { render :action => "moderate" }
         format.xml 
       end
     end

def complete
     @names = Name.find(params[:name_ids])
     @names.each do |name|
       name.update_attributes!(params[:name].reject { |k,v| v.blank? })
     end
     flash[:notice] = "Updated records!"
     redirect_to names_path
   end


#moderate.html.erb
<% form_tag complete_names_path do %>  
  <% @names.each do |name| %>
    <fieldset>
    <% fields_for "name_ids[#{name.id}]", name do |name_fields| %>
    <%= name_fields.text_field :which %>
    <%= name_fields.text_field :what %>
    <%= name_fields.check_box :published %> 
    <% end %>
    </fieldset>   
  <% end %>  
<%= submit_tag "Ok" %>
<% end %>/


#routes.rb
ActionController::Routing::Routes.draw do |map| 
  map.connect 'moderate', :controller => 'names', :action => 'moderate'
  map.resources :names, :collection => { :complete => :put}
  map.root :names
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'

我明白,name_ids 有问题,但不明白,我应该做什么。 先感谢您。

Ruby 1.8.7(2009-06-12 补丁级别 174)

[universal-darwin10.0]Rails 2.3.5

Rails 日志中的 moderatecomplete 操作:

<块引用>

处理 NamesController#moderate(对于 127.0.0.1,2010-10-16 21:36:42) [GET] [4;35;1m名称加载 (0.6ms)[0m [0mSELECT * FROM "名称" WHERE ("姓名"."已发布" = 'f') [0m 渲染模板内 布局/名称渲染名称/中等 在 12 毫秒内完成(视图:7,DB:1)| 200 OK [http://localhost/moderate]

处理 NamesController#complete (对于 127.0.0.1,2010-10-16 21:36:49) [POST] 参数:{"commit"=>"确定", “authenticity_token”=>“CtmsjIavksOMSIArrdovkkzuZzHVjkenFFMO5bHIvgg=”, "name_ids"=>{"7"=>{"已发布"=>"0", "what"=>"Партия", "which"=>"Крутая"}, "1"=>​​{"已发布"=>"1", "什么"=>"Россия", "哪个"=>"Единая"}, "4"=>{"已发布"=>"0", "什么"=>"Организация", “which”=>“Молдавская”}}}
[4;36;1m名称负载 (0.4ms)[0m
[0;1mSELECT * FROM "名称" WHERE ("名称"."id" IN (7,1,4)) [0m

NoMethodError(您有一个零对象 当你没想到的时候!你可能会 期望有一个 Array 的实例。 评估时发生错误 nil.reject):
应用程序/控制器/names_controller.rb:47:in 完成'
应用程序/控制器/names_controller.rb:46:in
每个'
应用程序/控制器/names_controller.rb:46:in `完成'

渲染救援/_trace (110.3ms) 渲染救援/_request_and_response (0.5ms) 渲染救援/布局 (内部服务器错误)

I'm working on a moderating feature for my application, which is based on a basic scaffold structure. What I need, is to edit several records with the boolean parameter publised on false. In moderate.html I'm getting the list of all unpublished entries with the ability to change their parameters which, what and published. The error appears, when I'm trying to save the changes through the complete action.

ArgumentError in NamesController#complete

Unknown key(s): 7, 1, 4

The "7, 1, 4" are id of my unpublished records.

Here are the parts of my code:

#names_controller.rb

    def moderate
     @names = Name.find(:all, params[:name_ids], :conditions => {:published => false})
      respond_to do |format|
        format.html { render :action => "moderate" }
         format.xml 
       end
     end

def complete
     @names = Name.find(params[:name_ids])
     @names.each do |name|
       name.update_attributes!(params[:name].reject { |k,v| v.blank? })
     end
     flash[:notice] = "Updated records!"
     redirect_to names_path
   end


#moderate.html.erb
<% form_tag complete_names_path do %>  
  <% @names.each do |name| %>
    <fieldset>
    <% fields_for "name_ids[#{name.id}]", name do |name_fields| %>
    <%= name_fields.text_field :which %>
    <%= name_fields.text_field :what %>
    <%= name_fields.check_box :published %> 
    <% end %>
    </fieldset>   
  <% end %>  
<%= submit_tag "Ok" %>
<% end %>/


#routes.rb
ActionController::Routing::Routes.draw do |map| 
  map.connect 'moderate', :controller => 'names', :action => 'moderate'
  map.resources :names, :collection => { :complete => :put}
  map.root :names
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'

I understand, that there's something wrong with the name_ids, but don'nt understand, what should I do.
Thank you in advance.

ruby 1.8.7 (2009-06-12 patchlevel 174)

[universal-darwin10.0] Rails 2.3.5

Rails log for moderate and complete actions:

Processing NamesController#moderate (for 127.0.0.1 at 2010-10-16 21:36:42)
[GET] [4;35;1mName Load (0.6ms)[0m
[0mSELECT * FROM "names" WHERE
("names"."published" = 'f') [0m
Rendering template within
layouts/names Rendering names/moderate
Completed in 12ms (View: 7, DB: 1) |
200 OK [http://localhost/moderate]

Processing NamesController#complete
(for 127.0.0.1 at 2010-10-16 21:36:49)
[POST] Parameters: {"commit"=>"Ok",
"authenticity_token"=>"CtmsjIavksOMSIArrdovkkzuZzHVjkenFFMO5bHIvgg=",
"name_ids"=>{"7"=>{"published"=>"0",
"what"=>"Партия", "which"=>"Крутая"},
"1"=>{"published"=>"1",
"what"=>"Россия", "which"=>"Единая"},
"4"=>{"published"=>"0",
"what"=>"Организация",
"which"=>"Молдавская"}}}
[4;36;1mName Load (0.4ms)[0m
[0;1mSELECT * FROM "names" WHERE
("names"."id" IN (7,1,4)) [0m

NoMethodError (You have a nil object
when you didn't expect it! You might
have expected an instance of Array.
The error occurred while evaluating
nil.reject):
app/controllers/names_controller.rb:47:in
complete'
app/controllers/names_controller.rb:46:in
each'
app/controllers/names_controller.rb:46:in
`complete'

Rendered rescues/_trace (110.3ms)
Rendered rescues/_request_and_response
(0.5ms) Rendering rescues/layout
(internal_server_error)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无敌元气妹 2024-10-04 22:05:45

可能您只需要从 name_ids 哈希中获取键。尝试:

@names = Name.find(params[:name_ids].keys)

一个单独的问题是您对 params[:name] 的引用,它为零。你的意思是(编辑:使用 to_s 来匹配参数键,哈哈):

@names.each do |name|
  name.update_attributes!(params[:name_ids][name.id.to_s].reject { |k,v| v.blank? })
end

编辑(简短的解释):

发生的事情是你在参数 params[:name_ids] 中有一个嵌套哈希。它看起来像:

"name_ids"=>{"7"=>{"published"=>"0", "what"=>"Партия", "which"=>"Крутая"}, "1"=>{"published"=>"1", "what"=>"Россия", "which"=>"Единая"}, "4"=>{"published"=>"0", "what"=>"Организация", "which"=>"Молдавская"}}

ActiveRecord“查找”方法可以采用 ids 数组,但不能采用值的哈希值。您最初在这一行中提交给“find”的内容:

@names = Name.find(params[:name_ids])

...是 params[:name_ids]: 的值:

{"7"=>{"published"=>"0", "what"=>"Партия", "which"=>"Крутая"}, "1"=>{"published"=>"1", 
"what"=>"Россия", "which"=>"Единая"}, "4"=>{"published"=>"0", "what"=>"Организация", 
"which"=>"Молдавская"}

当您想要的是:

@names = Name.find(['7','1','4'])

调用 params[:name_ids].keys 为您提供的值。

第二个问题是这一行:

name.update_attributes!(params[:name].reject { |k,v| v.blank? })

params 中没有值 ':name',因此对其调用 'reject' 会导致 'no method' 错误 - nil 对象上没有 'reject' 方法。您想要的是更新与循环中特定名称相对应的“名称”的属性。这意味着您想要从 params[:name_ids][:id] 中获取值,其中 :id 是“name”的 id。

这一切都可以追溯到 fields_for 创建参数的方式。这行:

<% fields_for "name_ids[#{name.id}]", name do |name_fields| %>

意味着 params 将包含一个名为“name_ids”的散列,其键对应于 name.id,其本身将包含 ActiveRecord 可以在 update_attributes 方法中使用的属性散列。

其中有很多著名的 Rails 魔法需要跟踪——这有帮助吗?

Likely you need to get just the keys from the name_ids hash. Try:

@names = Name.find(params[:name_ids].keys)

A separate problem is your reference to params[:name], which is nil. Did you mean (EDIT: use to_s to match the params key, lol):

@names.each do |name|
  name.update_attributes!(params[:name_ids][name.id.to_s].reject { |k,v| v.blank? })
end

EDIT (brief-ish explanation):

What was happening was that you had a nested hash in the params, params[:name_ids]. It looked like:

"name_ids"=>{"7"=>{"published"=>"0", "what"=>"Партия", "which"=>"Крутая"}, "1"=>{"published"=>"1", "what"=>"Россия", "which"=>"Единая"}, "4"=>{"published"=>"0", "what"=>"Организация", "which"=>"Молдавская"}}

An ActiveRecord 'find' method can take an array of ids, but not a hash of values. What you were originally submitting to 'find' in this line:

@names = Name.find(params[:name_ids])

...was the value for params[:name_ids]:

{"7"=>{"published"=>"0", "what"=>"Партия", "which"=>"Крутая"}, "1"=>{"published"=>"1", 
"what"=>"Россия", "which"=>"Единая"}, "4"=>{"published"=>"0", "what"=>"Организация", 
"which"=>"Молдавская"}

When what you wanted was:

@names = Name.find(['7','1','4'])

which is what calling params[:name_ids].keys gives you.

The second problem was this line:

name.update_attributes!(params[:name].reject { |k,v| v.blank? })

There was no value ':name' in params, so calling 'reject' on it cause the 'no method' error -- there is no 'reject' method on the nil object. What you wanted was to update the attributes for the 'name' that corresponded to the particular name in the loop. This meant you wanted to get the values out of params[:name_ids][:id] where :id was the id of 'name'.

It all goes back to the way fields_for created the params to begin with. This line:

<% fields_for "name_ids[#{name.id}]", name do |name_fields| %>

meant that params would contain a hash called 'name_ids', with keys corresponding to name.id, that themselves would contain hashes of attributes that ActiveRecord could use in the update_attributes method.

There's a good bit of the famous Rails magic to keep track of in there -- does that help?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文