0

我在这里有一个 Rails 应用程序,有一个基本形式。我的目标是有一个表单,它将向数据库添加一个新用户,然后使用 jquery 和 ajax 更新 .each 块。

目前,数据可以很好地输入到数据库中,但是 prepend 会导致 nil 错误,而不是最新的条目。显然,变量在某个时候失去了它的价值。

这是我的代码:

/controllers/registrations_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

/landing/_sub_account_partial.html.erb

<li><%= current_sub_account %></li>

/landing/create.js.erb

$("ul").prepend("<%= escape_javascript(render 'landing/sub_account_partial', :current_sub_account => @current_sub_account ) %>");
  $('#user_email').val('');

/landing/signup_page_2.html.erb

 <%= 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>

/控制器/landing_controller.rb

...
    @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)

先感谢您

4

1 回答 1

1

您尚未在创建操作中定义 @current_sub_account ,因此部分正在传递一个 nil 实例变量。

你需要像@current_sub_account = WHATEVER IS YOUR SUB ACCOUNT OBJECT在创建动作中的东西

于 2013-09-25T07:36:12.790 回答