Note that there are some explanatory texts on larger screens.

plurals
  1. POAjaxified Boolean Field toggles upon page load and refresh
    primarykey
    data
    text
    <p>I have a link_to that toggles a boolean field when clicked. The problem is that it toggles when the page is refreshed and/or loaded. Here is the code:</p> <p>stories/show.html.erb</p> <pre><code>&lt;div id="story"&gt; &lt;%= render @story %&gt; &lt;/div&gt; </code></pre> <p>_story.html.erb</p> <pre><code>&lt;div id="storyShow"&gt; &lt;% if story.user == current_user %&gt; &lt;%= render 'stories/published', { :story =&gt; story } %&gt; &lt;% end %&gt; &lt;/div&gt; </code></pre> <p>stories_controller.rb</p> <pre><code> def toggle_published @story = Story.find(params[:id]) @story.toggle!(:published) end </code></pre> <p>application_helper.rb</p> <pre><code> def published_link_text(publishable) publishable.published? ? 'Published' : 'Un-Published' end </code></pre> <p>_published.html.erb</p> <pre><code>&lt;% if story.published? %&gt; &lt;span id="publishedSwitchGreen" class="greenText"&gt; &lt;%= link_to published_link_text(story), toggle_published_story_path(story), :remote =&gt; true, :id =&gt; "story_publish#{story.id}" %&gt; &lt;/span&gt; &lt;% else %&gt; &lt;span id="publishedSwitchRed" class="redText"&gt; &lt;%= link_to published_link_text(story), toggle_published_story_path(story), :remote =&gt; true, :id =&gt; "story_publish#{story.id}" %&gt; &lt;/span&gt; &lt;% end %&gt; </code></pre> <p>toggle_published.js.erb</p> <pre><code>$("#story_publish&lt;%= @story.id %&gt;").text("&lt;%= published_link_text(@story) %&gt;"); </code></pre> <p>routes.rb</p> <pre><code> resources :stories do get 'toggle_published', :on =&gt; :member end </code></pre>
    singulars
    1. This table or related slice is empty.
    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