0

当我提交下面的 new.html.erb 时,http://localhost:3000/user/new_user_by_admin它会显示错误:没有路由匹配“/user/new_user_by_admin”与 {:method=>:post}。我希望将此页面提交给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

帮助!!

4

2 回答 2

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 匹配

于 2013-01-22T05:51:39.547 回答
0
map.new_user_by_admin '/new_user_by_admin', :controller => 'users', :action => 'new_user_by_admin' 

我为解决 routes.rb 文件中的上述问题所做的更改

于 2013-01-23T06:25:47.630 回答