在rails 2.3.8中没有匹配的路由

时间:2013-01-22 05:41:35

标签: ruby-on-rails routes

当我提交以下new.html.erb时,它会转到http://localhost:3000/user/new_user_by_admin并显示错误:没有路由与{:method =>:post}匹配“/ user / new_user_by_admin”。我希望将此页面提交给http://localhost:3000/users/new_user_by_admin。我需要做哪些改变?

<% form_for :user,:url => url_for { :controller => :user ,:action => :new_user_by_admin, :method => :post }  do |f| %>
    <p><%= f.label :login %><br/>
            <%= f.text_field :login %></p>

            <p><%= f.label :name %><b`enter code here`r/>
            <%= f.text_field :name %></p>

            <p><%= f.label :email %><br/>
            <%= f.text_field :email %></p>

            <p><%= f.label :password %><br/>
            <%= f.password_field :password %></p>

            <p><%= f.label :password_confirmation, 'Confirm Password' %><br/>
            <%= f.password_field :password_confirmation %></p>
            <p><%= submit_tag 'Sign up' %></p>
        <% end %>

用户控制器的代码是:

class UsersController < ApplicationController
def new_user_by_admin
      @user=User.new(params[:user])
      @user.save
      redirect_to("/users/")
  end
end
routes.rb的

代码是:

ActionController::Routing::Routes.draw do |map|
  map.home '' , :controller => 'users', :action => 'index'
  map.logout '/logout', :controller => 'sessions', :action => 'destroy'
  map.login '/login', :controller => 'sessions', :action => 'new'
  map.register '/register', :controller => 'users', :action => 'create'
  map.signup '/signup', :controller => 'users', :action => 'new'
  map.adduser '/new_user_by_admin', :controller => 'users', :action => 'new_user_by_admin', :method => :post

  map.resources :users
  map.resource :session
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
end

帮助!!

2 个答案:

答案 0 :(得分:0)

你的匹配错了。它应该是:

当你这样做时:

map.adduser '/new_user_by_admin', :controller => 'users', :action => 'new_user_by_admin', :method => :post

您将操作与url localhost:3000 / new_user_by_admin匹配,而不是您真正想要的操作。试试下面的一个:

map.adduser '/users/new_user_by_admin', :controller => 'users', :action => 'new_user_by_admin', :method => :post

将您的控制器操作与localhost:3000 / users / new_user_by_admin

匹配

答案 1 :(得分:0)

map.new_user_by_admin '/new_user_by_admin', :controller => 'users', :action => 'new_user_by_admin' 

我为routes.rb文件中的上述问题的解决方案做了这些更改