1

我已经被这个问题困扰了几个小时,我希望有人能帮我弄清楚为什么这段代码不起作用。

我要做的是创建一个与特定用户关联的帐户。我可以创建一个用户,但是当我去创建帐户时,它看起来不像表单正在发布到我的控制器中的 create 方法(表单数据数组显示附加到我的浏览器栏中的帐户/新) .

这是我的模型:

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)
4

2 回答 2

1

对不起,我首先误解了这个问题。但是,您似乎想将现有user对象与新的account. 这是一个应该对您有所帮助的链接。

https://stackoverflow.com/a/11335976/1160106

大问题陈述,但清晰而简短的答案。让我知道它是否对您有帮助。

于 2012-10-04T17:14:06.627 回答
0

希望这将有助于您在代码中构建嵌套实例。在帐户控制器中,

  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]
于 2012-10-04T15:24:53.283 回答