提交表单时Rails未定义的方法

时间:2015-08-11 18:07:19

标签: ruby-on-rails ruby forms ruby-on-rails-4

我在这个问题上的智慧结束了,我一直在努力解决这个问题大约一个星期,所以我希望有人可以告诉我我做错了什么。我正在使用activeldap编写LDAP应用程序,但我似乎无法使LDAP修改方法正常工作。

我正在使用_modify partial定位用户,然后将params传递给控制器​​以加载另一个表单的实例变量,以便我可以查看/编辑用户配置文件。

zion_users / _modify.html.haml

= simple_form_for :modify_entry, :url => modify_entry_zion_users_path, :method => :post, :html => { :class => 'form-horizontal' } do |f|
  = f.error_notification

  .control-group
    .control-label
      = f.label :un, "Username"
    .controls
      = f.select :un, options_for_select(ZionUser.form_array.sort), include_blank: true

  .control-group
    .controls
      = f.submit 'Search', :class => 'btn btn-primary'
      = link_to "Cancel", zion_users_path, :class => "btn btn-danger", 'data-no-turbolink' => true

zion_users_controller.rb

  def modify_user
    values = params[:modify_entry]
    @modify_user = ZionUser.modify_user(values[:cn],values[:gidnumber],values[:shadowlastchange],values[:uid],values[:loginshell],values[:sn],values[:gecos],values[:homedirectory],values[:uidnumber])

    if @modify_user
      redirect_to :back, :flash => { :success => 'Zion user was successfully changed'}
    else
      redirect_to :back,  :flash => { :error => 'ERROR: Zion user was NOT created because the username already exists'}
    end
  end

  def modify_entry
    values = params[:modify_entry]
    @zion_users = ZionUser.find(values[:un])
  end

zion_users / _modify_entry.html.haml

= simple_form_for :modify_user, :url => url_for(:controller => 'zion_users', :action => 'modify_user'), :method => :post, :html => { :class => 'form-horizontal' } do |f|
  = f.error_notification

  %dl.dl-horizontal
    %dt
      dn
    %dd
      = @zion_users.dn

  .control-group
    .control-label
      = f.label :uidnumber, "uidnumber"
    .controls
      = f.input_field :uidnumber, :value => @zion_users.uidnumber, :label => false, :class => 'required'
  .control-group
    .control-label
      = f.label :uid, "uid"
    .controls
      = f.input_field :uid, :value => @zion_users.uid, :label => false, :class => 'required'
  .control-group
    .control-label
      = f.label :cn, "cn"
    .controls
      = f.input_field :cn, :value => @zion_users.cn, :label => false, :class => 'required'
  .control-group
    .control-label
      = f.label :sn, "sn"
    .controls
      = f.input_field :sn, :value => @zion_users.sn, :label => false, :class => 'required'
  .control-group
    .control-label
      = f.label :gidnumber, "gidnumber"
    .controls
      = f.input_field :gidnumber, :value => @zion_users.gidnumber, :label => false, :class => 'required'
  .control-group
    .control-label
      = f.label :loginshell, "loginshell"
    .controls
      = f.input_field :loginshell, :value => @zion_users.loginshell, :label => false, :class => 'required'
  .control-group
    .control-label
      = f.label :homedirectory, "homedirectory"
    .controls
      = f.input_field :homedirectory, :value => @zion_users.homedirectory, :label => false, :class => 'required'
  .control-group
    .control-label
      = f.label :gecos, "gecos"
    .controls
      = f.input_field :gecos, :value => @zion_users.gecos, :label => false, :class => 'required'
  .control-group
    .control-label
      = f.label :shadowlastchange, "shadowlastchange"
    .controls
      = f.input_field :shadowlastchange, :value => @zion_users.shadowlastchange, :label => false, :class => 'required'

  .control-group
    .controls
      = f.submit 'Submit', :class => 'btn btn-primary'
      = link_to "Cancel", zion_users_path, :class => "btn btn-danger", 'data-no-turbolink' => true

