Note that there are some explanatory texts on larger screens.

plurals
  1. POHas_many through and path helper - accessing resources through the application
    primarykey
    data
    text
    <p>I have an app in which users can follow law firms</p> <p>I have 3 models - User - Firm - Follow</p> <pre><code>class Firm &lt; ActiveRecord::Base has_many :follows, :dependent =&gt; :destroy has_many :users, :through =&gt; :follows class User &lt; ActiveRecord::Base has_many :follows, :dependent =&gt; :destroy has_many :firms, :through =&gt; :follows class Follow &lt; ActiveRecord::Base belongs_to :firm belongs_to :user </code></pre> <p>In a table in my firms index view, I would like to take the current signed and create an association between that user and the law firm - through the follow table.</p> <p>In effect doing this - firm.users &lt;&lt; User(current)</p> <p>This is the code that I have at present, how would you suggest that I structure the path, and the corresponding controller?</p> <pre><code>&lt;% @firms.each do |firm| %&gt; &lt;tr id = "firm_&lt;%= firm.id %&gt;"&gt; &lt;td&gt;&lt;%= link_to image_tag(firm.logo_url, :size =&gt; "80x120"), firm.url %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= link_to firm.name, firm_path(firm) %&gt;&lt;/td&gt; &lt;% if user_signed_in? %&gt;&lt;td&gt; &lt;%= button_to 'Follow', ? , method: :post %&gt; &lt;/td&gt; &lt;% end %&gt; </code></pre> <p>I am using devise for the User authentication and have put the following helpers into application helper to allow my login partial to function in a different models view.</p> <pre><code> def resource_name :user end def resource_id :user_id end def resource @resource ||= User.new end </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