嵌套属性未显示的问题?

时间:2014-05-27 02:32:50

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

尝试将avatar嵌入user,但是头像是否为零? @user.inspect不包含我打算拥有的image属性... @user.avater == nil

错误

First argument in form cannot contain nil or be empty
Extracted source (around line #6):

  <%= @user.inspect %>

  <%= form_for [@user, @user.avatar], url: {action: :create} do |f| %>
    <% User::USER_FIELDS.map do |field| %>
      <li>
        <%= f.label field %>

模型

class User < ActiveRecord::Base
  USER_FIELDS = ["first_name", "last_name", "email", "password"].freeze
  has_one :avatar
  accepts_nested_attributes_for :avatar
end

class Avatar < ActiveRecord::Base
  belongs_to :user
end

查看

<%= form_for [@user, @user.avatar], url: {action: :create} do |f| %>
  <% User::USER_FIELDS.map do |field| %>
    <li>
      <%= f.label field %>

    <% if field =~ /password/ %>
      <%= f.password_field field %>
    </li>

    <% else %>
      <%= f.text_field field %>
    </li>
    <% end %>
  <% end %>
    <%= f.fields_for :avatar do |a| %>
      <li>
        <%= a.label :image %>
        <%= a.text_field :image %>
      </li>
    <% end %>
  <%= f.submit "Create Your Account" %>
<% end %>

控制器

class UserController < ApplicationController
  def index
  end

  def new
    @user = User.new
  end

  def create
    user = User.new(require_parmas)

    if user.save
      puts "saved" #temp
    end
  end

  private
  def require_parmas
    params.require(:user).permit(User::USER_FIELDS, avatar: [:image])
  end
end

更新

2.1.0 :075 > u.build_avatar
ActiveRecord::UnknownAttributeError: unknown attribute: user_id
  from /Users/fab/.rvm/gems/ruby-2.1.0/gems/activerecord-4.1.1/lib/active_record/attribute_assignment.rb:50:in `rescue in _assign_attribute'
  from /Users/fab/.rvm/gems/ruby-2.1.0/gems/activerecord-4.1.1/lib/active_record/attribute_assignment.rb:45:in `_assign_attribute'
  from /Users/fab/.rvm/gems/ruby-2.1.0/gems/activerecord-4.1.1/lib/active_record/attribute_assignment.rb:32:in `block in assign_attributes'
  from /Users/fab/.rvm/gems/ruby-2.1.0/gems/activerecord-4.1.1/lib/active_record/attribute_assignment.rb:26:in `each'
  from /Users/fab/.rvm/gems/ruby-2.1.0/gems/activerecord-4.1.1/lib/active_record/attribute_assignment.rb:26:in `assign_attributes'
  from /Users/fab/.rvm/gems/ruby-2.1.0/gems/activerecord-4.1.1/lib/active_record/associations/association.rb:169:in `initialize_attributes'
  from /Users/fab/.rvm/gems/ruby-2.1.0/gems/activerecord-4.1.1/lib/active_record/associations/association.rb:248:in `block in build_record'
  from /Users/fab/.rvm/gems/ruby-2.1.0/gems/activerecord-4.1.1/lib/active_record/core.rb:200:in `initialize'
  from /Users/fab/.rvm/gems/ruby-2.1.0/gems/activerecord-4.1.1/lib/active_record/inheritance.rb:30:in `new'
  from /Users/fab/.rvm/gems/ruby-2.1.0/gems/activerecord-4.1.1/lib/active_record/inheritance.rb:30:in `new'
  from /Users/fab/.rvm/gems/ruby-2.1.0/gems/activerecord-4.1.1/lib/active_record/reflection.rb:207:in `build_association'
  from /Users/fab/.rvm/gems/ruby-2.1.0/gems/activerecord-4.1.1/lib/active_record/associations/association.rb:247:in `build_record'
  from /Users/fab/.rvm/gems/ruby-2.1.0/gems/activerecord-4.1.1/lib/active_record/associations/singular_association.rb:29:in `build'
  from /Users/fab/.rvm/gems/ruby-2.1.0/gems/activerecord-4.1.1/lib/active_record/associations/builder/singular_association.rb:18:in `build_avatar'
  from (irb):75
  from /Users/fab/.rvm/gems/ruby-2.1.0/gems/railties-4.1.1/lib/rails/commands/console.rb:90:in `start'
  from /Users/fab/.rvm/gems/ruby-2.1.0/gems/railties-4.1.1/lib/rails/commands/console.rb:9:in `start'
  from /Users/fab/.rvm/gems/ruby-2.1.0/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:69:in `console'
  from /Users/fab/.rvm/gems/ruby-2.1.0/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
  from /Users/fab/.rvm/gems/ruby-2.1.0/gems/railties-4.1.1/lib/rails/commands.rb:17:in `<top (required)>'
  from /Users/fab/.rvm/gems/ruby-2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in `require'
  from /Users/fab/.rvm/gems/ruby-2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in `block in require'
  from /Users/fab/.rvm/gems/ruby-2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:232:in `load_dependency'
  from /Users/fab/.rvm/gems/ruby-2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in `require'
  from /Users/fab/Sites/ammoloadz/bin/rails:8:in `<top (required)>'
  from /Users/fab/.rvm/gems/ruby-2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in `load'
  from /Users/fab/.rvm/gems/ruby-2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in `block in load'
  from /Users/fab/.rvm/gems/ruby-2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:232:in `load_dependency'
  from /Users/fab/.rvm/gems/ruby-2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in `load'
  from /Users/fab/.rvm/rubies/ruby-2.1.0/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
  from /Users/fab/.rvm/rubies/ruby-2.1.0/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
  from -e:1:in `<main>'2.1.0 :076 > 

1 个答案:

答案 0 :(得分:2)

has_one的默认值为nil。 你必须建立一个:

def new
  @user = User.new
  @user.build_avatar
end