Rails和jquery使用Ajax问题更新。每个块

Rails和jquery使用Ajax问题更新。每个块,jquery,ruby-on-rails,ajax,Jquery,Ruby On Rails,Ajax,我这里有一个rails应用程序,带有一个基本表单。我的目标是创建一个表单,将新用户添加到数据库中,然后使用jquery和ajax更新.each块 目前,数据很好地输入到数据库中,但是前置会导致一个nil错误,而不是最新的条目。很明显,这个变量在某个点上正在失去它的值 这是我的密码: /控制器/注册\u controller.rb class RegistrationsController < Devise::RegistrationsController skip_before_fil

我这里有一个rails应用程序,带有一个基本表单。我的目标是创建一个表单,将新用户添加到数据库中,然后使用jquery和ajax更新.each块

目前,数据很好地输入到数据库中,但是前置会导致一个nil错误,而不是最新的条目。很明显,这个变量在某个点上正在失去它的值

这是我的密码:

/控制器/注册\u controller.rb

class RegistrationsController < Devise::RegistrationsController
  skip_before_filter :require_no_authentication
 def create
    @user = User.new(params[:user])

    respond_to do |format|
      if @user.save
        set_flash_message(:notice, :signed_up)
        format.html { sign_in_and_redirect :user, @user}
        format.js { render 'landing/create'}
      else
        format.html { render action: "new" }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end
...
end
...
    @sub_accounts = User.where(:agent_id => current_user.id)
...
下面是我的服务器日志