的routes.rb

  resources :zion_users do
    match 'delete', :on => :collection, :via => [:get, :post]
    match 'password', :on => :collection, :via => [:get, :post]
    match 'modify', :on => :collection, :via => [:get, :post]
    match 'modify_entry', :on => :collection, :via => [:get, :post]
    match 'modify_user', :on => :collection, :via => [:get, :post]
  end

现在我迷失了。我按预期获得了加载了用户个人资料的表单,但在提交此表单时出现此错误,我不明白它试图告诉我的内容。请求参数哈希看起来很好,它将进行正确的操作,控制器方法ZionUser.modify_user在模型中定义,我已经确认它正常工作。任何帮助将不胜感激。

Started POST "/zion_users/modify_user" for 127.0.0.1 at 2015-08-11 11:31:48 -0600
Processing by ZionUsersController#modify_user as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"tPEW3wVD367iJCKsLeSAflWJ0Z6ROCzFhusW6FRhyVg8bx4nHGx6SG9QJUyGpNfauRSeMqy5SO4OyTEqR9ybQg==", "modify_user"=>{"uidnumber"=>"1237", "uid"=>"abby.brown", "cn"=>"abby.brown", "sn"=>"abby.brown", "gidnumber"=>"100", "loginshell"=>"/bin/bash", "homedirectory"=>"/opt/ltsp-users/abby.brown", "gecos"=>"ltsp", "shadowlastchange"=>"15096"}, "commit"=>"Submit"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
Completed 500 Internal Server Error in 23ms (ActiveRecord: 1.0ms)

NoMethodError - undefined method `[]' for nil:NilClass:
  • 我改变了我的控制器方法,如下所示
  def modify_user
    values = params[:modify_user]
    @modify_user = ZionUser.modify_user(values[:cn],values[:gidnumber],values[:shadowlastchange],values[:uid],values[:loginshell],values[:sn],values[:gecos],values[:homedirectory],values[:uidnumber])

    if @modify_user
      redirect_to :back, :flash => { :success => 'Zion user was successfully changed'}
    else
      redirect_to :back,  :flash => { :error => 'ERROR: Zion user was NOT created because the username already exists'}
    end
  end
Started POST "/zion_users/modify_user" for 127.0.0.1 at 2015-08-11 12:40:01 -0600
Processing by ZionUsersController#modify_user as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"3NCseVnbPVag0gwRkNc4CVMcMFwN48H48Mn3MGD8rmhUTqSBQPSYsC2mC/E7l2+tv4F/8DBipdN469Dyc0H8cg==", "modify_user"=>{"uidnumber"=>"1237", "uid"=>"abby.brown", "cn"=>"abby.brown", "sn"=>"abby.brown", "gidnumber"=>"100", "loginshell"=>"/bin/sh", "homedirectory"=>"/opt/ltsp-users/abby.brown", "gecos"=>"ltsp", "shadowlastchange"=>"15096"}, "commit"=>"Submit"}
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
  LDAP: search_full (9.3ms) {:base=>"ou=people,dc=zion,dc=in", :scope=>:sub, :filter=>"(&(uid=abby.brown)(objectClass=top))", :attributes=>["*", "objectClass"], :limit=>1}
Redirected to http://localhost:3000/zion_users/modify_entry
Completed 302 Found in 23ms (ActiveRecord: 0.4ms)


Started GET "/zion_users/modify_entry" for 127.0.0.1 at 2015-08-11 12:40:01 -0600
Processing by ZionUsersController#modify_entry as HTML
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.2ms)

NoMethodError - undefined method `[]' for nil:NilClass:
  app/controllers/zion_users_controller.rb:54:in `modify_entry'
  actionpack (4.2.3) lib/action_controller/metal/implicit_render.rb:4:in `send_action'

1 个答案:

答案 0 :(得分:0)

所以第一个问题可以通过更改像这样的

这样的参数的提取来简单地解决
values = params[:modify_user]

然后我们讨论的第二个问题出现在重定向上。在重定向上,它转到Redirected to http://localhost:3000/zion_users/modify_entry

期待params[:modify_entry],但由于params中没有modify_entry,因此崩溃会导致nil类错误。如果您重定向到index路径或根路径,一切都会正常工作。