Michael Hartl的未定义sign_up方法:练习5

时间:2014-03-26 07:24:51

标签: ruby-on-rails ruby ruby-on-rails-3.2

点击主页上的“立即注册”后,我无法进入注册页面。我已经完成了堆栈溢出,但没有一个能解决问题。这是我的代码:

的routes.rb

get "users/new"
root :to => 'static_pages#home'

  match '/static_pages/home', :to => 'static_pages#home'
  match '/static_pages/help', :to => 'static_pages#help'
  match '/static_pages/about', :to => 'static_pages#about'
  match '/static_pages/contact', :to => 'static_pages#contact'
  match '/users/new', :to => 'users#new' 

home.html.erb

<div class="center hero-unit">
  <h1>Welcome to the Sample App</h1>

  <h2>
    This is the home page for the
    <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
    sample application.
  </h2>

  <%= link_to "Sign up now!", "signup_path", class: "btn btn-large btn-primary" %>
</div>

<%= link_to image_tag("rails.png", alt: "Rails"), 'http://rubyonrails.org/' %>

header.html.erb

<header class="navbar navbar-fixed-top navbar-inverse">
  <div class="navbar-inner">
    <div class="container">
      <%= link_to "sample app", root_path, id: "logo" %>
      <nav>
        <ul class="nav pull-right">
          <li><%= link_to "Home", 'home' %></li>
          <li><%= link_to "Help", 'help' %></li>
          <li><%= link_to "Sign in", 'new' %></li>
        </ul>
      </nav>
    </div>
  </div>
</header>

users_controller.rb

class UsersController < ApplicationController
  def new
  end
end

我长期坚持这一点,无法为此找到解决方案。

3 个答案:

答案 0 :(得分:1)

编辑:尝试将此添加到您的routes.rb

match '/signup',  to: 'users#new',            via: 'get'

答案 1 :(得分:1)

我想你想用

match '/users/new', :to => 'users#new' 

作为signup路线。您可以使用:as参数指定此内容,如下所示:

match '/users/new', :to => 'users#new', :as => 'signup'

将为您提供正确的signup_path

请注意,根据Rails routing docs,您现在应该使用getpost代替match,或指定:via => [:post]以禁止其他查询参数。顺便说一句:您的signup应该是:post,因为它会尝试更改服务器的状态。

答案 2 :(得分:1)

match '/signup',  to: 'users#new'

这对我来说很好,我不知道我错过了什么