0

我需要有关友谊独特性的帮助,并将我的 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

友谊控制器.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 %>

谁能帮我 ?

4

1 回答 1

1

鉴于嵌套路线,它需要了解用户才能获得友谊。您可以使用很酷的助手来帮助您从对象中获取路径

更像红宝石的惯用语

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

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

由于您使用的是设计......很可能您真正想要的是:

<%  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" %>
于 2012-07-09T12:13:02.217 回答