如何渲染模态偏?

时间:2017-02-10 18:02:07

标签: javascript ruby-on-rails haml bootstrap-modal erb

我有一个可以删除或编辑的项目列表。我想实现它,以便当用户按下编辑时,会弹出一个模式,其中已经预先填充了项目的信息。我似乎无法让它发挥作用。

_items.haml

- @items.each do |item|
        .col-lg-2.col-md-3.col-sm-3.col-xs-12
            .partial.item
                .pad10
                    %label #{item.title}
                    = link_to item_path(item), :class => "btn-xs", :method => "put", remote: true do
                        edit
                    = link_to "x", item_path(item), :method => :delete, :data => {:confirm => "Are you sure?"}, :class => "btn-xs"
                    %h2.item-num
                        = "#{item.result}"

items_controller.rb

def update
    @item = Item.find(params[:id])
    @feature = @item.feature
    respond_to do |f|
        f.js { render layout: false }
        f.html
    end
end

update.js.erb

<%= j render(:partial => 'edit_item', locals: { item: @item, feature: @feature }) %>
$('#editItem').modal('show');

_edit_item.haml

#editItem.modal.fade
    .modal-dialog
        .modal-content
            .modal-header
                %button.close
                    x
                .modal-body
                    = form_for @item do |f|
                        .dev
                            .col-lg-7.col-xs-12
                                .well
                                    .row
                                        .col-md-9.col-xs-9
                                            %h4 Edit Item
                                        .col-md-3.col-xs-3
                                            .btn.form-control Cancel
                                    .row
                                        = f.hidden_field :feature_id, :value => @feature.id
                                        .col-md-6.col-xs-12
                                            = f.label :count
                                            = f.select(:count, ["Select"] + Item.counts(@feature), {}, { :class => "form-control input", :id => "column" })
                                        .col-md-3.col-xs-12
                                            = f.label :calculation
                                            = f.select(:calculation, ["Select"] + Item.calculations, {}, { :class => "form-control input", :id => "column" })
                                        .col-md-3.col-xs-12
                                            = f.label :format
                                            = f.select(:format, Item.formats, {}, { :class => "form-control input", :id => "column" })
                                        .col-md-12.col-xs-12
                                            = f.label :title
                                            = f.text_field :title, :class => "form-control input-sm", :value => "i.e. Total Billing"
                                    %br
                                    .row
                                        .col-md-12.col-xs-12
                                            = f.submit "Save", :class => "btn form-control btn-primary"

1 个答案:

答案 0 :(得分:0)

很简单,您使用的是@item变量而不是您想要在局部使用的本地变量,@feature变量也是如此。