ここに、基本的なフォームを備えた Rails アプリがあります。私の目標は、新しいユーザーをデータベースに追加し、jquery と ajax を使用して .each ブロックを更新するフォームを作成することです。
現在、データはデータベースに問題なく入力されていますが、先頭に追加すると、最新のエントリではなく 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>
/controllers/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)
前もって感謝します