【问题标题】:"Template is missing" error. Missing Template Create“模板丢失”错误。缺少模板创建
【发布时间】:2013-04-14 13:24:25
【问题描述】:

我尝试在创建新文章页面的视图中显示验证错误。我在文章模型中验证了检查正文和标题的存在(验证:标题,:正文,:存在=>真)。当我保留文章和标题文本框但显示“模板丢失”错误并显示以下信息时,它不允许创建新文章。

Missing template articles/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "F:/kuta/billi/app/views" * "C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/twitter-bootstrap-rails-2.2.6/app/views" * "C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/devise-2.2.3/app/views"

我已将 放入文章的新页面的部分中,并将 gem 'dynamic_form' 放入 gemfile。

_form.html.erb for article/new.html.erb

<%= form_for @article, :html => { :class => '' } do |f| %>
<%= f.error_messages %>
  <div>
    <%= f.label :title, :style => "margin-top:10px;" %>
    <div>
      <%= f.text_field :title, :style => "width:730px; height:30px; border: 1px solid #66c9ee;margin-top:10px; background-color:#FFFFFF;" %>
    </div>
  </div>
  <div>
    <%= f.label :body, :class => 'control-label' %>
   <%= f.text_area :body, :style => "width:730px; height:250px; border: 1px solid #66c9ee;margin-top:10px; background-color:#FFFFFF;" %>
  <%= f.label :tag_list, :style => "margin-top:10px" %><br />
  <%= f.text_field :tag_list, :style => "width:730px; height:30px; border: 1px solid #66c9ee;margin-top:0px; background-color:#FFFFFF;" %>
  <div style="margin-top: 20px">
    <%= f.submit nil, :class => 'btn btn-primary' %>
    <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
                articles_path, :class => 'btn' %>
  </div>
<% end %>

article.rb 模型

class Article < ActiveRecord::Base
   attr_accessible :title, :body
   attr_accessible :tag_list
   has_many :comments
   belongs_to :user
   has_many :taggings
   has_many :tags, through: :taggings
   validates :title, :body, :presence => true


   def tag_list
    self.tags.collect do |tag|
     tag.name
    end.join(", ")
   end

   def tag_list=(tags_string)
    tag_names = tags_string.split(",").collect{|s| s.strip.downcase}.uniq
    new_or_found_tags = tag_names.collect { |name| Tag.find_or_create_by_name(name) }
    self.tags = new_or_found_tags
   end
end

你能帮我吗,我错了。

【问题讨论】:

    标签: ruby-on-rails ruby validation views


    【解决方案1】:

    当验证失败时,在您的控制器操作中,您应该使用 render 'new' 而不是 render 'create' 再次呈现新模板

    【讨论】:

    • 嗨 zippie,你的建议对我有用。谢谢。但验证错误出现时没有任何颜色(基本上它应该是红色的)。任何想法如何给它们上色。谢谢。
    • 当它们显示出来时,右键单击 -> 检查元素并查看包裹在它们周围的 div 具有什么类或 id。一旦你发现用 css 把它们变成红色