设计嵌套用户注册表单

时间:2011-01-23 20:40:25

标签: ruby-on-rails forms devise

我正在为使用设备身份验证的用户创建嵌套注册表单而烦恼。表单正在创建一个新的所有者,但它不会创建任何新用户。

以下是我的模特:

User.rb

class User <  ActiveRecord::Base
  has_one :worker
  has_one :owner
end

Owner.rb

class Owner < ActiveRecord::Base
  belongs_to :user
  accepts_nested_attributes_for :user
  attr_accessible :name, :website, :user_attributes, :user
end

所有者控制器的一部分:

class OwnersController < ApplicationController

  def show
   @owner = Owner.find(params[:id])
  end

  def new
   @owner = Owner.new
  end

  def create
   @owner = Owner.create(params[:owner])
   if @owner.save
    flash[:notice] = "Owner created"
    redirect_to(:action => 'list')
   else
    render('new')
   end
  end

end

我的老板表格:

<%= link_to("<< Back to List", {:action => 'list'}, :class => 'back-link') %>

<div class="subject new">
  <h2>Create owner</h2>

  <%= form_for(:owner, :url => {:action => 'create'}, :html => {:multipart => true} ) do |f| %>
    </tr>
    <th>Name:</th>
    <td><%= f.text_field(:name) %></td>
    </tr>
    </tr>
    <th>Website:</th>
    <td><%= f.text_field(:website) %></td>
    </tr>
    <%= fields_for :user do |form| %>
      <%= form.text_field(:email) %>
      <%= form.password_field(:password) %>
      <%= form.password_field(:password_confirmation) %>
    <% end %>

    <div class="form-buttons">
      <%= submit_tag("Creat owner") %>
    </div>

  <% end %>
</div>

我的开发日志

Started POST "/owners" for XXXXXX at 2011-01-23 21:16:59 +0100
  Processing by OwnersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"gA07ajItgkADpZ/rF5NM73AncUDWgb0Q2QJjXvMCSFY=", "user"=>{"email"=>"yes@example.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "owner"=>{"name"=>"asdas", "website"=>"www.test.com", "commit"=>"Create owner"}
  [1m[36mSQL (0.0ms)[0m  [1mBEGIN[0m
  [1m[35mSQL (0.0ms)[0m  describe `owners`
  [1m[36mAREL (0.0ms)[0m  [1mINSERT INTO `owners` (`name`, `website`, `created_at`, `updated_at`, `user_id`) VALUES ('asdas', 'www.test.com','2011-01-23 20:17:00', '2011-01-23 20:17:00', NULL)[0m
  [1m[35mSQL (125.0ms)[0m  COMMIT
  [1m[36mSQL (0.0ms)[0m  [1mBEGIN[0m
  [1m[35mSQL (0.0ms)[0m  COMMIT
Redirected to http://localhost:3000/owners/list
Completed 302 Found in 344ms

我的新开发日志:

Started POST "/owners" for 127.0.0.1 at 2011-01-24 16:41:02 +0100
  Processing by OwnersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"0TcWhIoUt9Qwrst8Zrj5tycd8LZ1VWdQ98YUSBWb9f4=", "owner"=>{"name"=>"sdfsdrfwrf", "user"=>{"email"=>"sdfdf@example.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}, "commit"=>"Create owner"}
Completed   in 375ms

ActiveRecord::AssociationTypeMismatch (User(#18210216) expected, got ActiveSupport::HashWithIndifferentAccess(#9451968)):
  app/controllers/owners_controller.rb:22:in `create'

Rendered C:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (15.6ms)
Rendered C:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (2062.5ms)
Rendered C:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (2125.0ms)

1 个答案:

答案 0 :(得分:1)

似乎你需要

<%= f.fields_for :user do |form| %>

你有

的地方
<%= fields_for :user do |form| %>

(原始答案)

你的意思是

@owner = Owner.create(params[:owner])

create method? (Notice the lower case 'o' in @ owner`)