路由错误没有路由匹配[POST]“ / user / register”

时间:2019-10-08 14:43:45

标签: ruby-on-rails ruby routing routes

我正在尝试为我的Rails网站注册用户。当我单击注册按钮时,我得到以下提示:

 Routing Error
 No route matches [POST] "/user/register"

这是我的user_controller.rb文件:

class UserController < ApplicationController


  def index
    @title = "RailsSpace User Hub"
  end

  def register
    @title = "Register"
    if request.post? and params[:user]
      @user = User.new(params[:user])
      if @user.save
        flash[:notice] = "User #{@user.screen_name} created!"
        redirect_to :action => "index"
      end
    end
  end
end

这是我的config / routes.rb文件:

Rails.application.routes.draw do
  get 'user/index'
  get 'user/register'
  get  'site/index'
  get '/site/about'
  get 'site/help'
  get 'users/about', to: 'user#about'
  get 'users/help', to: 'user#help'

  root  'site#index'
end

这是我的user / register.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 %>

非常感谢您提前提供的所有帮助!

2 个答案:

答案 0 :(得分:0)

您有get 'user/register',它只匹配GET请求,如果您只想匹配POST做post 'user/register',如果您想同时匹配GET和POST做match 'user/register', via: [:get, :post]

答案 1 :(得分:0)

尝试将获取信息更改为帖子:

## config/routes.rb

Rails.application.routes.draw do
  get 'user/index'
  post 'user/register'
  get  'site/index'
  get '/site/about'
  get 'site/help'
  get 'users/about', to: 'user#about'
  get 'users/help', to: 'user#help'

  root  'site#index'
end