Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Issue with forms in partial after ajax render


  • Please log in to reply
4 replies to this topic

#1 Pash91

Pash91

    Passenger

  • Members
  • 2 posts

Posted 29 September 2013 - 01:11 PM

I'm having an issue with UJS, jQuery and partials. I have a table with a bunch of rows. When I click edit on one of the rows, the row transforms into text fields where I can edit the row. Then I have a submit; however, clicking it does nothing. It just doesn't look like its triggering. Clicking edit sends a call to the controller, which responds with edit.js.erb which has jQuery replace the contents of the row with the edit partial.

Is there anything obvious I'm overlooking? This works fine when I remove all the AJAX calls

Thanks

Item controller:

 def edit
respond_to do |format|  
     format.html { respond_with @item }
       format.js { } 
   end
    end
    def update
     @item.update_attributes(params[:item])
respond_to do |format|  
     format.html { respond_with @item, :location => items_path }
       format.js { } 
   end     
   end  

edit.html.erb: 

<%= form_for(@item, item_path) do |form| %> <%= render form %> <% end %>

_form.html.erb

<%= content_tag :tr, :class => "item_#{@item.id}" do -%>
  <td class="name"><%= form.text_field :name %></td>
  <td class="description"><%= form.text_area :description, :size => "30x5" %></td>
  <td class="price"><%= number_to_currency(form.text_field :price) %></td>
  <td class="actions"><%= form.submit :submit %></td>
<% end -%>

 

edit.js.erb:

$(' .item ').html(" 'item/edit', :locals => {:item => @item})) %>");

update.js.erb:

$(' .item ').html(" 'item/item', :item => @item) %>");


#2 lazarus

lazarus

    Signalman

  • Members
  • 16 posts
  • LocationGreece

Posted 01 October 2013 - 07:02 PM

Shouldn't this

 

$(' .item ').html(" 'item/item', :item => @item) %>");

 

be

 

$(' .item ').html(" 'item/item', :locals => {:item => @item} ");



#3 Pash91

Pash91

    Passenger

  • Members
  • 2 posts

Posted 01 October 2013 - 07:04 PM

Perhaps. It turns out the problem was trying to draw a form in a table cell. I didn't know that wasn't allowed and didn't notice my form was closing right after being open. Took forever to notice that. 



#4 lazarus

lazarus

    Signalman

  • Members
  • 16 posts
  • LocationGreece

Posted 01 October 2013 - 07:08 PM

Perhaps. It turns out the problem was trying to draw a form in a table cell. I didn't know that wasn't allowed and didn't notice my form was closing right after being open. Took forever to notice that. 

 

Ok, at least it's solved now.



#5 Bharat Soni

Bharat Soni

    Dispatcher

  • Members
  • 53 posts
  • LocationAhmedabad

Posted 14 October 2013 - 01:00 PM

As you have given the dynamic class like :class=>"item_#{@item.id}" 

 

do like this------

 

for edit.js.erb

 

$(".item_#{@item.id}").html("<%= escape_javascript(render(:partial => 'item/edit', :locals => {:item =>@item})).html_safe %>");

 

for update.js.erb

 

$(".item_#{@item.id}").html("<%= escape_javascript(render(:partial => 'item/item', :locals => {:item =>@item})).html_safe %>");


Fun with Ruby on Rails





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users