nil的未定义方法`build':NilClass rails

时间:2015-03-18 18:10:51

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

我认为正在进行的是:我的视图中的:承诺属性(:数量和:频率)没有传递给控制器​​中的新动作。另外,我知道我的承诺属性不是保存,也许是因为在调用@user时pledge_id为nil。

new.html.erb

<%= form_for @user, url: pledge_path, controller: 'user', action:     'create', method: 'post' do |f| %>
  <%= f.label :user_name %>
  <%= f.text_area :user_name %>
  <%= f.label :email %>
  <%= f.text_area :email%>
  <%= f.label :address %>
  <%= f.text_area :address %>
  <%= f.fields_for(:pledge) do |b| %>
    <%= b.label :amount %>
    <%= b.text_area :amount %>
    <%= b.label :frequency %>
    <%= b.text_area :frequency %>
    <%= b.submit 'I also want o pledge' %>
  <% end %>
<%= f.submit 'I just want to sumbit my info' %>

<% end %>

pledge.rb

class Pledge < ActiveRecord::Base
  belongs_to :user
  validates_presence_of :user

end

user.rb

class User < ActiveRecord::Base
  has_many :pledges, foreign_key: "pledge_id"
   accepts_nested_attributes_for :pledges
   attr_accessor :pledges

end

schema.rb

ActiveRecord::Schema.define(version: 20150318161827) do

create_table "pledges", force: :cascade do |t|
  t.integer "frequency"
  t.float   "amount"
  t.integer "user_id"
end

add_index "pledges", ["user_id"], name: "index_pledges_on_user_id"

create_table "users", force: :cascade do |t|
  t.string  "user_name"
  t.string  "email"
  t.string  "address"
  t.integer "pledge_id"
end

end

user_controller.rb

class UserController < ApplicationController
def new
    @user = User.new
    @user.pledges.build 
end

def create
    if params[:commit] == 'I just want to sumbit my info'
        User.create(user_params)

    elsif
        @pledge = Pledge.new(params[:pledge])
        @pledge.save
        @user = User.create(pledge_params)  
    end
    redirect_to thank_you_path

end
def pledge_params
    params.require(:user).permit(:email, :user_name, :address,
                                pledge_attributes: [:id, :amount, :frequency])
end

def user_params
    params.require(:user).permit(:email, :user_name, :address)
end
end

提前感谢任何建议!

2 个答案:

答案 0 :(得分:13)

来自here的信息对我来说很有帮助,就像你的情况一样。

因此,在Sontya的代码中,您需要将@user.pledges.build更改为@user.build_pledges

虽然,我认为你解决了你的问题,但我这样做是为了帮助那些搜索这些信息的人。

答案 1 :(得分:1)

你不能打电话

@user.pledges.build

如下所示,只会创建User的实例,但不会将其保存到数据库中。

@user = User.new

2)为什么你在用户表中有pledge_id,因为用户has_many认捐,你不能在用户表中存储一个pledge_id 3)只应有一个submit按钮,formpledges值将作为nested_attributes

发送

修改

class Pledge < ActiveRecord::Base
  belongs_to :user
  validates_presence_of :user

end

class User < ActiveRecord::Base
   has_many :pledges
   accepts_nested_attributes_for :pledges
   attr_accessor :pledges
end

控制器,现在这应该可行

def new
   @user = User.new
   @user.pledges.build
end

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

def user_params
  params.require(:user).permit(:name, etc..., pledges_attributes:[:amount, etc..])
end    

<强> new.erb.html

<%= form_for @user do |f| %>
  <% if @user.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
      <ul>
      <% @user.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :email %><br>
    <%= f.text_field :email %>
  </div>
  <%= f.fields_for :pledge do |builder| %>
     <%= builder.label :amount %>
     <%= builder.text_field :amount %>
  <% end %>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>