Cocoon Gem没有显示形式 - rails 4

时间:2016-03-04 11:58:56

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

我不确定为什么我的茧嵌套表格没有出现,任何帮助都会非常感激

  • <button><%= link_to_add_association 'add a required skill', f, :requiredskills %></button>可以很好地显示表单
  • 但我不确定为什么这不会呈现<%= render 'requiredskill_fields', :f => duty %>
  • 形式
  
      
  • 我想要做的是已经显示一个表单,当用户点击add a required skill时,会显示另一个表单。   目前,当用户点击显示的add a required skill时   另一种形式 - 主要问题是要显示一个表格   原来。我假设这个<%= render 'requiredskill_fields', :f => duty %>假设放置一个表格,但我不确定它为什么不起作用
  •   

用户/ _form.html.erb

<%= simple_form_for(@user) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :firstname %>
    <%= f.input :lastname %>

    <h3>required skills & expertise</h3>
    <div>
      <div class="requiredskill_info" id="skill">
        <%= f.simple_fields_for :requiredskills do |skill| %>
          <%= render 'requiredskill_fields', :f => skill %>
        <% end %>
        <div class="add_requiredskill"><button><%= link_to_add_association 'add a required skill', f, :requiredskills %></button></div>
      </div>
    </div>
  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

用户/ _requiredskill_fields.html.erb

<div class="nested-fields">
  <%= f.association :category_advert, collection: CategoryAdvert.all, prompt: "select a category" %>
  <%= f.grouped_collection_select :category_advertskill_id, CategoryAdvert.order(:name), :category_advertskills, :name, :id, :name, {prompt: "Select a category"}, {class: "category_advertskill"} %>
  <button><%= link_to_remove_association 'remove required skill', f %></button>
</div>

users_controller.rb

class UsersController < ApplicationController
  respond_to :html, :xml, :json
  before_action :set_user, only: [:show, :edit, :update, :destroy]

  def index
    @users = User.all
    @user = current_user
  end

  def show
    @resume = @user.resume
  end

  def edit
  end

  def update
    @user.update(user_params)
    redirect_to @user
  end

  private
    def set_user
      @user = User.find(params[:id])
    end

    def user_params
      params.require(:user).permit(:email, :firstname, :lastname, :city, :category_qualification_id, :category_careerlevel_id, :desiredjob, :category_distance_id, :preferedlocation, :category_notice_id, :category_country_id, :category_positiontype_id, :image, :cvattachment, :hidecv, :jobadvice, :validlicense, :owntransport, :considerrelocation, :preferredlocation, :neednotice, :stratdate, :availabletowork, :category_outsource_id, :category_advertskill_id, :category_age_id, languages_attributes: [:id, :name, :_destroy], requiredskills_attributes: [:id, :name, :category_advert_id, :category_advertskill_id, :category_year_id, :category_level_id, :_destroy])
    end
end

user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :requiredskills
  has_many :category_advertskills, through: :requiredskills
  has_many :category_adverts, through: :requiredskills

  has_one :resume

  accepts_nested_attributes_for :languages, :reject_if => :all_blank, :allow_destroy => true
  accepts_nested_attributes_for :requiredskills, :reject_if => :all_blank, :allow_destroy => true
end

1 个答案:

答案 0 :(得分:1)

def edit
  @requiredskills = @user.requiredskills.present? ? @user.requiredskills : @user.requiredskills.build
end

在控制器中更改此代码