ARTLoe
ARTLoe

Reputation: 1799

No route matches {:action=>"update", :controller=>"" - Rails 5

I am getting the below error and unsure how to correct it - your help would be much appreciated.

when at a users show page, i do not get the error, but when at for example at any other page...such as the events index.html page the error displays

No route matches {:action=>"update", :controller=>"friendships", :friend_id=>#<User id: 5, email: "[email protected]"

expanded error in terminal

Started GET "/events" for ::1 at 2016-12-17 12:44:43 +0000
Processing by EventsController#index as HTML
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 4]]
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 4]]
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."email" IS NULL LIMIT 1
  Subscription Load (0.1ms)  SELECT  "subscriptions".* FROM "subscriptions" WHERE "subscriptions"."title" = ? LIMIT 1  [["title", "premium"]]
  User Load (0.1ms)  SELECT "users".* FROM "users" INNER JOIN "friendships" ON "users"."id" = "friendships"."friend_id" WHERE "friendships"."user_id" = ? AND "friendships"."status" = ?  [["user_id", 4], ["status", "requested"]]
   (0.2ms)  SELECT COUNT(*) FROM "users" INNER JOIN "friendships" ON "users"."id" = "friendships"."friend_id" WHERE "friendships"."user_id" = ? AND "friendships"."status" = ?  [["user_id", 4], ["status", "requested"]]
  Rendered shared/_content_dropdownbox_friendrequest.html.erb (4.8ms)
  Rendered shared/_header.html.erb (10.7ms)
  Rendered events/index.html.erb within layouts/application (11.8ms)
Completed 500 Internal Server Error in 23ms

ActionController::UrlGenerationError - No route matches {:action=>"update", :controller=>"friendships", :friend_id=>#<User id: 5, email: "[email protected]", encrypted_password: "$2a$10$PMO5FPBzjjnFHI/ye8rfP.ONtHP3gagXomj1sbruBXH..."

route file

Rails.application.routes.draw do

  resources :friendships,   only: [:create, :update, :destroy]

  devise_for :users
  resources :users
end

views/shared/_content_dropdownbox_friendrequest.html.erb

<ul>
  <% current_user.requested_friends.each do |requester| %>
    <li>
      <%= link_to(image_tag(requester.image_url, :alt =>  "image", :class =>"#", id: ""), user_path(requester)) %>
      <%= link_to truncate("#{requester.firstname} #{requester.lastname}", length: 23), user_path(requester) %>
      <%= link_to 'Accept', friendship_path(user_id: current_user, friend_id: requester), controller: "friendships", action: "update", method: :put %>
      <%= link_to 'Decline', friendship_path(user_id: current_user, friend_id: requester), controller: "friendships", action: "decline", method: :delete %>
    </li>
    <div class="clear"></div>
  <% end %>
</ul>

frienships_controller

class FriendshipsController < ApplicationController
  before_action :authenticate_user!
  before_filter :setup_friends

  # Sends a friend request.
  # We'd rather call this "request", but that's not allowed by Rails.
  def create
    Friendship.request(@user, @friend)
    flash[:notice] = "Request sent."
    redirect_to :back
  end

  # Accepts a friend request.
  # We'd rather call this "accept", but that's not allowed by Rails.
  def update
    @user = User.friendly.find(params[:user_id])
    @friend = User.friendly.find(params[:friend_id])
    if @user.requested_friends.include?(@friend)
      Friendship.accept(@user, @friend)
      flash[:notice] = "Connection with #{@friend.firstname} accepted!"
    else
      flash[:notice] = "No connect request from #{@friend.firstname}."
    end
    redirect_to :back
  end

  def destroy
    @user = User.friendly.find(params[:user_id])
    @friend = User.friendly.find(params[:friend_id])
    if @user.requested_friends.include?(@friend) #decline
      Friendship.breakup(@user, @friend)
      redirect_to :back
    elsif @user.pending_friends.include?(@friend) #cancel
      Friendship.breakup(@user, @friend)
      redirect_to :back
    elsif @user.friends.include?(@friend) #delete
      Friendship.breakup(@user, @friend)
      redirect_to :back
    end
  end

  private
  def setup_friends
    @user = User.find(current_user.id)
    @friend = User.find_by_email(params[:id])
  end
end

i am unsure how to solve the error, i tried something like the below but does not work:

<span>
  <%= form_for friendship, url: friendship_path(requester), method: :put do |f| %>
    <%= f.hidden_field :user_id, value: current_user.id %>
    <%= f.hidden_field :friend_id, value: requester.id %>
    <%= submit_tag "Accept_test", controller: "friendships", action: "update", class: "btn btn_add_friend" %>
  <% end %>
</span>

Upvotes: 1

Views: 2784

Answers (1)

Nikita Misharin
Nikita Misharin

Reputation: 2020

update controller is waiting for an id parameter, while the only parameters you pass are user_id and friend_id.

You can change your routes to the following

resources :friendships,   only: [:create, :destroy] do
  collection do
    put :update
  end
end

Upvotes: 4

Related Questions