Started POST "/users" for 127.0.0.1 at 2013-09-25 01:31:10 -0500
Processing by RegistrationsController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"8PopBt4QrbsseKlJusgjYGoStd3UqPtkfVcXfswldm4=", "user"=>{"agent_id"=>"70", "agent_username"=>"jeufhfuiruieie9988897", "email"=>"kjfjie9e9uirur4849@djfrieieie9e.com", "birthday(2i)"=>"9", "birthday(3i)"=>"25", "birthday(1i)"=>"2013", "first_name"=>"0005", "last_name"=>"00006", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "privacy_level"=>""}}
   (0.1ms)  begin transaction
  User Exists (0.1ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" = 'kjfjie9e9uirur4849@djfrieieie9e.com' LIMIT 1
  User Exists (0.1ms)  SELECT 1 AS one FROM "users" WHERE LOWER("users"."agent_username") = LOWER('jeufhfuiruieie9988897') LIMIT 1
Binary data inserted for `string` type on column `encrypted_password`
  SQL (0.5ms)  INSERT INTO "users" ("admin_only", "agent_id", "agent_username", "birthday", "created_at", "current_sign_in_at", "current_sign_in_ip", "email", "encrypted_password", "first_name", "last_name", "last_sign_in_at", "last_sign_in_ip", "middle_name", "password_digest", "privacy_level", "remember_created_at", "reset_password_sent_at", "reset_password_token", "sign_in_count", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["admin_only", nil], ["agent_id", 70], ["agent_username", "jeufhfuiruieie9988897"], ["birthday", Wed, 25 Sep 2013], ["created_at", Wed, 25 Sep 2013 06:31:10 UTC +00:00], ["current_sign_in_at", nil], ["current_sign_in_ip", nil], ["email", "kjfjie9e9uirur4849@djfrieieie9e.com"], ["encrypted_password", "$2a$10$hQP3HX1tf0cVDhnOVrqMuOntPMOZidRpVcVj2tNF2XSy1pdeWRYiK"], ["first_name", "0005"], ["last_name", "00006"], ["last_sign_in_at", nil], ["last_sign_in_ip", nil], ["middle_name", nil], ["password_digest", nil], ["privacy_level", nil], ["remember_created_at", nil], ["reset_password_sent_at", nil], ["reset_password_token", nil], ["sign_in_count", 0], ["updated_at", Wed, 25 Sep 2013 06:31:10 UTC +00:00]]
   (2.9ms)  commit transaction
  Rendered landing/_sub_account_partial.html.erb (1.0ms)
  Rendered landing/create.js.erb (2.1ms)
Completed 500 Internal Server Error in 99ms

ActionView::Template::Error (undefined method `full_name' for nil:NilClass):
    1: <li><%= current_sub_account.full_name %></li>
  app/views/landing/_sub_account_partial.html.erb:1:in `_app_views_landing__sub_account_partial_html_erb___717255238604345729_70329913890480'
  app/views/landing/create.js.erb:1:in `_app_views_landing_create_js_erb___1846104658260022742_70329904266920'
  app/controllers/registrations_controller.rb:10:in `block (2 levels) in create'
  app/controllers/registrations_controller.rb:6:in `create'


  Rendered /Users/mikeland/.rvm/gems/ruby-1.9.3-p362@rails32/gems/actionpack-3.2.12/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.3ms)
  Rendered /Users/mikeland/.rvm/gems/ruby-1.9.3-p362@rails32/gems/actionpack-3.2.12/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.1ms)
  Rendered /Users/mikeland/.rvm/gems/ruby-1.9.3-p362@rails32/gems/actionpack-3.2.12/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (9.0ms)

提前感谢您

您尚未在创建操作中定义@current\u sub\u account,因此将向该分部传递一个nil实例变量


您需要类似于@current\u sub\u account=在landing\u controller.rb中的创建操作中,您的子帐户对象是什么。。。然后使用活期账户。这是怎么回事?我用当前账户作为每个区块的变量。嗯,我明白了。当你呈现create.js.erb?WORKS时,你从哪里得到@current\u sub\u account?我在我的RegistrationController中添加了@current\u sub\u account=User.where:agent\u id=>current\u User.id.last after if@User.save。
 <%= form_tag(user_registration_path, :remote => true) do %>
    <%= hidden_field_tag "user[agent_id]", current_user.id %>
    <tr>Agent Username<br></tr>
    <tr><%= text_field_tag "user[agent_username]" %><br></tr>
    <tr>Email (optional)<br></tr>
    <tr><%= email_field_tag "user[email]" %><br></tr>
    ...
  <% end %>
<ul>

  <% @sub_accounts.each do |current_sub_account| %>
    <%= render 'landing/sub_account_partial', :current_sub_account => current_sub_account %>
  <% end %>
</ul>
...
    @sub_accounts = User.where(:agent_id => current_user.id)
...
Started POST "/users" for 127.0.0.1 at 2013-09-25 01:31:10 -0500
Processing by RegistrationsController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"8PopBt4QrbsseKlJusgjYGoStd3UqPtkfVcXfswldm4=", "user"=>{"agent_id"=>"70", "agent_username"=>"jeufhfuiruieie9988897", "email"=>"kjfjie9e9uirur4849@djfrieieie9e.com", "birthday(2i)"=>"9", "birthday(3i)"=>"25", "birthday(1i)"=>"2013", "first_name"=>"0005", "last_name"=>"00006", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "privacy_level"=>""}}
   (0.1ms)  begin transaction
  User Exists (0.1ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" = 'kjfjie9e9uirur4849@djfrieieie9e.com' LIMIT 1
  User Exists (0.1ms)  SELECT 1 AS one FROM "users" WHERE LOWER("users"."agent_username") = LOWER('jeufhfuiruieie9988897') LIMIT 1
Binary data inserted for `string` type on column `encrypted_password`
  SQL (0.5ms)  INSERT INTO "users" ("admin_only", "agent_id", "agent_username", "birthday", "created_at", "current_sign_in_at", "current_sign_in_ip", "email", "encrypted_password", "first_name", "last_name", "last_sign_in_at", "last_sign_in_ip", "middle_name", "password_digest", "privacy_level", "remember_created_at", "reset_password_sent_at", "reset_password_token", "sign_in_count", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["admin_only", nil], ["agent_id", 70], ["agent_username", "jeufhfuiruieie9988897"], ["birthday", Wed, 25 Sep 2013], ["created_at", Wed, 25 Sep 2013 06:31:10 UTC +00:00], ["current_sign_in_at", nil], ["current_sign_in_ip", nil], ["email", "kjfjie9e9uirur4849@djfrieieie9e.com"], ["encrypted_password", "$2a$10$hQP3HX1tf0cVDhnOVrqMuOntPMOZidRpVcVj2tNF2XSy1pdeWRYiK"], ["first_name", "0005"], ["last_name", "00006"], ["last_sign_in_at", nil], ["last_sign_in_ip", nil], ["middle_name", nil], ["password_digest", nil], ["privacy_level", nil], ["remember_created_at", nil], ["reset_password_sent_at", nil], ["reset_password_token", nil], ["sign_in_count", 0], ["updated_at", Wed, 25 Sep 2013 06:31:10 UTC +00:00]]
   (2.9ms)  commit transaction
  Rendered landing/_sub_account_partial.html.erb (1.0ms)
  Rendered landing/create.js.erb (2.1ms)
Completed 500 Internal Server Error in 99ms

ActionView::Template::Error (undefined method `full_name' for nil:NilClass):
    1: <li><%= current_sub_account.full_name %></li>
  app/views/landing/_sub_account_partial.html.erb:1:in `_app_views_landing__sub_account_partial_html_erb___717255238604345729_70329913890480'
  app/views/landing/create.js.erb:1:in `_app_views_landing_create_js_erb___1846104658260022742_70329904266920'
  app/controllers/registrations_controller.rb:10:in `block (2 levels) in create'
  app/controllers/registrations_controller.rb:6:in `create'


  Rendered /Users/mikeland/.rvm/gems/ruby-1.9.3-p362@rails32/gems/actionpack-3.2.12/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.3ms)
  Rendered /Users/mikeland/.rvm/gems/ruby-1.9.3-p362@rails32/gems/actionpack-3.2.12/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.1ms)
  Rendered /Users/mikeland/.rvm/gems/ruby-1.9.3-p362@rails32/gems/actionpack-3.2.12/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (9.0ms)