【问题标题】:Rails 3.2 Nested Forms Not showingRails 3.2嵌套表单未显示
【发布时间】:2012-05-16 05:14:49
【问题描述】:

无论我做什么,我的朋友嵌套表单字段都不会显示..我相信正确设置了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

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 associations nested-attributes simple-form


    【解决方案1】:

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

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

    【讨论】:

    • 天哪,非常感谢你.. 已经为此辛勤工作了几个小时。非常感谢!
    • np。我被这件事咬了很多次,我发誓再也不会发生这种事了。
    【解决方案2】:

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

    我不得不使用。 @user.build_friends

    【讨论】:

      猜你喜欢
      • 2013-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多