Rails 4:设计不保存嵌套属性

时间:2013-12-07 09:19:33

标签: devise ruby-on-rails-4 nested-attributes one-to-one

人们请帮助我

我正在使用宝石设计。我有模型用户和地址。


schema.rb

create_table "addresses", force: true do |t|
 t.integer  "index"
 t.string   "telephone"
 t.string   "city"
 t.datetime "created_at"
 t.datetime "updated_at"
 t.integer  "user_id"
end

create_table "users", force: true do |t|
  t.string   "first_name"
  t.string   "last_name"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.string   "email",                  default: "", null: false
  t.string   "encrypted_password",     default: "", null: false
  t.string   "reset_password_token"
  t.datetime "reset_password_sent_at"
  t.datetime "remember_created_at"
  t.integer  "sign_in_count",          default: 0,  null: false
  t.datetime "current_sign_in_at"
  t.datetime "last_sign_in_at"
  t.string   "current_sign_in_ip"
  t.string   "last_sign_in_ip"
end

user.rb

class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable, :omniauthable

validates_presence_of :first_name, :last_name, :email, :password
validates_uniqueness_of :email

has_one :address, :inverse_of => :user, :autosave => true

Address.rb

class Address < ActiveRecord::Base
belongs_to :user
 end

users_controller.rb

class UsersController < ApplicationController
before_filter :authenticate_user!, :only => [:profile]
  def profile
   end

application_Controller.rb

class ApplicationController < ActionController::Base

  protect_from_forgery with: :exception

  before_filter :configure_permitted_parameters, if: :devise_controller?
  def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) do |u|
      u.permit(:first_name, :last_name,  :gender, :email, :password,           :password_confirmation,
address_attributes: [:telephone, :city, :index]
      )
      end
      end
   end

route.rb

Foo::Application.routes.draw do
  root to: "users#profile"
    devise_for :users

/app/views/devise/registrations/new.html.haml

= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do          |f|
 = devise_error_messages!
 %h1 Registration
.form-group
  = label_tag 'first_name', nil, class: 'small_label'
  = f.text_field :first_name,  class: "form-control"
.form-group
  = label_tag 'last_name', nil, class: 'small_label'
  = f.text_field :last_name,  class: "form-control"
.form-group#email-validate
  #email_massage_error.text-danger
  = label_tag 'email', nil, class: 'small_label'
  = f.text_field :email,  class: "form-control"
  %span
.form-group
  = label_tag 'password', nil, class: 'small_label'
  = f.password_field :password,  class: "form-control"
.form-group
  = label_tag 'password confirmation', nil, class: 'small_label'
  = f.password_field :password_confirmation,  class: "form-control"
%h3 Address:
.form-group
  = f.fields_for :address do |a|
  = label_tag 'telephone', nil, class: 'small_label'
  = a.text_field :telephone,  class: "form-control"
  = label_tag 'city', nil, class: 'small_label'
  = a.text_field :city,  class: "form-control"
  = label_tag 'zip code', nil, class: 'small_label'
  = a.text_field :index,  class: "form-control"
.form-group
= button_tag :Registration, :class => "btn btn-success"

当提交来自时,地址fild不保存。

1 个答案:

答案 0 :(得分:1)

在用户模型中添加以下行:

accepts_nested_attributes_for :addresses,  :allow_destroy => true

Rails具有自动复数,可能无法正确处理“地址”一词,因此您可能还想尝试:

accepts_nested_attributes_for :addresss,  :allow_destroy => true