4

我是 Rails 新手,尚未设法解决以下问题。我使用名为“用户”的模型进行身份验证。此模型与另一个名为“company”的模型相关联:

用户.rb

  class User < ActiveRecord::Base

  devise :database_authenticatable, :registerable, :confirmable,
         :recoverable, :rememberable, :trackable, :validatable

  belongs_to :company
  accepts_nested_attributes_for :company

  attr_accessible :company_attributes, :email, :password, 
                  :password_confirmation, :remember_me, :company_id

公司.rb

  class Company < ActiveRecord::Base

  has_many :users
  attr_accessible :name
  validates :name, :presence => true, :uniqueness => true

注册时,用户同时创建公司和用户:

users_controller.rb

def new
    @user = User.new
    @user.build_company

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @user }
    end
end

def create
  @user = User.new(params[:user])

  respond_to do |format|
    if @user.save
      format.html { redirect_to @user, notice: 'User was successfully created.' }
      format.json { render json: @user, status: :created, location: @user 
    else
      format.html { render action: 'new' }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end

到目前为止,注册工作正常,这意味着如果@user.save为真,则在用户模型和公司模型中分别创建两条记录,并在用户模型的列中具有正确的关联company_id

如果@user.save为 false,例如因为用户属性 email 的验证失败,因为它已经被采用,则呈现一个新视图。正如预期的那样,@user属性(在本例中为电子邮件)被保留并保留在 之后的相应表单字段中render action: 'new'。但是,公司属性名称的表单字段显示为空白。这是注册页面的视图:

新的.html.erb

<%= devise_error_messages! %>
<h2>Sign up</h2>
<%= simple_form_for(resource, :as => resource_name, 
                              :url => registration_path(resource_name),
                              :html => {:class => 'form-horizontal' }) do |f| %>

    <%= f.simple_fields_for :company,  @user.build_company do |builder| %>
        <div><%= builder.label :name, "Company" %><br />
        <%= builder.text_field :name, :autofocus => true, 
                            :placeholder => "Example Company Inc.",
                            :required => true %></div>
    <% end %>

  <div><%= f.label :email %><br />
  <%= f.email_field :email, :placeholder => "example@email.com",  :required => true %></div>

  <div><%= f.label :password %><br />
  <%= f.password_field :password,  :required => true %></div>

  <div><%= f.label :password_confirmation %><br />
  <%= f.password_field :password_confirmation,  :required => true %></div>

  <%= f.submit "Sign up", :class => "btn"%>
<% end %>

<%= render "devise/shared/links" %>

我认为我可能必须@company在用户创建方法中创建一个实例变量,以便可以将表单字段内容保存在 params 哈希中,如下所示:

@company = @user.build_company(params[:company])

不幸的是,这不起作用,到目前为止,我还没有在 stackoverflow 上发布的其他问题中找到解决方案。我期待您的意见,以便我可以继续构建我的第一个应用程序;)

在此先感谢您的帮助。

4

1 回答 1

0

多亏了 Michal 的意见,我重新安排了我的控制器,以便通过关联公司创建用户,而不是相反。相应的公司控制器如下所示:

公司控制器.rb

# GET company/new
def new
    @company = Company.new
    @company.users.build
end

# POST company/
def create
    @company = Company.new(params[:company])

    respond_to do |format|
      if @company.save
        format.html { redirect_to root_path, notice: 'Welcome! Your account has been created successfully. A confirmation link has been sent to your email address.' }
        format.json { render json:  root_path, status: :created, location: @company }
      else
        format.html { render action: "new" }
        format.json { render json: @company.errors, status: :unprocessable_entity }
      end
    end
end

GET 请求的表单嵌套了 Devise User 模型字段,如下所示:

新的.html.erb

<h2>Create an account</h2>
<%= simple_form_for( @company, :html => {:class => 'form-horizontal' }) do |f| %>
<%= render 'shared/error_messages', :object => @company %>
    <%= f.input     :name, 
                    :label =>'Company name',
                    :placeholder => "Example Company Inc.",
                    :autofocus => true %>
    <%= f.simple_fields_for :users do |builder| %>
        <%= builder.input   :email, 
                            :placeholder => "example@email.com" %>
        <%= builder.input :password %>
        <%= builder.input :password_confirmation %>
    <% end %>
    <%= f.button    :submit, 'Create account',
                    :class => 'btn'%>
<% end %>
<%= link_to "Sign in", new_session_path(:user) %>

现在,如果验证失败,所有实例属性值都保留在各自的表单字段中。

于 2013-06-15T19:39:10.340 回答