Rails 代码中没有路由匹配
No route matches in Rails code
此代码全部基于 Michael Hartl 在 Rails 上的 Ruby 教程。我在 users_controller
中使用
创建了一个 assigned
动作
def assign
puts "in assign..."
@scout = User.find(params[:follower_id])
@der = User.find(params[:followed_id])
end
我意识到这目前没有任何作用,但在 _follow
部分,我有
<%= form_for @user, url: { action: "assign" } do |f| %>
<div><%= hidden_field_tag :follower_id, @user.id %></div>
<%= f.label :followed_id, "Assign #{@user.name} to" %>
<%= f.collection_select :following, @ders, :id, :name, prompt: true %>
<%= f.submit "Assign", class: "btn btn-primary" %>
<% end %>
但我收到错误 No route matches {:action=>"assign", :controller=>"users", :id=>"4"}
。我是 rails 的新手,所以这可能只是一个愚蠢的问题。我究竟做错了什么?我需要修改我的 routes.rb
文件吗?另外,如果我尝试 <%= form_for @user do |f| %>
,控制器如何知道要使用哪个操作?是基于显示表单的动作吗?
编辑:我的 routes.rb
文件是
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users do
member do
get :following, :followers
end
end
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :relationships, only: [:create, :destroy]
end
编辑 2:@Rich 的 form_for
块生成的 HTML 是
<form class="edit_user" id="edit_user_5" action="/users/5/assign" accept-charset="UTF-8" method="post">
<input name="utf8" type="hidden" value="✓">
<input type="hidden" name="_method" value="patch">
<input type="hidden" name="authenticity_token" value="...">
<label for="user_followed_id">Assign Mr. Marley Muller to</label>
<select name="user[following]" id="user_following">
<option value="1">Example User</option>
<option value="2">Matthew Swartz</option>
<option value="3">Joseph Swartz</option>
</select>
<input type="submit" name="commit" value="Assign" class="btn btn-primary">
</form>
这就是为什么我目前收到找不到 ID 错误的原因,因为它没有发送真实 ID (edit_user_5
)
编辑 3:这是为请求传递的参数
Started PATCH "/users/6/assign" for 68.100.59.128 at 2015-12-15 03:54:39 +0000
Processing by UsersController#assign as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"...", "user"=>{"following"=>"2"}, "commit"=>"Assign", "id"=>"6"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 6]]
Unpermitted parameter: following
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", nil]]
Completed 404 Not Found in 9ms (ActiveRecord: 1.0ms)
ActiveRecord::RecordNotFound (Couldn't find User with 'id'=):
app/controllers/users_controller.rb:69:in `assign'
编辑 4:使用以下路由文件,
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users do
member do
get :following, :followers
match :assign, to: :assign, via: [:post, :patch]
end
end
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :relationships, only: [:create, :destroy]
end
以及以下用于指定一个用户关注另一个用户的表格,
<%= form_for @user, url: { action: "assign" } do |f| %>
<%= f.label :follower_id, "Assign #{@user.name} to" %>
<%= f.collection_select :following, @ders, :id, :name, prompt: true %>
<%= f.submit "Assign", class: "btn btn-primary" %>
<% end %>
我收到 Couldn't find User with 'id'=
的 ActiveRecord::RecordNotFound in UsersController#assign
错误,但使用以下参数:
{"utf8"=>"✓",
"_method"=>"patch",
"authenticity_token"=>"...",
"user"=>{"following"=>"3"},
"commit"=>"Assign",
"id"=>"4"}
ID 正确:"user"=>{"following"=>"3"}
和 "id"=>"4"
,我相信我只是试图错误地访问它们。这是用户控制器中的分配操作:
def assign
@scout = User.find(params[:id])
@der = User.find(params_hash[:followed_id])
# make scout follow der here
redirect_to @scout
end
想法?
在您的 routes.rb 文件中,您必须像这样定义特定控制器操作的路由:
resource :users do
member do
post 'assign' # change the request method to 'put'
end
end
然后它将为控制器中的 assign 方法生成一个路由,例如:users/4/assign
有关详细信息,请访问 guides
要添加到 Emu
的答案中,您的代码将不起作用...
def assign
puts "in assign..."
@scout = User.find(params[:follower_id])
@der = User.find(params[:followed_id])
end
您从 form_for
对象获得的参数结构如下:
params[:user][:follower_id]
params[:user][:followed_id]
您必须使用以下内容:
#app/controllers/users_controller.rb
class UsersController < ApplicationController
def assign
@scout = User.find params[:id]
@der = User.find params_hash[:followed_id]
end
private
def params_hash
params.require(:user).permit(:followed_id)
end
end
如果您使用相同的 @user
对象来填充它,您也可以删除表单中的 hidden
字段:
<%= form_for @user, url: { action: "assign" } do |f| %>
<%= f.label :followed_id, "Assign #{@user.name} to" %>
<%= f.collection_select :following, @ders, :id, :name, prompt: true %>
<%= f.submit "Assign", class: "btn btn-primary" %>
<% end %>
更新
您需要在路线中包含 :assign
:
Rails.application.routes.draw do
root 'static_pages#home'
resources :static_pages, only: [], path: "" do
collection do
get :help, :about, :contact
end
end
resources :sessions, only: [:new, :create, :destroy], path_names: {new: "login", create: "login", destroy: "logout"}
resources :users, path_names: { new: "signup" } do
member do
get :following, :followers, :assign
end
end
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :relationships, only: [:create, :destroy]
end
原来是 Emu 和 Rich 的答案的组合。 HTML 中的表格是
<%= form_for @user, url: { action: "assign" } do |f| %>
<%= f.label :follower_id, "Assign #{@user.name} to" %>
<%= f.collection_select :following, @ders, :id, :name, prompt: true %>
<%= f.submit "Assign", class: "btn btn-primary" %>
<% end %>
有控制器动作
def assign
@scout = User.find(params[:id])
@der = User.find(params[:user][:following])
@scout.follow(@der)
redirect_to @scout
end
路线为
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users do
member do
get :following, :followers
match :assign, to: :assign, via: [:post, :patch]
end
end
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :relationships, only: [:create, :destroy]
end
此代码全部基于 Michael Hartl 在 Rails 上的 Ruby 教程。我在 users_controller
中使用
assigned
动作
def assign
puts "in assign..."
@scout = User.find(params[:follower_id])
@der = User.find(params[:followed_id])
end
我意识到这目前没有任何作用,但在 _follow
部分,我有
<%= form_for @user, url: { action: "assign" } do |f| %>
<div><%= hidden_field_tag :follower_id, @user.id %></div>
<%= f.label :followed_id, "Assign #{@user.name} to" %>
<%= f.collection_select :following, @ders, :id, :name, prompt: true %>
<%= f.submit "Assign", class: "btn btn-primary" %>
<% end %>
但我收到错误 No route matches {:action=>"assign", :controller=>"users", :id=>"4"}
。我是 rails 的新手,所以这可能只是一个愚蠢的问题。我究竟做错了什么?我需要修改我的 routes.rb
文件吗?另外,如果我尝试 <%= form_for @user do |f| %>
,控制器如何知道要使用哪个操作?是基于显示表单的动作吗?
编辑:我的 routes.rb
文件是
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users do
member do
get :following, :followers
end
end
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :relationships, only: [:create, :destroy]
end
编辑 2:@Rich 的 form_for
块生成的 HTML 是
<form class="edit_user" id="edit_user_5" action="/users/5/assign" accept-charset="UTF-8" method="post">
<input name="utf8" type="hidden" value="✓">
<input type="hidden" name="_method" value="patch">
<input type="hidden" name="authenticity_token" value="...">
<label for="user_followed_id">Assign Mr. Marley Muller to</label>
<select name="user[following]" id="user_following">
<option value="1">Example User</option>
<option value="2">Matthew Swartz</option>
<option value="3">Joseph Swartz</option>
</select>
<input type="submit" name="commit" value="Assign" class="btn btn-primary">
</form>
这就是为什么我目前收到找不到 ID 错误的原因,因为它没有发送真实 ID (edit_user_5
)
编辑 3:这是为请求传递的参数
Started PATCH "/users/6/assign" for 68.100.59.128 at 2015-12-15 03:54:39 +0000
Processing by UsersController#assign as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"...", "user"=>{"following"=>"2"}, "commit"=>"Assign", "id"=>"6"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 6]]
Unpermitted parameter: following
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", nil]]
Completed 404 Not Found in 9ms (ActiveRecord: 1.0ms)
ActiveRecord::RecordNotFound (Couldn't find User with 'id'=):
app/controllers/users_controller.rb:69:in `assign'
编辑 4:使用以下路由文件,
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users do
member do
get :following, :followers
match :assign, to: :assign, via: [:post, :patch]
end
end
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :relationships, only: [:create, :destroy]
end
以及以下用于指定一个用户关注另一个用户的表格,
<%= form_for @user, url: { action: "assign" } do |f| %>
<%= f.label :follower_id, "Assign #{@user.name} to" %>
<%= f.collection_select :following, @ders, :id, :name, prompt: true %>
<%= f.submit "Assign", class: "btn btn-primary" %>
<% end %>
我收到 Couldn't find User with 'id'=
的 ActiveRecord::RecordNotFound in UsersController#assign
错误,但使用以下参数:
{"utf8"=>"✓",
"_method"=>"patch",
"authenticity_token"=>"...",
"user"=>{"following"=>"3"},
"commit"=>"Assign",
"id"=>"4"}
ID 正确:"user"=>{"following"=>"3"}
和 "id"=>"4"
,我相信我只是试图错误地访问它们。这是用户控制器中的分配操作:
def assign
@scout = User.find(params[:id])
@der = User.find(params_hash[:followed_id])
# make scout follow der here
redirect_to @scout
end
想法?
在您的 routes.rb 文件中,您必须像这样定义特定控制器操作的路由:
resource :users do
member do
post 'assign' # change the request method to 'put'
end
end
然后它将为控制器中的 assign 方法生成一个路由,例如:users/4/assign
有关详细信息,请访问 guides
要添加到 Emu
的答案中,您的代码将不起作用...
def assign
puts "in assign..."
@scout = User.find(params[:follower_id])
@der = User.find(params[:followed_id])
end
您从 form_for
对象获得的参数结构如下:
params[:user][:follower_id]
params[:user][:followed_id]
您必须使用以下内容:
#app/controllers/users_controller.rb
class UsersController < ApplicationController
def assign
@scout = User.find params[:id]
@der = User.find params_hash[:followed_id]
end
private
def params_hash
params.require(:user).permit(:followed_id)
end
end
如果您使用相同的 @user
对象来填充它,您也可以删除表单中的 hidden
字段:
<%= form_for @user, url: { action: "assign" } do |f| %>
<%= f.label :followed_id, "Assign #{@user.name} to" %>
<%= f.collection_select :following, @ders, :id, :name, prompt: true %>
<%= f.submit "Assign", class: "btn btn-primary" %>
<% end %>
更新
您需要在路线中包含 :assign
:
Rails.application.routes.draw do
root 'static_pages#home'
resources :static_pages, only: [], path: "" do
collection do
get :help, :about, :contact
end
end
resources :sessions, only: [:new, :create, :destroy], path_names: {new: "login", create: "login", destroy: "logout"}
resources :users, path_names: { new: "signup" } do
member do
get :following, :followers, :assign
end
end
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :relationships, only: [:create, :destroy]
end
原来是 Emu 和 Rich 的答案的组合。 HTML 中的表格是
<%= form_for @user, url: { action: "assign" } do |f| %>
<%= f.label :follower_id, "Assign #{@user.name} to" %>
<%= f.collection_select :following, @ders, :id, :name, prompt: true %>
<%= f.submit "Assign", class: "btn btn-primary" %>
<% end %>
有控制器动作
def assign
@scout = User.find(params[:id])
@der = User.find(params[:user][:following])
@scout.follow(@der)
redirect_to @scout
end
路线为
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users do
member do
get :following, :followers
match :assign, to: :assign, via: [:post, :patch]
end
end
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :relationships, only: [:create, :destroy]
end