带有设计注册表格

时间:2017-07-02 08:39:17

标签: ruby-on-rails ruby ruby-on-rails-4

我试图在设计注册/注册中添加一些嵌套字段。我有一个商店模型(它是用户模型)和一个帐户模型。我为商店和帐户创建了控制器(使用rails生成设计:控制器)和视图(使用rails生成设计:视图)。我正在尝试从商店注册/注册中的帐户模型传递一些嵌套字段,以便注册用户并创建帐户。商店确实注册但不创建帐户。

这是我正在尝试做的事情:

的routes.rb

Rails.application.routes.draw do
devise_for :stores, :controllers => { registrations: "stores/registrations",
                                    sessions: "stores/sessions" }

devise_scope :store do
get "stores/index"=> "stores/sessions#index", :as => "store_index"
get "stores/sign_up"=> "stores/registrations#new", :as => "store_sign_up"
get "stores/sign_in"=> "stores/sesssions#new", :as => "store_sign_in"
get "stores/sing_out"=> "stores/sesssions#destroy", :as => "store_sing_out"
end

store.rb

class Store < ApplicationRecord
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

  has_one :account, :autosave => true

  accepts_nested_attributes_for :account
end

account.rb

class Account < ApplicationRecord
  belongs_to :store
end

存储/ registration_controller.rb

class Stores::RegistrationsController < Devise::RegistrationsController
  before_action :configure_permitted_parameters, if: :devise_controller?
  skip_before_action :verify_authenticity_token, :only => :create

   def new
     build_resource({})
     resource.build_account
     respond_with self.resource
    end


   def create
     super
   end


   def edit
     super
    end


   def update
     super
   end


   def destroy
     super
   end

   def cancel
     super
   end

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up) { |u|
      u.permit(:email, :password, :password_confirmation, :remember_me,
           :account_attributes => [:first_name, :last_name, :buisness_name,
                                   :buisness_description, :web_site, :phone_number,
                                   :street, :city, :zip_code, :country])
}
  end


  # The path used after sign up.
  def after_sign_up_path_for(resource)
    items_path(resource)
   end

  end

应用程序/视图/存储/注册/ new.html.erb

<h2>Sign up</h2>

<% resource.build_account if resource.account.nil? %>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true %>
  </div>

  <div class="field">
   <%= f.label :password %>
   <% if @minimum_password_length %>
   <em>(<%= @minimum_password_length %> characters minimum)</em>
   <% end %><br />
   <%= f.password_field :password, autocomplete: "off" %>
   </div>

   <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "off" %>
   </div>


<%= fields_for :account do |form| %>
  <div class="field">
    <%= form.label :first_name %>
    <%= form.text_field :first_name, id: :account_first_name %>
  </div>

  <div class="field">
    <%= form.label :last_name %>
    <%= form.text_field :last_name, id: :account_last_name %>
  </div>

  <div class="field">
    <%= form.label :buisness_name %>
    <%= form.text_field :buisness_name, id: :account_buisness_name %>
  </div>

   <div class="field">
    <%= form.label :buisness_description %>
    <%= form.text_field :buisness_description, id: :account_buisness_description %>
   </div>

  <div class="field">
    <%= form.label :web_site %>
    <%= form.text_field :web_site, id: :account_web_site %>
  </div>

  <div class="field">
    <%= form.label :phone_number %>
    <%= form.text_field :phone_number, id: :account_phone_number %>
  </div>

  <div class="field">
   <%= form.label :street %>
   <%= form.text_field :street, id: :account_street %>
  </div>

  <div class="field">
    <%= form.label :city %>
    <%= form.text_field :city, id: :account_city %>
  </div>

  <div class="field">
    <%= form.label :state %>
    <%= form.text_field :state, id: :account_state %>
  </div>

  <div class="field">
   <%= form.label :zip_code %>
   <%= form.text_field :zip_code, id: :account_zip_code %>
  </div>

  <div class="field">
   <%= form.label :country %>
   <%= form.text_field :country, id: :account_country %>
  </div>
  <% end %>

  <div class="actions">
    <%= f.submit "Sign up" %>
  </div>
<% end %>

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

并且没有创建帐户......这就是我得到的

Started POST "/stores" for 127.0.0.1 at 2017-07-02 11:34:25 +0300
Processing by Stores::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓",  "authenticity_token"=>"ltsFRnWTbbghe/Hve+gqpJ5SVmpqB8bzgNo9PnEUc2DYE0hQLWRb 2JgAoUtSex2QlmynColjljxWOHQYxhWzLA==", "store"=> {"email"=>"test9@gmail.com", "password"=>"[FILTERED]",  "password_confirmation"=>"[FILTERED]"}, "account"=>{"first_name"=>"1", "last_name"=>"1", "buisness_name"=>"1", "buisness_description"=>"1", "web_site"=>"1", "phone_number"=>"1", "street"=>"1", "city"=>"1", "state"=>"1", "zip_code"=>"1", "country"=>"1"}, "commit"=>"Sign up"}
   (0.1ms)  begin transaction
  Store Exists (0.2ms)  SELECT  1 AS one FROM "stores" WHERE "stores"."email" = ? LIMIT ?  [["email", "test9@gmail.com"], ["LIMIT", 1]]
  SQL (0.5ms)  INSERT INTO "stores" ("email", "encrypted_password", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["email", "test9@gmail.com"], ["encrypted_password", "$2a$11$K2bdPn80L47p8tFdMrvj5e8J.0SFPB5QgwLu8I6OYDV5j08BrSshy"], ["created_at", "2017-07-02 08:34:25.447647"], ["updated_at", "2017-07-02 08:34:25.447647"]]
   (1.5ms)  commit transaction
   (0.2ms)  begin transaction
  SQL (0.3ms)  UPDATE "stores" SET "sign_in_count" = ?, "current_sign_in_at" = ?, "last_sign_in_at" = ?, "current_sign_in_ip" = ?, "last_sign_in_ip" = ?, "updated_at" = ? WHERE "stores"."id" = ?  [["sign_in_count", 1], ["current_sign_in_at", "2017-07-02 08:34:25.452166"], ["last_sign_in_at", "2017-07-02 08:34:25.452166"], ["current_sign_in_ip", "127.0.0.1"], ["last_sign_in_ip", "127.0.0.1"], ["updated_at", "2017-07-02 08:34:25.453031"], ["id", 9]]
   (0.6ms)  commit transaction
Redirected to http://localhost:3000/items.9
Completed 302 Found in 168ms (ActiveRecord: 3.4ms)

1 个答案:

答案 0 :(得分:0)

罪魁祸首是fields_for。您应该使用form_for实例(在您的情况下为f包装它。因此<%= fields_for :account do |form| %>应为<%= f.fields_for :account do |form| %>