ruby - Rails: undefined method `relationships_path' -
i following michael hart book , wanted , add follow , unfollow button users show page. while implementing encountered error undefined method 'relationships_path'
.
any helpful , appreciated.
relationships controller.rb
class relationshipscontroller < applicationcontroller before_action :logged_in_user def create @user = user.find(params[:followed_id]) current_user.follow(@user) respond_to |format| format.html { redirect_to @user } format.js end end def destroy @user = relationship.find(params[:id]).followed current_user.unfollow(@user) respond_to |format| format.html { redirect_to @user } format.js end end end
users 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 |format| format.html { redirect_to @user } format.js end end def destroy @user = relationship.find(params[:id]).followed current_user.unfollow(@user) respond_to |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
relationship.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
user.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 user. def follow(other_user) active_relationships.create(followed_id: other_user.id) end # unfollows user. def unfollow(other_user) active_relationships.find_by(followed_id: other_user.id).destroy end # returns true if current user following other user. def following?(other_user) following.include?(other_user) end end
_follow.html.erb
<%= form_for(current_user.active_relationships.build, remote: true) |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) |f| %> <%= f.submit "unfollow", class: "btn" %> <% end %>
show.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>
routes.rb
rails.application.routes.draw 'users/index' 'users/show' devise_for :users member :following, :followers end end 'users/:id' => 'users#show', as: :user end
relationships | migration
class createrelationships < activerecord::migration def change create_table :relationships |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
as per route.rb, don't have required route i.e. post /relationships
. add route.rb
:
resources :relationships
Comments
Post a Comment