Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby On Rails: Ajax to Refresh Partial Not Working
    primarykey
    data
    text
    <p>I am having a difficult time trying to get my partial to refresh on button press. I want to simply refresh the div, and not the whole page. Here is what I have in the view:</p> <pre><code>&lt;div id="ajax"&gt; &lt;%= render 'layouts/confessions' %&gt; &lt;/div&gt; </code></pre> <p>Then in my partial <code>_confessions.html.erb</code>, I have some basic html and two buttons similar to this:</p> <pre><code>&lt;%= form_tag( { :controller =&gt; :confessions, :action =&gt; :upvote, :id =&gt; conf.id }, { :method =&gt; :put } ) do %&gt; &lt;%= submit_tag 'Like' %&gt; &lt;% end %&gt; </code></pre> <p>My confessions_controller.rb:</p> <pre><code>def upvote @confession = Confession.find(params[:id]) Confession.increment_counter :upvotes, @confession respond_to do |format| format.js end end </code></pre> <p>And finally, <code>upvote.js.erb</code>:</p> <pre><code>$('#ajax').html("&lt;%= escape_javascript(render(:partial =&gt; 'confessions')).html_safe %&gt;"); </code></pre> <p>The action of submitting to my database is working, but the page is now redirecting to <code>/upvote?id=9</code> (id can be different), instead of just refreshing the div. What am I doing wrong? I am new to Rails, so I could be missing something completely obvious...</p> <p>EDIT: Here is my folder structure:</p> <p>My view: <code>views/pages/home.html.erb</code></p> <p>My partial: <code>views/layouts/_confessions.html.erb</code></p> <p>My Controller: <code>controllers/confessions_controller.rb</code></p> <p>My js.erb file: <code>views/confessions/upvote.js.erb </code></p> <p>After <code>rake routes</code></p> <pre><code> confessions GET /confessions(.:format) confessions#index POST /confessions(.:format) confessions#create new_confession GET /confessions/new(.:format) confessions#new edit_confession GET /confessions/:id/edit(.:format) confessions#edit confession GET /confessions/:id(.:format) confessions#show PUT /confessions/:id(.:format) confessions#update DELETE /confessions/:id(.:format) confessions#destroy upvote /upvote(.:format) confessions#upvote downvote /downvote(.:format) confessions#downvote root / pages#home </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload