我已经被这个问题困扰了几个小时了,我希望有人可以帮我弄清楚为什么这段代码无效。
我要做的是创建一个与特定用户关联的帐户。我可以创建一个用户,但是当我去创建帐户时,它看起来不像是在我的控制器中发布到create方法的表单(表单数据数组显示在我的浏览器栏中附加到accounts / new) 。
以下是我的模特:
class User < ActiveRecord::Base
attr_accessible :email_address, :password, :password_confirmation
has_secure_password
has_one :account, dependent: :destroy
before_save {|user| user.email_address = email_address.downcase}
我的帐户模型:
class Account < ActiveRecord::Base
attr_accessible :cell_phone, :first_name, :home_phone, :last_name, :middle_initial, :work_phone
belongs_to :user
has_many :addresses
has_many :orders
has_many :items
#strip digits and return string of numbers
before_save do |account|
account.cell_phone = cell_phone.gsub(/\D/,'')
account.home_phone = home_phone.gsub(/\D/,'')
account.work_phone = work_phone.gsub(/\D/,'')
end
before_save do |account|
account.first_name = first_name.capitalize
account.last_name = last_name.capitalize
account.middle_initial = middle_initial.capitalize
end
在我的用户控制器中(这可以正常工作):
def new
if signed_in?
@user = current_user
redirect_to @user
else
@user = User.new
end
end
def create
@user = User.new(params[:user])
if @user.save
sign_in @user
redirect_to new_account_path
else
flash[:error] = "Sorry, something went wrong"
render 'new'
end
end
我的账户控制员(我觉得问题出在这里):
class AccountsController < ApplicationController
#before_filter :get_current_user
before_filter :signed_in_user
def new
@user = current_user
@account = @user.build_account(params[:account])
end
def create
@user = current_user
@account = @user.build_account(params[:account])
if @account.save
flash[:success]
redirect_to user_path(current_user)
else
flash[:error]
redirect_to root_path
end
end
我将current_user和sign_in作为Helper文件中的方法。
最后,我的账户表格
<%= form_for(@account) do |form| %>
<%= render 'shared/user_form_error_messages', object: form.object %>
<%= form.label :first_name, "First Name" %>
<%= form.text_field :first_name %>
<%= form.label :middle_initial, "Middle Initial (optional)" %>
<%= form.text_field :middle_initial %>
<%= form.label :last_name, "Last Name" %>
<%= form.text_field :last_name %>
<%= form.label :home_phone, "Home Phone" %>
<%= form.text_field :home_phone %>
<%= form.label :cell_phone, "Cell Phone" %>
<%= form.text_field :cell_phone %>
<%= form.label :work_phone, "Work Phone" %>
<%= form.text_field :work_phone %>
<%= form.submit "Complete Account", class:"btn" %>
<% end %>
提前感谢您的帮助。我一直在谷歌搜索示例,阅读rdocs并尝试了很多不同的东西。如果有人能够解释为什么我的代码不起作用(而不仅仅是给我答案),我将非常感激。
在@thesis请求中,当我点击表单上的“提交”时,这是我日志的输出:
Started GET "/accounts/new? utf8=%E2%9C%93&authenticity_token=DQ5mOiICI0C83l9xgkSlvToQKyWjE3Adm3X3U0HJ1W4%3D&account%5Bfirst_name%5D=Jesse&account%5Bmiddle_initial%5D=A&account%5Blast_name%5D=Flores&account%5Bhome_phone%5D=555-555-5555&account%5Bcell_phone%5D=&account%5Bwork_phone%5D=&commit=Complete+Account" for 127.0.0.1 at 2012-10-04 16:14:11 -0400
Connecting to database specified by database.yml
Processing by AccountsController#new as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"DQ5mOiICI0C83l9xgkSlvToQKyWjE3Adm3X3U0HJ1W4=", "account"=>{"first_name"=>"Jesse", "middle_initial"=>"A", "last_name"=>"Flores", "home_phone"=>"555-555-5555", "cell_phone"=>"", "work_phone"=>""}, "commit"=>"Complete Account"}
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'B_E1bsUASQhHC-Zb-6updg' LIMIT 1
Account Load (0.6ms) SELECT "accounts".* FROM "accounts" WHERE "accounts"."user_id" = 2 LIMIT 1
(0.1ms) begin transaction
(0.0ms) commit transaction
Rendered shared/_user_form_error_messages.html.erb (0.5ms)
Rendered forms/_form_account.html.erb (5.9ms)
Rendered accounts/new.html.erb within layouts/application (13.2ms)
Rendered layouts/_navigation.html.erb (0.4ms)
Rendered forms/_form_header_signin.html.erb (0.8ms)
Rendered layouts/_header.html.erb (2.7ms)
Rendered layouts/_messages.html.erb (0.5ms)
Rendered layouts/_footer.html.erb (0.3ms)
Completed 200 OK in 279ms (Views: 160.1ms | ActiveRecord: 4.6ms)
答案 0 :(得分:1)
抱歉,我先误解了这个问题。但是,您似乎希望将现有user
对象与新account
相关联。这是一个可以帮助你的链接。
<强> https://stackoverflow.com/a/11335976/1160106 强>
大问题陈述但清晰简洁的答案。如果它对你有帮助,请告诉我。
答案 1 :(得分:0)
希望这可以帮助您在代码中构建嵌套实例。 在accountscontroller中,
def new
@user = current_user
@account = @user.accounts.build(params[:account])
end
def create
@user = current_user
@account = @user.accounts.build(params[:account])
if @account.save
flash[:success]