简体   繁体   English

如何渲染模态局部?

[英]How to render a modal partial?

I have a list of items that can be deleted or edited. 我有一个可以删除或编辑的项目列表。 I want to implement it so that when a user presses edit, a modal would pop up with the item's information already prefilled. 我要实现它,以便当用户按下edit时,将弹出一个模态,其中已预先填充了该项目的信息。 I can't seem to get it to work. 我似乎无法正常工作。

_items.haml _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 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 update.js.erb

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

_edit_item.haml _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"

很简单,您使用的是@item变量,而不是您想在局部变量上使用的局部变量, @feature变量也是@feature

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM