【问题标题】:Rails/Facebox - update with remote functionRails/Facebox - 使用远程功能更新
【发布时间】:2012-08-05 10:29:02
【问题描述】:

已搜索此问题,但未能找到解决方案。

我有一个表单,其中的元素被部分替换,具体取决于表单中先前元素的选择(例如 - 一旦用户选择了“来源”,表单将使用任何可用的目的地更新“目的地”collection_select从选定的原点)。 这很好用.. 除了我希望表单以模态方式显示在 Facebox 弹出窗口中。

目前,facebox 弹出窗口可以正确加载表单,但表单元素上的 onchange 操作似乎没有更新...

查看中:
<%= link_to "New Delivery", new_delivery_requests_path, :remote => true %>

远程调用(views/delivery_requests/new.js.erb):
$.facebox('<%= escape_javascript(render 'new') %>')

新布局(views/delivery_requests/new.html.erb):

<%= form_for @price_update do |f| %>
    <% if @price_update.errors.any? %>
    <h2>Errors:</h2>
    <ul>
        <% @price_update.errors.full_messages.each do |message|%>
        <li><%= message %> </li>
        <% end %>
    </ul>
    <% end %>
    <br />
    <table>
        <tr> 
            <td><%= f.label "Select origin location: "%> </td> 
            <td><%= collection_select(:route, :origin_id,  @origins,  :id, :name, 
            { :prompt   => "Please select an origin" },
            { :onchange => "#{remote_function(
                :url  => { :action => "update_destinations"}, 
                :with => "'origin_id='+value")}",
                :id => "origin_select"} )%>
            </td>
        </tr>
        <tr>
            <td><%= f.label "Select destination location: "%> </td>
            <td><div id="destinations">
                <%= collection_select(:route, :destination, 
                @available_routes, :id, :destination, 
                { :prompt   => "Please select an origin first" }, { :disabled => "disabled" })%></div></td>
        </tr>               
        <tr>
            <td><%= f.label "Current Prices: "%> </td>
            <td>
                <div id="current-prices-table"></div>
            </td>
        </tr>   
        <tr>
            <td><%= f.label "Please select price option" %></td>
            <td>
                <div id = "price-selection"></div>
            </td>
        </tr>
        <tr>
            <td><%= f.label "Priority" %></td>
            <td><%= f.label "Price Per Gram" %></td>
            <td><%= f.label "Price per cm3" %></td>
        </tr>
        <tr>
            <td><div id="priority-label"></div></td>
            <td><%= f.text_field :new_price_per_gram, :id => "price_per_gram_textfield" %></td>
            <td><%= f.text_field :new_price_per_vol, :id => "price_per_vol_textfield" %></td>
        </tr>
    </table>
    <br />
    <%= f.submit "Update Prices"%>  
<% end %>

delivery_requests 控制器中的更新目的地操作:

def update_destinations
    # updates the available list of destinations after an
    # origin location has been chosen when making a new delivery request
    sel_origin = Location.find(params[:origin_id])
    @available_routes = Route.find_available_routes(sel_origin.name)
    render :update do |page|
      page.replace_html 'destinations', :partial => 'destinations_select', :object => @destinations
    end
  end

我对 Rails 还很陌生 - 任何建议都会受到欢迎。

【问题讨论】:

    标签: jquery ruby-on-rails facebox


    【解决方案1】:

    今晚我自己一直在努力解决这个问题,终于想出了一个答案。您需要引用您打开的脸框。这就是你要让你的东西发挥作用的方法。

    def update_destinations
    # updates the available list of destinations after an
    # origin location has been chosen when making a new delivery request
    sel_origin = Location.find(params[:origin_id])
    @available_routes = Route.find_available_routes(sel_origin.name)
    render :update do |page|
        page << "jQuery('#facebox .content #destinations').replaceWith(#{(render :partial => 'destinations_select', :object => @destinations).to_json})"       
    end
    end
    

    我要感谢jQuery .on not working with dynamic DOM/HTML,它让我找到了https://github.com/ihower/facebox_render/blob/master/lib/facebox_render.rb,在那里我构建了答案。

    【讨论】:

    • 感谢您的回答。我认为我们当时采用了不同的方法,但希望这可以帮助其他人。干杯。
    猜你喜欢
    • 2016-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    相关资源
    最近更新 更多