ruby-on-rails - rails : undefined method `relationships_path'

标签 ruby-on-rails ruby ruby-on-rails-3

我正在关注 Michael Hart 的书,并希望将关注和取消关注按钮添加到用户显示页面。在实现这个时,我遇到了这个错误 undefined method 'relationships_path'

任何帮助都会有所帮助和感激。

enter image description here


关系 Controller .rb

class RelationshipsController < ApplicationController
  before_action :logged_in_user

  def create
    @user = User.find(params[:followed_id])
    current_user.follow(@user)
    respond_to do |format|
      format.html { redirect_to @user }
      format.js
    end
  end

  def destroy
    @user = Relationship.find(params[:id]).followed
    current_user.unfollow(@user)
    respond_to do |format|
      format.html { redirect_to @user }
      format.js
    end
  end
end

用户 Controller .rb

class UsersController < ApplicationController

   before_action :authenticate_user!
   before_action :set_user



def create
  @user = User.friendly.find(params[:followed_id])
  current_user.follow(@user)
  respond_to do |format|
    format.html { redirect_to @user }
    format.js
  end
end

def destroy
  @user = Relationship.find(params[:id]).followed
  current_user.unfollow(@user)
  respond_to do |format|
    format.html { redirect_to @user }
    format.js
  end
end

  def following
    @title = "Following"
    @user  = User.find(params[:id])
    @users = @user.following.paginate(page: params[:page])
    render 'show_follow'
  end

  def followers
    @title = "Followers"
    @user  = User.find(params[:id])
    @users = @user.followers.paginate(page: params[:page])
    render 'show_follow'
  end
end

关系.rb

class Relationship < ActiveRecord::Base
  belongs_to :follower, class_name: "User"
  belongs_to :followed, class_name: "User"
  validates :follower_id, presence: true
  validates :followed_id, presence: true
end

用户.rb

class User < ActiveRecord::Base
    extend FriendlyId

    friendly_id :name, use: :slugged

  has_many :active_relationships, class_name:  "Relationship",
                                  foreign_key: "follower_id",
                                  dependent:   :destroy
  has_many :following, through: :active_relationships, source: :followed
  has_many :passive_relationships, class_name:  "Relationship",
                                   foreign_key: "followed_id",
                                   dependent:   :destroy
  has_many :followers, through: :passive_relationships, source: :follower



      # Follows a user.
  def follow(other_user)
    active_relationships.create(followed_id: other_user.id)
  end

  # Unfollows a user.
  def unfollow(other_user)
    active_relationships.find_by(followed_id: other_user.id).destroy
  end

  # Returns true if the current user is following the other user.
  def following?(other_user)
    following.include?(other_user)
  end

end

_follow.html.erb

<%= form_for(current_user.active_relationships.build, remote: true) do |f| %>
  <div><%= hidden_field_tag :followed_id, @user.id %></div>                        
  <%= f.submit "Follow", class: "btn btn-primary" %>
<% end %>

_unfollow.html.erb

<%= form_for(current_user.active_relationships.find_by(followed_id: @user.id),
             html: { method: :delete },
             remote: true) do |f| %>                       
  <%= f.submit "Unfollow", class: "btn" %>
<% end %>

显示.html.erb

<% provide(:title, @user.name) %>
<div class="row">
  <aside class="col-md-4">
    <section class="stats">
      <% @user ||= current_user %>

<div class="stats">

    <strong id="following" class="stat">
      <%= @user.following.count %>
    </strong>
    following
  </a>

    <strong id="followers" class="stat">
      <%= @user.followers.count %>
    </strong>
    followers
  </a>
      <%= render 'follow'  %>
</div>
    </section>
  </aside>

</div>

路线.rb

 Rails.application.routes.draw do

    get 'users/index'
    get 'users/show'

    devise_for :users do
        member do
          get :following, :followers
        end
    end

    get 'users/:id' => 'users#show', as: :user

 end

人际关系 |迁移

class CreateRelationships < ActiveRecord::Migration
  def change
    create_table :relationships do |t|
      t.integer :follower_id, index: true
      t.integer :followed_id, index: true

      t.timestamps null: false
    end
    add_index :relationships, [:follower_id, :followed_id], unique: true
  end
end

最佳答案

根据您的 route.rb,您没有所需的路线,即 POST /relationships。将其添加到您的 route.rb 中:

resources :relationships

关于ruby-on-rails - rails : undefined method `relationships_path' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31402682/

相关文章:

ruby-on-rails - 解析字符串以添加到 URL 编码的 URL

jquery - 自动隐藏 rails 中的 flash 消息

ruby-on-rails - 如何在 OSX 上安装 Ruby on Rails 3?

ruby-on-rails - Ruby On Rails : How do I create multple text_field_tags that store the values into the same array?

ruby-on-rails - ruby 对象数组...或散列

ruby-on-rails - Rails google-ads-ruby 设置

ruby-on-rails - 如何将link_to放在rails中的字符串中

ruby-on-rails - 如何阻止 Rails 为没有 ID 的连接表调用 next_seq?

ruby-on-rails - Rails 3 脚手架,添加路由 问题

ruby - 在 Rakefile 中动态生成 Rake 测试任务(基于现有的测试文件)