MyException - 我的异常网
当前位置:我的异常网» 操作系统 » rails出现No route matches [POST] "/user/reg

rails出现No route matches [POST] "/user/register"异常

www.MyException.Cn  网友分享于:2013-02-20  浏览:25次
rails出现No route matches [POST] "/user/register"错误

(如果不了解题意,请查看上一篇文章:搭建rails项目)

问题是这样的:

index.html.erb文件内容:

<h2>Register</h2>
<%= form_for :user do |form| %>
	<fieldset>
		<legend>Enter Your Details</legend>
		<div class="form_row">
			<label for="screen_name">Screen name:</label>
			<%= form.text_field :screen_name, :size => User::SCREEN_NAME_SIZE, :maxlength => User::SCREEN_NAME_MAX_LENGTH %>
		</div>
		<div class="form_row">
			<label for="email">Email:</label>
			<%= form.text_field :email, :size => User::EMAIL_SIZE, :maxlength => User::EMAIL_MAX_LENGTH %>
		</div>
		<div class="form_row">
			<label for="password">Password:</label>
			<%= form.password_field :password, :size => User::PASSWORD_SIZE, :maxlength => User::PASSWORD_MAX_LENGTH %>
		</div>
		<div class="form_row">
			<%= submit_tag "Register!", :class => "submit" %>
		</div>
	</fieldset>
<% end %>

application.html.erb文件内容如下:

<!DOCTYPE html>
<html>
<head>
  <title><%= @title %></title>
  <%= stylesheet_link_tag    "application", :media => "all" %>
  <%= stylesheet_link_tag    "site" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>
</head>
<body>

<%= yield %>
</body>
</html>

user.rb文件内容如下:(可选,由于index.html文件对文本框的大小进行了限制,所以这里附上这个文件)

class User < ActiveRecord::Base
  # attr_accessible :title, :body
  SCREEN_NAME_MIN_LENGTH = 4
  SCREEN_NAME_MAX_LENGTH = 20
  PASSWORD_MIN_LENGTH = 4
  PASSWORD_MAX_LENGTH = 20
  EMAIL_MAX_LENGTH = 50
  SCREEN_NAME_RANGE = SCREEN_NAME_MIN_LENGTH..SCREEN_NAME_MAX_LENGTH
  PASSWORD_RANGE = PASSWORD_MIN_LENGTH..PASSWORD_MAX_LENGTH
  SCREEN_NAME_SIZE = 20
  PASSWORD_SIZE = 10
  EMAIL_SIZE = 30
end
访问:http://localhost:3000/site/index

点击“Register”进行注册,出现错误“Routing Error   No route matches [POST]”

跳转到的页面如下:

如何解决这样的问题呢,我查看了routes.rb文件,文件如下:

Ch14::Application.routes.draw do
  get "user/index"

  get "user/register"

  # The priority is based upon order of creation:
  # first created -> highest priority.

  # Sample of regular route:
  #   match 'products/:id' => 'catalog#view'
  # Keep in mind you can assign values other than :controller and :action

  # Sample of named route:
  #   match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
  # This route can be invoked with purchase_url(:id => product.id)

  # Sample resource route (maps HTTP verbs to controller actions automatically):
  #   resources :products

  # Sample resource route with options:
  #   resources :products do
  #     member do
  #       get 'short'
  #       post 'toggle'
  #     end
  #
  #     collection do
  #       get 'sold'
  #     end
  #   end

  # Sample resource route with sub-resources:
  #   resources :products do
  #     resources :comments, :sales
  #     resource :seller
  #   end

  # Sample resource route with more complex sub-resources
  #   resources :products do
  #     resources :comments
  #     resources :sales do
  #       get 'recent', :on => :collection
  #     end
  #   end

  # Sample resource route within a namespace:
  #   namespace :admin do
  #     # Directs /admin/products/* to Admin::ProductsController
  #     # (app/controllers/admin/products_controller.rb)
  #     resources :products
  #   end

  # You can have the root of your site routed with "root"
  # just remember to delete public/index.html.
  # root :to => 'welcome#index'

  # See how all your routes lay out with "rake routes"

  # This is a legacy wild controller route that's not recommended for RESTful applications.
  # Note: This route will make all actions in every controller accessible via GET requests.
  # match ':controller(/:action(/:id))(.:format)'
end

把最后一行代码 match ':controller(/:action(/:id))(:format)' 的注释去掉。

ok,再次点击的时候就不会跳转到错误界面了,而是不进行跳转,这是正常情况。

本文是 踏雁寻花 原创, 转载请注明出处:http://blog.csdn.net/tayanxunhua/article/details/8581802

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有