我收到以下错误并且不确定如何纠正错误 - 非常感谢您的帮助。
在用户显示页面时,我没有收到错误,但在例如任何其他页面时...例如事件index.html页面显示错误
No route matches {:action=>"update", :controller=>"friendships", :friend_id=>#<User id: 5, email: "ian@gmail.com"
终端中的扩展错误
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: "ian@gmail.com", encrypted_password: "$2a$10$PMO5FPBzjjnFHI/ye8rfP.ONtHP3gagXomj1sbruBXH..."
路线档案
Rails.application.routes.draw do
resources :friendships, only: [:create, :update, :destroy]
devise_for :users
resources :users
end
视图/共享/ _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
我不确定如何解决错误,我尝试了类似下面的内容但不起作用:
<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>
答案 0 :(得分:3)
更新控制器正在等待id
参数,而您传递的唯一参数是user_id
和friend_id
。
您可以将路线更改为以下
resources :friendships, only: [:create, :destroy] do
collection do
put :update
end
end