twitter-bootstrap 没有路由匹配 {:action=>"show", :controller=>"users"}

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14731654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 16:17:12  来源:igfitidea点击:

No route matches {:action=>"show", :controller=>"users"}

ruby-on-railsruby-on-rails-3formstwitter-bootstrapcontroller

提问by holyredbeard

I'm trying to implement a Twitter Boostrap login form, that's gonna be used on every page (because the navigation bar is a part of the layout).

我正在尝试实现一个 Twitter Boostrap 登录表单,它将在每个页面上使用(因为导航栏是布局的一部分)。

However, when trying the code below I get the following error:

但是,在尝试下面的代码时,我收到以下错误:

No route matches {:action=>"show", :controller=>"users"}

User controller:

用户控制器:

class UsersController < ApplicationController

  def index
    @users = User.all
  end

  def show
    ...
  end

  def login
    ...
  end

end

_navigation.html.erb:

_navigation.html.erb:

<div class="dropdown-menu" style="padding: 15px; padding-bottom: 0px;">
    <%= form_for("user", :url => user_path) do |f| %>
        <%= f.label :email%>
        <%= f.text_field(:email, :size => 30, :class => 'login_field', :placeholder => 'Anv?ndarnamn')%>
        <%= f.label :password%>
        <%= f.text_field(:password, :size => 30, :class => 'login_field', :placeholder => 'L?senord')%>

        <%= f.submit "Logga in", :class => 'login_submit btn btn-primary' %>
    <% end %>
</div>

config/routes.rb:

配置/routes.rb:

get "home/index"

resources :users
resources :projects
resources :tickets

root :to => 'home#index'

rake routes (that has to do with users):

rake 路由(与用户有关):

    users GET    /users(.:format)             users#index
          POST   /users(.:format)             users#create
 new_user GET    /users/new(.:format)         users#new
edit_user GET    /users/:id/edit(.:format)    users#edit
     user GET    /users/:id(.:format)         users#show
          PUT    /users/:id(.:format)         users#update
          DELETE /users/:id(.:format)         users#destroy

I'm new to Rails but find it strange that it complains that the route doesn't exist because the action "show" is to be found inside the user controller.

我是 Rails 的新手,但发现它抱怨路由不存在很奇怪,因为要在用户控制器中找到“显示”操作。

The other thing I'm wondering about is why it looks for the action "show", while it should be "login" in this case?

我想知道的另一件事是为什么它寻找动作“show”,而在这种情况下它应该是“login”?

Why is this happening and what shall I do?

为什么会发生这种情况,我该怎么办?

回答by jvnill

your error is in this line

你的错误在这一行

<%= form_for("user", :url => user_path) do |f| %>

user_pathis expecting an id. if you change that to users_path, that should fix it but I don't think that's your intention.

user_path期待身。如果您将其更改为users_path,那应该可以解决它,但我认为这不是您的意图。

UPDATE: to use the loginaction on the users controller, you need to update your routes

更新:要使用login用户控制器上的操作,您需要更新您的路线

resources :users do
  post :login, on: :collection, as: :login
end

passing the :asoption creates a named_route for you called login_users_pathwhich you can use on your form_for. and since we wanted to do a post, we also need to specify that in the form_for

传递该:as选项会为您创建一个 named_route login_users_path,您可以在您的form_for. 并且因为我们想做一个帖子,所以我们还需要在form_for

<%= form_for("user", :url => login_users_path, :html => { :method => :post }) do |f| %>

回答by Manoj Monga

Update your routes.rb to look like:

更新你的 routes.rb 看起来像:

get "home/index"

resources :users do
  post :login, :on => :collection
end

resources :projects
resources :tickets

root :to => 'home#index'

and in your view file change the form_for line to be:

并在您的视图文件中将 form_for 行更改为:

<%= form_for("user", :url => login_users_path) do |f| %>

回答by Rahul Tapali

resources :usersonly adds default routes. If you want to add new action (other then defaults) you need to use 'collection. And you can specify the method getor post. After adding to routes.rb. You can get the pathby running rake routesthen you add the correct route in the actionof form.

resources :users只添加默认路由。如果要添加新操作(除默认值之外),则需要使用 ' collection。您可以指定方法getpost. 添加到routes.rb. 您可以path通过运行获得,rake routes然后在action表单中添加正确的路由。

   resources :users, :collection => {:login => :post}