[英]Ruby on Rails post path turns into get path
我正在將Ruby on Rails 5用於個人項目,但似乎無法獲得指向正確控制器操作和客戶端操作的鏈接路由。
<div class="admin-users index">
<h2>Admin Users</h2>
<%= image_tag('plus_sign.png', :size => '11x11', :alt => '+') %>
<%= link_to("Add New Admin User", new_admin_user_path, :class => 'action new') %>
<table class="listing" summary="Admin user list">
<tr class="header">
<th>Username</th>
<th>Name</th>
<th>Email</th>
<th>Actions</th>
</tr>
<% @admin_users.each do |admin_user| %>
<tr>
<td><%= admin_user.username %></td>
<td><%= admin_user.name %></td>
<td><%= mail_to(admin_user.email) %></td>
<td class="actions">
<%= link_to("Edit", edit_admin_user_path(admin_user), :class => 'action edit') %>
<%= link_to("Delete", delete_admin_user_path(admin_user), :class => 'action delete') %>
<%= link_to("Follow User", followings_path(:followed_id => admin_user), :method => :post)%>
</td>
</tr>
<% end %>
</table>
</div>
我有一個鏈接操作,發出發布請求以在兩個用戶之間創建綁定。
Rails.application.routes.draw do
resources :followings, :except => [:show, :update, :delete, :new] do
end
我使用資源路由使事情變得更簡單,但是不幸的是,當我單擊“關注用戶”時,我的發布請求變成了一個獲取請求,並且我無法將任何用戶添加到我的關注列表中。
http://localhost:3000/followings?followed_id=5
當我單擊“關注用戶”按鈕時,它會帶我到上面的URL,這很好,但是在查看控制台后,我看到它是一個GET請求。
Started GET "/followings?followed_id=5" for ::1 at 2016-12-24 18:16:26 -0400
ActiveRecord::SchemaMigration Load (1.0ms) SELECT `schema_migrations`.* FROM `schema_migrations`
Processing by FollowingsController#index as HTML
Parameters: {"followed_id"=>"5"}
AdminUser Load (0.0ms) SELECT `admin_users`.* FROM `admin_users` WHERE `admin_users`.`id` = 1 LIMIT 1
Rendering followings/index.html.erb within layouts/admin
Following Load (0.0ms) SELECT `followings`.* FROM `followings` WHERE `followings`.`admin_user_id` = 1
AdminUser Load (1.0ms) SELECT `admin_users`.* FROM `admin_users` WHERE `admin_users`.`id` = 3 LIMIT 1
Rendered followings/index.html.erb within layouts/admin (64.0ms)
Completed 200 OK in 542ms (Views: 448.4ms | ActiveRecord: 10.0ms)
這是以下控制器:
class FollowingsController < ApplicationController
layout 'admin'
def index
admin_user = AdminUser.find(session[:user_id])
@user = admin_user
end
def create
current_user = AdminUser.find(session[:user_id])
@following = current_user.following.build(:followed_id => params[:followed_id])
if @following.save
flash[:notice] = "Added user."
redirect_to root_url
else
flash[:notice] = "Cannot add user."
redirect_to(admin_users_path)
end
end
def destroy
current_user = AdminUser.find(session[:user_id])
@following = current_user.followings.find(params[:id])
@following.destroy
flash[:notice] = "Removed user."
redirect_to root_url
end
end
不知道為什么要這么做。
我不確定,但是我可以想象這與您為“后續活動”設置的路線有關。 跟隨不是跟隨的復數,因此不遵循Rails的命名約定,因此不能正確生成路由。 我會將資源切換為“關注者”(因為關注者是關注者的復數),或者設置您自己的自定義路線,而不使用資源方法。 您可以執行以下操作:
post 'follow_user/:followed_id' => 'followings#create', as: :follow_user
get 'followings' => 'followings#index', as: :followings
那么您的關注用戶鏈接將是:
<%= link_to("Follow User", follow_user_path(followed_id: admin_user), method: :post) %>
我還建議在更新或創建記錄時在控制器中使用強參數。 http://api.rubyonrails.org/classes/ActionController/StrongParameters.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.