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