独特的友谊帮助需要Rails 3

时间:2012-07-09 10:36:11

标签: ruby-on-rails rails-activerecord

我需要有关友谊唯一性的帮助并添加到朋友链接 我的routes.rb:

devise_for :users

resources :users, only: [:index, :show] do
          resources :friendships, only: [:create, :destroy]
          end

我的用户模型:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :username
  # attr_accessible :title, :body
  has_many :topics
  has_many :posts
  has_many :friendships
    has_many :friends, :through => :friendships
    has_many :inverse_friendships, :class_name => "Friendship", :foreign_key => "friend_id"
    has_many :inverse_friends, :through => :inverse_friendships, :source => :user
end

我的友谊模特:

class Friendship < ActiveRecord::Base
  attr_accessible :friend_id, :user_id
  belongs_to :user
    belongs_to :friend, :class_name => "User"
end

friendships_controller.rb:

class FriendshipsController < ApplicationController
    before_filter :authenticate_user!

    def create
        @friendship = current_user.friendships.build(:friend_id => params[:friend_id])
        if @friendship.save
            flash[:notice] = "Arkadaşlara eklendi."
            redirect_to root_url
        else
            flash[:error] = "Arkadaşlara Eklenemiyor."
            redirect_to root_url
        end
    end

    def destroy
        @friendship = current_user.friendships.find(params[:id])
        @friendship.destroy
        flash[:notice] = "Arkadaşlarımdan kaldırıldı."
        redirect_to current_user
    end
end

users_controller.rb:

class UsersController < ApplicationController
    before_filter :authenticate_user!

  def index
        @users = User.all
  end
  def show
    @user = User.find(params[:id])
    @topics = @user.topics.paginate(page: params[:page])
    @friendship = @user.friendships.build(:friend_id => params[:friend_id])
    @friendships = @user.friendships.all

  end 
end

这是我的朋友添加链接:

 <h1>
        <%= @user.username %>
      </h1>

            <%= link_to "Arkadaşlarıma Ekle", friendships_path(:friend_id => @user), :method => :post,class: "btn btn-large btn-primary" %>

我无法找到将用户显示为朋友的正确途径。

<%  for friendship in @friendships %> 
<%= link_to friendship.friend.username, '#' %>
(<%= link_to 'Sil', friendship, :method => :delete %>)
<% end %>

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

鉴于嵌套路线,它需要知道用户才能获得友谊。您可以使用从对象

获取路径的酷助手

更像一点惯用的红宝石

<%  @friendships.each do |friendship} %> 
  <%= link_to friendship.friend.username, '#' %>
  (<%= link_to 'Sil', [@user, friendship], :method => :delete %>)
<% end %>

对于“添加到朋友”链接也是类似的...但我不清楚逻辑 - 你在这个阶段有@user吗? @user是登录用户还是“当前”用户?

因为你正在使用Devise ......你上面真正想要的是:

<%  current_user.friendships.each do |friendship} %> 
  <%= link_to friendship.friend.username, '#' %>
  (<%= link_to 'Sil', [current_user, friendship], :method => :delete %>)
<% end %>

如果此代码段来自另一个用户的页面...则更像是

<h1><%= @user.username %></h1>

<%= link_to "Arkadaşlarıma Ekle", user_friendships_path(current_user, :friend_id => @user), :method => :post, class: "btn btn-large btn-primary" %>