2013-01-22 130 views
0

當我提交下面的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。我需要做什麼改變?用戶控制器在rails中沒有路由匹配2.3.8

<% 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("https://stackoverflow.com/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 

幫助!!

+0

請使用rake檢查:路線 –

回答

0

你的配對是錯誤的。它應該是:

當你這樣做:

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

你相匹配的行動網址本地主機:3000/new_user_by_admin,而不是你真正想要的。請嘗試以下方法之一:

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

將匹配您的控制器動作爲localhost:3000 /用戶/ new_user_by_admin

+0

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

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

這改變了我都做了上述問題中的routes.rb文件中的解決方案

相關問題