SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Having a problem updating partial.

    Hey all. After not touching rails for a while I am doing another project and I am stuck.

    I have a form that creates a user address. Once submitted it is suppose to refresh the div that lists all available addresses but it does not.


    I have this form...

    HTML Code:
    <div class="address-form">
    	<div align="center">
    		<span class="larger lightgrey shadow2">Add a new address:</span>
    	</div>										
    	<% remote_form_for :newaddress, :url => { :controller => 'users', :action => 'cartaddress' }, :update => { :success => 'cart-second' } do |na|%>
    		<div class="address-tr">
    			<div class="address-form-label" align="right">
    				<span class="grey small bold">Address 1: </span>
    			</div>
    			<div class="address-field">
    				<%= na.text_field :address1 %>
    			</div>
    		</div>	
    		
    		<div class="address-tr">
    			<div class="address-form-label" align="right">
    				<span class="grey small bold">Address 2: </span>
    			</div>
    			<div class="address-field">
    				<%= na.text_field :address2 %>
    			</div>
    		</div>	
    		
    		<div class="address-tr">
    			<div class="address-form-label" align="right">
    				<span class="grey small bold">City: </span>
    			</div>
    			<div class="address-field">
    				<%= na.text_field :city %>
    			</div>
    		</div>
    		
    		<div class="address-tr">
    			<div class="address-form-label" align="right">
    				<span class="grey small bold">State: </span>
    			</div>
    			<div class="address-field">
    				<%= na.text_field :state %>
    			</div>
    		</div>
    		
    		<div class="address-tr">
    			<div class="address-form-label" align="right">
    				<span class="grey small bold">Zip: </span>
    			</div>
    			<div class="address-field">
    				<%= na.text_field :zip %><%= na.hidden_field :user_id, :value => current_user.id %>
    			</div>
    		</div>
    		
    		<div class="address-tr" style="margin-top: 0px;">
    			<div class="address-form-label" align="right">
    			</div>
    			<div class="address-field">
    				<%= submit_tag 'ADD ADDRESS', :class => 'button', :id => 'submit-button' %>
    			</div>
    		</div>
    		
    	<% end %>
    </div>
    Which links to this action:
    Code Ruby:
      def cartaddress
     
        @cartaddress = Address.create!(params[:newaddress])
     
        respond_to do |format|
          format.html {} ## will do later
          format.js
        end
     
      end

    Which then calls cartaddress.rjs.

    Code JavaScript:
    page.replace_html :cartsecond, :partial => 'store/cartsecond'


    Which in turn is suppose to update id="cartsecond" with partial
    Code Ruby:
    			<%= render :partial => 'shared/jquerycart'%>
    			<div class="cart-header highlight bold large">
    				DELIVERY ADDRESS
    			</div>
    			<div class="delivery-address small grey" id="addresslist">
    				<% if @addresses.size == 0 %>
    				No addresses to show.  
    				<% else %>
    				<ul>
    				<% for addy in @addresses %>
    					<li class="cart-item <%= cycle("even", "odd") -%>">
    						<div style="width: 130px; float: left; line-height: 12px;">
    							<%= addy.address1 %> <%= addy.address2 %><br />
    							<%= addy.city %>, <%= addy.state %> <%= addy.zip %>
    						</div>
    						<div style="float: right; padding-top: 5px;">
    							<%= link_to_remote "select", :update => "shopcart", :url => {:controller => "cart", :action => "submit", :id => @cart.id, :addy => addy.id }, :html => { :class => "button"} %>
    						</div>	
    					</li>
    				<% end %>
    				</ul>
    				<% end %>
    			</div>
    			<!-- add new address @cart
    			The DIV ID that it is calling is in the application.html.erb file.
    			-->
    			<div style="width: 100px; height: 30px; padding-top: 10px; overflow: visible;">
    				<%= link_to_remote "Add New", :url => { :controller => 'users', :action => 'cartaddy' },:update => { :success => "cartaddress" }, :html => { :class => 'button' } %>
    			</div>
    			<!-- /add new address @cart -->

    The result is that I when <%= submit_tag 'ADD ADDRESS' %> is pressed a record is created in the DB, but the partial does not show the new record created. I have to either press the submit_tag again to show the new record, but of course another duplicate record is created, or refresh the page which is a bad option for me.

    I am sure I am missing something stupidly simple, but I can't figure it out.

    The rendering of the partial works, confirmed with firebug and other cruder methods, but it does not display the new record.

    Thanks in advance for your help!

  2. #2
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should not have to call the second javascript routine. If you use the :update option to specify the element id where you want the result of the AJAX call to be inserted, you can do it all via the single form_remote_for call. Have a look at the API:

    http://railsbrain.com/api/rails-2.3....orm_remote_for

    All you should have to do is update the form_remote_for call:
    Code:
    remote_form_for :newaddress, :url => { :controller => 'users', :action => 'cartaddress' }, :update => { :success => 'cartsecond' } do |na|&#37;>
    then render the partial via the action:
    Code:
    def cartaddress
     
        @cartaddress = Address.create!(params[:newaddress])
     
        render :partial => 'store/cartsecond'
       
    end
    You may have to pass @cartaddress to the partial via :locals, as I've found rendering partial asynchronously doesn't always result in controller instance variables being passed to the partial. It's often better to explicitly pass the data to the partial in the render call.

    Code:
    def cartaddress
     
        @cartaddress = Address.create!(params[:newaddress])
     
        render :partial => 'store/cartsecond', :locals => { :cartaddress => @cartaddress }
       
    end


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •