Rails正在打印一个意外的标签

时间:2016-03-30 02:41:17

标签: javascript ruby-on-rails haml erb

考虑一种含有各种成分的果汁对象,即果汁形式(/juices/_form.haml

= form_for @juice do |juice_f|
  %fieldset
    %legend
      Juice Data

    .field
      = juice_f.label "Codename"
      = juice_f.text_field :codename

    .field
      = juice_f.label "Capacity"
      = juice_f.text_field :capacity

  %fieldset
    %legend
      Ingredients

  .field
    = link_to "Add", new_juice_ingredient_path(form: {object_id: juice_f.object_id}), remote: true, class: "new_juice_ingredient"

    - if ingredients = @juice.juice_ingredients
      - ingredients.each do |ingredient|
        - @juice_ingredient = ingredient
        = render partial: "ingredient_fields", member: @juice_ingredient, locals: {form_parent: juice_f}

  .field
    = juice_f.submit "Save"

当我调用url进行编辑时,渲染它没问题,但是为了创建新的果汁,当我添加新字段时,</div>会在链接remove之后打印。

部分_ingredient_fields.erb

<div class="field new">
  <%= javascript_include_tag 'remove_field' %>

  <%= form_parent.fields_for :juice_ingredients, @juice_ingredient do |juice_ingredient_f| %>

    <div class="inline">
      <%= juice_ingredient_f.label "Ingredient" %>
      <%= juice_ingredient_f.select(:ingredient_id, options_for_select( ingredients.map{ |s| [s.name, s.id] }, selected: juice_ingredient_f.object.try(:ingredient_id))) %>
    </div>

    <div class="inline">
      <%= juice_ingredient_f.label "Amount" %>
      <%= juice_ingredient_f.text_field :amount %>
    </div>

    <div class="inline">
      <%= action_for_remove "Remover", juice_ingredient_f, class: "remove-field" %>
    </div>

  <% end %>
</div>

controller

class JuicesController < ApplicationController
  respond_to :html, :js

  def new_ingredient
    respond_with @juice_ingredient = juice.juice_ingredients.build, layout: false
  end

  def new
    respond_with @juice = juice
  end

  def create
    respond_with Juice.create(juice_attrs), location: juices_path
  end

  def show
    respond_with @juice = juice
  end

  def edit
    respond_with @juice = juice
  end

  def update
    respond_with juice.update(juice_attrs), location: juices_path
    # , notice: "Lote atualizado com sucesso"
  end

  def destroy
    respond_with Juice.destroy(id), location: juices_path
  end

  def index
    respond_with @juices = Juice.all
  end

  def form_parent
    ObjectSpace._id2ref(params[:form][:object_id].to_i) if params[:form][:object_id]
  end

  def ingredients
    Ingredient.all
  end
  helper_method :ingredients, :form_parent

  private

  def id
    params[:id]
  end

  def instance
    @instance ||= Juice.new
  end

  def juice
    id ? Juice.find(id) : instance
  end

  def juice_attrs
    params.require(:juice)
      .permit(:codename, :capacity, :capacity_unit, juice_ingredients_attributes: [:id, :ingredient_id, :amount, :_destroy])
  end

end

js呈现部分_ingredient_fields.js.erb

$('a.new_juice_ingredient').closest(".field").after("<%= escape_javascript(render 'ingredient_fields') %>");

model Juice

class Juice < ActiveRecord::Base

  has_many :juice_ingredients
  has_many :ingredients, :through => :juice_ingredients
  accepts_nested_attributes_for :juice_ingredients, allow_destroy: true

end

form_helper.rb

module FormHelper

  def action_for_remove label, f_obj, opts
    if f_obj.object.id
      concat f_obj.label label
      f_obj.check_box :_destroy
    else
      link_to label, "javascript:void(0)", opts
    end
  end

end

0 个答案:

没有答案