Note that there are some explanatory texts on larger screens.

plurals
  1. POCalling an action in ArticlesController, from CommentsController - Rails
    primarykey
    data
    text
    <h3>The situation:</h3> <p>I have something like the following:</p> <pre><code>class Game &lt; ActiveRecord::Base has_many :turns def calculate_score #return score of all turns played so far end end class Turn &lt; ActiveRecord::Base belongs_to :game end </code></pre> <p>... with corresponding controllers. (this isn't exactly what I have, but my question is very general. The question title shows another similar situation)</p> <p>When I am in <strong>/game/:id</strong> (the show action), I display all turns, and have AJAX edit-in-place to update any turns. Updating a turn calls the <strong>update</strong> method of <em>TurnsController</em>.</p> <h3>The question:</h3> <p>How can I, from <em>TurnsController</em>, call an action in the <em>GameController</em> after a turn is updated?</p> <p>I need an action called "turn_updated" in <em>GameController</em> to check all turns and perform manipulations on the collection game.turns .</p> <p>Thanks!</p> <p>PS: Clearly I am new to Rails, so I can't be more specific because I am not exactly sure of how to phrase my question, or of what is possible.</p> <hr /> <h3>Update:</h3> <p>Thanks for the answers so far.</p> <p>I have been using a redirect_to, but my problem with that is that <strong>update</strong> is an AJAX call. It needs to <strong>render :text => @turn.value</strong> in some conditions, and <strong>redirect_to</strong> in others. Since it's an AJAX call, redirect_to replaces the div that showed the turn value with the entire contents of the new address.</p> <p>I am trying things along these lines now:</p> <pre><code>#turn update action def update url = url_for(:controller =&gt; 'games', :action =&gt; 'turn_updated', :id =&gt; @turn.game_id) render :partial =&gt; 'shared/redirect.js.erb', :locals =&gt; {:url =&gt; url} end </code></pre> <p>and the partial is:</p> <pre><code>&lt;script type="text/javascript"&gt;window.location.replace(&lt;%= url %&gt;);&lt;/script&gt; </code></pre> <p>I am getting closer with this, but it seems somewhat flimsy... For instance, if I replace the partial's JS with <em>alert("Works!");</em> it executes the alert, but the redirect doesn't always happen.</p>
    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