Note that there are some explanatory texts on larger screens.

plurals
  1. POUser friendships Nesting logic in Rails 3
    primarykey
    data
    text
    <p>I am trying to finish my forum application. I want arrange user Friendships. I am using devise as authentication system. I want some suggestions. Shall i nest the resources in User and Friendships. This is the way Railscasts used:</p> <pre><code>devise_for :users resources :users, only: [:index, :show] resources :friendships, only: [:create, :destroy] </code></pre> <p>This is how i used: </p> <pre><code>devise_for :users resources :users, only: [:index, :show] do resources :friendships, only: [:create, :destroy] end </code></pre> <p>My real problem is that . I want to use friendships in a way that , signed_in user can check the users list , and add a friend if he is not in the friendslist. Now i can add a friend multiple times. And also ,user can add himself as a friend.</p> <p>How can i fix this links with <code>if/else</code> statements:</p> <p>showing a user profile works:</p> <pre><code>&lt;section&gt; &lt;h1&gt; &lt;%= @user.username %&gt; &lt;/h1&gt; &lt;%= link_to "Arkadaşlarıma Ekle", friendships_path(:friend_id =&gt; @user), :method =&gt; :post,class: "btn btn-large btn-primary" %&gt; &lt;/section&gt; </code></pre> <p>and herei I cant find the way to show the friend's profile. :</p> <pre><code>&lt;% for friendship in @friendships %&gt; &lt;%= link_to friendship.friend.username, '#' %&gt; (&lt;%= link_to 'Sil', friendship, :method =&gt; :delete %&gt;) &lt;% end %&gt; </code></pre> <p>.. rake routes:</p> <p>I can understand that , I have to use correct if/elses but i am lost in nesting my resources and routing. . Thanks for explanations..</p> <p>These are my edits: In my users_controller: </p> <pre><code>def user_is_friend?(other_user) current_user.friendships.find_by_friend_id(other_user.id) end &lt;% unless user_is_friend?(@user) %&gt; &lt;%= button_to "Add friend", friendships_path(:friend_id =&gt; user), :method =&gt; :post %&gt; &lt;% end %&gt; </code></pre> <p>Is it correct? </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.
    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