错误地设置路线?

时间:2013-10-06 08:07:00

标签: ruby-on-rails ruby routes

我创建了一个简单的用户MVC,人们可以输入他们的电子邮件以加入我们的邮件列表。它似乎工作得很好。唯一的问题是我想在主页上显示视图。我想简单地把<%= render'users / new'%>在我的index.html.erb页面中但它一直给我一个错误

 Missing partial users/new with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb,       :builder, :coffee]}. Searched in:
  * "/Users/jeff/Desktop/selfstarter/app/views"
Extracted source (around line #4):

1: <%= render 'preorder/homepage/show_dont_tell' %>  
2: <%= render 'preorder/homepage/key_points' %>
3: <%= render 'preorder/homepage/other_points' %>
4: <%= render 'users/new' %>
5: 
6: <%#<%= render 'preorder/homepage/middle_reserve' %>  
7: <%#<%= render 'preorder/homepage/faqs' %>

我听说过我可能想把它变成局部但是如何将我的视图转换成可用的部分?

这是我的路线档案

    Selfstarter::Application.routes.draw do
   resources :users

  match '/signup',  to: 'users#new'
  get "users/new"
  get "static_pages/about"
  get "static_pages/FAQ"

  root :to => 'preorder#index'
  match '/preorder'               => 'preorder#index'
  get 'preorder/checkout'
  match '/preorder/share/:uuid'   => 'preorder#share', :via => :get
  match '/preorder/ipn'           => 'preorder#ipn', :via => :post
  match '/preorder/prefill'       => 'preorder#prefill'
  match '/preorder/postfill'      => 'preorder#postfill'



end

这是我的users_controller文件

    class UsersController < ApplicationController

  def new
    @user = User.new
  end
  def create
    @user = User.new(params[:user])
    if @user.save
        UserMailer.registration_confirmation(@user).deliver
        flash[:success] = "Welcome to the Island Mailing List!"
      redirect_to root_path 
    else
      render 'new'
    end
  end
end

这是我的user / new.html.erb文件

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="shortcut icon" href="../../assets/ico/favicon.png">

    <title>Jumbotron Template for Bootstrap</title>

    <!-- Bootstrap core CSS -->
    <link href="../../dist/css/bootstrap.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="jumbotron.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="../../assets/js/html5shiv.js"></script>
      <script src="../../assets/js/respond.min.js"></script>
    <![endif]-->
  </head>

  <body>
<div class= "jumbotron">
    <div class="container">
        <div class="row">
                <div class="col-lg-6">
    <%= form_for(@user) do |f| %>
<%= render 'shared/error_messages' %>

      <div class="input-group">
      <%= f.text_field :email, class: "form-control", placeholder: "YouEmailAdress@gmail.com" %>
    <span class="input-group-btn">
      <%= f.submit "Follow Project", class: "btn btn-large btn-primary" %></span>
    <% end %>

                        </div><!-- /input-group -->
                </div><!-- /.col-lg-6 -->
        </div><!-- /.row -->
    </div><!--/container -->
</div><!-- /jubotron -->





    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->


  <script src="../../assets/js/jquery.js"></script>
    <script src="../../dist/js/bootstrap.min.js"></script>
  </body>
</html>

我认为这与我的路线及其设置方式有关。我一直在寻找解决方案。我觉得它很容易。正如你所知道的那样,我对铁轨上的红宝石很新。

谢谢!

模型用户

这是我的用户模型

# == Schema Information
#
# Table name: users
#
#  id         :integer          not null, primary key
#  email      :string(255)
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

class User < ActiveRecord::Base
  attr_accessible :email

  before_save { |user| user.email = email.downcase }

  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, length: { maximum:80 }, 
            format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }

end

2 个答案:

答案 0 :(得分:0)

部分必须以下划线为前缀。这是一个惯例。因此,_new.html.erb文件夹中应该有一个名为app/views/users/的文件。

答案 1 :(得分:0)

rails在错误消息方面并不是很好。就像@Agis已经回答的那样,真正的问题是,它正在寻找以下划线开头的文件。

如果您在视图中使用render方法,则默认情况下会查找partials。请查看此处的文档:http://apidock.com/rails/ActionView/Helpers/RenderingHelper/render

这与在动作中调用render不同,其中视图模板将使用布局呈现。

在任何情况下,您通常不会仅仅将视图重新用作部分视图。这样做非常罕见,因此您将视图的某些部分提取为部分视图并重新使用,即登录表单或类似内容。

我希望这能帮助您对rails有一个基本的了解。

相关问题