Note that there are some explanatory texts on larger screens.

plurals
  1. POController logic for on-click events used with link_to
    primarykey
    data
    text
    <p>I am interested making a feature similar to that of SO's, where an user who has asked a question could select an answer from a list of responses, but am stuck on how to handle the click events from the controller perspective. </p> <p>Currently, I have the models <code>Question</code>, which <code>has_many</code> <code>answers</code>. Additionally, I have made an <code>Selection</code> model, which is meant to symbolize the answer selection, where each <code>Question</code> only could <code>has_one</code> selected answer. </p> <p>I render each questions's answers along with the question in the question's <code>show</code> action. In my <code>_answer</code> partial, I want to place the following logic: "if the user who has asked the question is logged on, he could see links to 'unselect' the answer <em>if</em> the related <code>selection</code> has that answer's <code>answer_id</code> already stored (in other words, marking that this answer was previously selected), pr otherwise he could 'select' the answer which stores the answer's <code>answer_id</code> in <code>selection</code>. (I should mention that I adapted most of the code below from <a href="https://github.com/schneems/reddit_on_rails/blob/master/part_two_comments_and_votes.md" rel="nofollow">Rails tutorial</a> here, where I (unfortunately) don't quite have a solid understanding how to use <code>POST</code> to match or assign attributes to an instance variable )</p> <pre><code> &lt;% if current_user?(answer.question.user) %&gt; &lt;% if current_user.selections.where(:answer_id =&gt; answer.id) == answer.id %&gt; &lt;%= link_to "unselect", selections_path(:selection =&gt; {:answer_id =&gt; nil}), :method =&gt; :post %&gt; &lt;% else %&gt; &lt;%= link_to "select", selections_path(:selection =&gt; {:answer_id =&gt; answer.id}), :method =&gt; :post %&gt; &lt;% end %&gt; &lt;% end %&gt; </code></pre> <p>My roadblock is in the controller, where I'm stuck on how to use <code>the create</code> method to assign the <code>@selection</code> variable to a new <code>answer_id</code> if the user selects a new answer via the "select" link. Any help, or any guides on how to write on-click events would be much appreciated. Thanks!</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.
 

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