我正在一个具有用户到用户友谊功能的项目中工作。 我不知道我的路线发生了什么,但这是我在html中的输出:
/friends.Friends->这应该是链接“ friends”,但是当我单击它时什么也没有发生
和
/friend_requests.Friend%20requests->它也应该是链接“ friend_requests”
Rails.application.routes.draw do
root "events#index"
devise_for :users
devise_scope :user do
get "login", :to => "devise/sessions#new"
get "logout", :to => "devise/sessions#destroy"
get "signup", :to => "devise/registrations#new"
end
resources :users, :only => [ :index, :show ]
resources :friendships, :only => [:create, :update, :destroy]
get '/friend_requests', to: 'friend_requests#index'
get '/friends', to: 'friends#index'
resources :events
end
这就是我的看法:
<%= link_to friends_path "Friends" %>
<br>
<%= link_to friend_requests_path "Friend requests" %>
这是包执行耙路径的输出
Prefix Verb URI Pattern Controller#Action
root GET / events#index
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
user_password PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
POST /users/password(.:format) devise/passwords#create
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
user_registration PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
POST /users(.:format) devise/registrations#create
login GET /login(.:format) devise/sessions#new
logout GET /logout(.:format) devise/sessions#destroy
signup GET /signup(.:format) devise/registrations#new
users GET /users(.:format) users#index
user GET /users/:id(.:format) users#show
friendships POST /friendships(.:format) friendships#create
friendship PATCH /friendships/:id(.:format) friendships#update
PUT /friendships/:id(.:format) friendships#update
DELETE /friendships/:id(.:format) friendships#destroy
friend_requests GET /friend_requests(.:format) friend_requests#index
friends GET /friends(.:format) friends#index
events GET /events(.:format) events#index
POST /events(.:format) events#create
new_event GET /events/new(.:format) events#new
edit_event GET /events/:id/edit(.:format) events#edit
event GET /events/:id(.:format) events#show
PATCH /events/:id(.:format) events#update
PUT /events/:id(.:format) events#update
DELETE /events/:id(.:format) events#destroy
page GET /*id high_voltage/pages#show
rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update
rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
当我通过输入localhost:300 / friends或localhost:300 / friend_requests访问时,它可以工作 但是当我尝试使用链接到时,它就坏了:/
答案 0 :(得分:3)
参数的顺序不正确。
从Docs
link_to(name = nil,options = nil,html_options = nil,&block)public
使用一组选项创建的URL创建给定名称的锚元素。请参阅文档中url_for的有效选项。也可以传递String而不是options哈希,它会生成一个锚元素,该锚元素使用String的值作为链接的href。使用:back符号而不是选项哈希将生成指向引荐来源网址的链接(如果不存在引荐来源网址,则会使用JavaScript反向链接)。如果将nil作为名称传递,则链接本身的值将成为名称。
名称应作为第一个参数传递,而您将其作为第二个参数传递。
尝试像这样交换参数:
<%= link_to "Friends" ,friends_path %>
<br>
<%= link_to "Friend requests", friend_requests_path %>
答案 1 :(得分:1)
您在视图中的link_to助手不正确,
link_to(name = nil,options = nil,html_options = nil,&block)
<%= link_to "Friends", friends_path %>
<br>
<%= link_to "Friend requests", friend_requests_path %>