我目前正在尝试构建一个简单的表单,该表单采用电子邮件地址并将其作为登录页面的一部分放入数据库中。表单位于 home.html.erb
上,由 pages
Controller 控制。
我收到以下错误:
Missing template signup/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/Users/me/rails_projects/this_project/app/views"
这是我的代码:
home.html.erb
有:
<%= form_for(@signup) do |f| %>
<div class="field">
<%= f.text_field :email %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
Pages
Controller 有:
class PagesController < ApplicationController
def home
@title = "Title of my site"
@signup = Signup.new
end
end
Signups
Controller 有:
class SignupController < ApplicationController
def show
@signup = Signup.new
end
def new
end
def create
@signup = Signup.new(params[:signup])
if @signup.save
else
render 'new'
end
end
end
Signup
模型有:
class Signup < ActiveRecord::Base
attr_accessible :email
email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates(:email, :presence => true,
:length => {:maximum => 40},
:format => {:with => email_regex})
end
我不确定出了什么问题,但是当我在字段中输入有效的电子邮件地址时,这是我收到的错误。我用谷歌搜索无济于事。非常感谢您的帮助。
最佳答案
它告诉您缺少模板。
通常,默认情况下(如果您不调用 render()
或 redirect_to()
)Rails 会呈现与您的操作关联的 View ,因此在您的情况下是:create.html.erb
。
因此,如果您成功创建用户,您应该重定向到更有意义的内容。
喜欢:
if @signup.save
redirect_to :root
else
#...
end
顺便说一下,我假设您首先配置了根路径,如下所示:
root :to => "something"
您会将用户重定向到对您的应用程序有意义的位置。
关于ruby-on-rails - 缺少注册/创建、应用/创建的模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10464332/