1

无论我做什么,我的朋友嵌套表单字段都不会显示..我相信正确设置了accepts_nested_attributes?..

    views/user_steps/show.html.erb

    <%= simple_form_for @user do |f| %>

    <%= f.input :city %>
    <%= f.input :address %>
    <%= f.input :zipcode %>
    <%= f.input :date_of_birth %>
    <%= f.input :gender, :collection => ['male','female'] %>

    <%= f.association :interests, :as => :check_boxes, :label => false %>


    <%= f.association :holidays, :as => :check_boxes, :label => false %>


    <%= f.simple_fields_for :friends do |friend_f| %>
    <%= friend_f.input :name %>
    <%= friend_f.input :dob %>
    <%= friend_f.input :gender %>
     <% end %>

    <%= f.button :submit %>
    <%end%>


class UserStepsController < ApplicationController

  def show
    @user = current_user 
  end

  def update
    @user = current_user
    @user.attributes = params[:user]
    @friend = Friend.new(params[:friend])
  end

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


end

class UsersController < ApplicationController
  before_filter :authenticate_user!

  def index
    authorize! :index, @user, :message => 'Not authorized as an administrator.'
    @users = User.paginate(:page => params[:page])
  end

  def show
    @user = User.find(params[:id])
  end
  def create
    @user = User.new(params[:user])

    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'Friend birthday(1) was successfully created.' }
        format.json { render json: @user, status: :created, location: @user }
      else
        format.html { render action: "new" }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    @user = User.find(params[:id])

     respond_to do |format|
      if @user.update_attributes(params[:user])
        format.html { redirect_to @user, notice: 'User was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /users/1
  # DELETE /users/1.json
  def destroy
    @user = User.find(params[:id])
    @user.destroy

    respond_to do |format|
      format.html { redirect_to users_url }
      format.json { head :no_content }
    end
  end
end

class User < ActiveRecord::Base
  attr_accessible :name, :email, :password, :password_confirmation, :interests_attributes, :remember_me, :city, :zipcode, :date_of_birth, :gender, :address, :interest_ids, :holiday_ids
  has_and_belongs_to_many :holidays
  has_and_belongs_to_many :interests
  has_many :friends
  accepts_nested_attributes_for :friends,  allow_destroy: true
  accepts_nested_attributes_for :interests, allow_destroy: true
  accepts_nested_attributes_for :holidays, allow_destroy: true

class Friend < ActiveRecord::Base
  belongs_to :user
  attr_accessible :dob, :gender, :name
end
4

2 回答 2

8

我的猜测是@user没有朋友,所以没有什么可以渲染的。

如果这是创建一个新用户,并且您希望他们也能够填写他们的朋友,请做一个@user.friends.build地方,这将为他们添加一个空朋友,并且应该允许嵌套字段呈现。

于 2012-05-16T05:17:34.780 回答
0

我在网上多次找到这个答案。我正在使用 ruby​​-2.2.0 和 rails 4.2.0。我想知道这是否已经改变,或者它是否是我特有的。

我不得不使用。 @user.build_friends

于 2015-03-25T00:36:48.040 回答