当前位置:  开发笔记 > 编程语言 > 正文

Rails代码中没有路由匹配

如何解决《Rails代码中没有路由匹配》经验,为你挑选了1个好方法。

这段代码全部基于Michael Hartl的Ruby on Rails教程.我创建了一个assigned在我的动作users_controller

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| %>
  
<%= hidden_field_tag :follower_id, @user.id %>
<%= 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 是

这是有道理的,为什么我目前得到一个找不到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 %>

我收到ActiveRecord::RecordNotFound in UsersController#assign错误Couldn't find User with 'id'=,但有以下参数:

{"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

思考?



1> Emu..:

routes.rb文件中,您必须为特定控制器操作定义路由,如下所示:

resource :users do 
  member do
    post 'assign' # change the request method to 'put'
  end
end

然后它将在控制器中为您的assign方法生成一个路径,如:users/4/assign

有关更多信息,请访问指南

推荐阅读
落单鸟人
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有