1
votes

Here is my simple ajax code.

The request is made, I get the response but my view does not get rendered with new data. I get a new page with new html that I asked to render instead being rendered inside current page.

controller -

    def list
    @users = User.find(:all)
    end

    def show
    respond_to do |format|
    format.html {   render :partial => "ajaxshow" }
    format.js
    end
    end

view -

<% @users.each do |c| %>
<%= link_to c.user_id, {:action => :show, :user_id => c.user_id, :remote => true } %>
<% end %>

partial -

# cat _ajaxshow.html.erb
<div  align="left" id="user_ajax">
<table width="1000px">
<tr><th>User ID</th><th>First</th><th>Last</th><th>ID</th><th>Email</th></tr>
</table>

log -

Started GET "/users/show?remote=true&user_id=someuser" for 10.10.10.10 at 2012-04-17 04:01:09 -0400
Processing by UsersController#show as HTML
  Parameters: {"remote"=>"true", "user_id"=>"someuser"}
  Rendered users/_ajaxshow.html.erb (0.3ms)
Completed 200 OK in 2ms (Views: 1.3ms | ActiveRecord: 0.0ms)
2
@pguardiario - see my comment below. I just added a js.erb but no result. - Majoris
How are you adding this data? It would seems sensible to me that you have to POST this data. So the correct path would be to use the create action. You can read here blog.madebydna.com/all/code/2011/12/05/ajax-in-rails-3.html and here net.tutsplus.com/tutorials/javascript-ajax/… - tommasop

2 Answers

1
votes

Your link_to will not trigger the ajax call

  <%= link_to c.user_id, {:action => :show, :user_id => c.user_id, :remote => true } %>

First change the above code

 <%= link_to c.user_id, {:action => :show, :user_id => c.user_id }, { :remote => true, :method => :get } %>
1
votes

You need to have a .js.erb file for the User controller action which will be called through the format.js call.

In show.js.erb use

$('#some_div_id').html($('<%= render :partial => "ajaxshow" %>'))

Anyhow the purpose of your action seems obscure to me.