我创建了一个简单的用户 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>
我相信这与我的路线及其设置方式有关。一段时间以来,我一直在寻找解决方案。我有一种感觉,它真的很容易。如您所知,我对 ruby on rails 很陌生。
谢谢!
模型用户
这是我的用户模型
# == 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