1

我嵌套表格字段的朋友不会显示不管我正确do..have的accepts_nested_attributes设置,我相信?..的Rails 3.2嵌套表格不显示

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 

回答

8

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

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

+0

神圣的垃圾非常感谢你..已经辛苦了几个小时。非常感谢! – js111

+0

np。我曾多次发誓,它永远不会再发生。 – x1a4

0

我在网上几次发现这个答案。我在rails 4.2.0中使用ruby-2.2.0。我想知道这是否已经改变,或者如果它是特定于我的东西。我不得不使用。 @user.build_friends