简体   繁体   中英

Rails: How do I re-render a partial view on a page?

I have a webpage with a partial view that has a button and displays some information. When the user clicks the button in this partial view, I need to be able to update that partial view from the controller within the action that gets called. Specifically, there is a string that I want to update. Here is my code so far, but it's not working..

partial view (_printLabel.rhtml)

<% if @printAsinEnabled %>
           <%= check_box_tag(:needPrint, :needPrint, false,
             :onchange => "if (this.checked) $('printLabelAction').show(); else $('printLabelAction').hide();") %>
             <%=_('Need to print label?')%><br /><br />
             <div id="printLabelAction" class="action" style="display:none;">
               <% onPrintLabelClick = generateAjaxRequest(
                   {:controller => @controller.controller_name,
                    :action => 'processPrintLabelRequest',
                    :creturnWipItemId => creturnWipItemId,
                    :barcode => collectedItemData.fnsku,
                    :title => asinInfo["title"]},
                   ["parameters: Form.serializeElements([$('printerAddress'),$('printerRemote_true'),$('printerRemote_false')])"]) %>
               <%=_('Printing')%>: <%= collectedItemData.fnsku %> <br /> <br />
             <%= radio_button_tag(:printerRemote, :false, !session[:printRemote]) %>
             <label for="printerRemote_false"><%=_('Local Printer')%></label> <br />
             <%= radio_button_tag(:printerRemote, :true, session[:printRemote]) %>
             <label for="printerRemote_true"><%=_('Remote Printer')%> - <%=_('Printer Address')%>:</label>
             <%= text_field_tag(:printerAddress, session[:printerAddress],
               :onchange => onPrintLabelClick,
               :onclick => "$('printerRemote_true').checked = true")%> <br />
             <div style="text-align:right;">
               <%= button_to_function(_("Print Label"), onPrintLabelClick) %>
             </div>
             <%=_('Error?')%>: <%= errorMessage %> <br /> <br />
           </div>

Here is the div where it gets rendered originally:

<div id="Block_asinSummary" class="block">
...
...
   <td height="200px" width ="70%">
     <%= link_to asinInfo["title"], asinInfo["link"], :target => "_blank" %><br/><br/>
     <% if !asinInfo["clothingSize"].nil? %>
       <b><%= _('Size')%>:&nbsp</b> <%= asinInfo["clothingSize"]%><br>
     <% end %>
     <font size='1'>
        <% sku = collectedItemData.displaySku %>
        <% if sku != collectedItemData.asin%>
          <b>SKU:</b> <%= sku %><br>
        <% else %>
          <b>ASIN:</b> <%= collectedItemData.asin %>
        <% end %>

        <%= render(:partial => 'printLabel', :locals => { :creturnWipItemId => creturnWipItemId, :collectedItemData => collectedItemData, :asinInfo => asinInfo, :errorMessage => "HI" })%>
     </font>         
   </td>
  </tr>
</table>
...
...
<% end %>

And here is the controller code that is supposed to update "errorMessage" with a new string when clicked:

render(:partial => 'printLabel', :layout => false, :locals => { :creturnWipItemId => creturnWipItemId, :collectedItemData => @collectedItemData, :asinInfo => asinInfo, :errorMessage => "Bye" })

I am not seeing any errors from this, but the errorMessage string is not updating from "HI" to "Bye".

You can't call render partial from your controller. Instead, render the template and pass a different error message to the partial using an instance variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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