我正在为一个类创建一个网站,我正在尝试使用名为"Users"的模型和一个名为"Relationships"的连接模型来实现一个朋友请求函数.我在用户#show页面上有一个按钮,它应该通过在Relationships控制器中使用create方法添加朋友.这是按钮的代码:
<%= link_to "Add as Friend", relationships_path(:friend_id => @user), method: :post %>
Run Code Online (Sandbox Code Playgroud)
但是,当我按下链接时,它会尝试访问索引方法.在查看控制台后,看起来链接正在发送GET请求,该请求路由到索引方法,而不是路由到create方法的POST请求.有人可以解释为什么会出现这种错误以及我如何解决它?
编辑:根据要求,这是我的路线中的内容:
Rails.application.routes.draw do
resources :interests
get 'interests/create'
get 'interests/destroy'
get 'home/index'
get 'sessions/create'
get 'sessions/destroy'
resources :users
resources :relationships
resources :subscriptions
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
# root 'welcome#index'
root 'home#index'
get "/auth/:provider/callback" => "sessions#create"
get "/signout" => "sessions#destroy", :as => :signout
Run Code Online (Sandbox Code Playgroud)
使用link_to帮助程序向Rails指示您希望a在HTML中生成标记.关于a标记的HTML规范的任何元素都不允许生成POST请求.但是,由于Rails了解允许POST和DELETE使用链接发出请求的效用,因此它在link_to帮助程序中提供了这些选项.但是,它的实现必须在引擎盖下使用JavaScript才能正常运行.
检查是否已安装jquery-ujs,以及您的资产管道是否正常工作以便以这种方式使用帮助程序.
您还可以评估使用a form_for和a button是否更好,因为这将自动进行POST.
| 归档时间: |
|
| 查看次数: |
1290 次 |
| 最近记录: |