我无法弄清楚如何显示错误消息 - 事实上甚至会在错误时显示页面。
以下是我目前的情况:
media.rb
class Media < ActiveRecord::Base
belongs_to :project
has_attached_file :media_file, :default_url => '/media_file/missing.png'
validates :description, :presence => true
validates_attachment_content_type :media_file, :content_type => ['image/gif', 'image/jpg', 'image/jpeg', 'image/png', 'audio/mpeg', 'audio/wav']
MEDIA_TYPES = ["Image", "Audio", "Video"]
end
_form.html.erb
<%= form_for [@project, @media], :html => {:multipart => true} do |f| %>
<% if @media.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@media.errors.count, "error") %> prohibited this media from being saved:</h2>
<ul>
<% @media.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
.
.
.
<% end %>
的routes.rb
resources :medias, :only => [:destroy, :update]
resources :projects do
resources :medias, :only => [:create, :edit, :index, :new, :show, :update]
end
medias_controller.rb
def new
@project = Project.find params[:project_id]
@media = Media.new(:project=>@project)
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @media }
end
end
def create
@media = Media.new(params[:media])
@media.project_id = params[:project_id]
respond_to do |format|
if @media.save
format.html { redirect_to(@media.project, :notice => 'Media was successfully created.') }
format.xml { render :xml => @media, :status => :created, :location => @media }
else
format.html { render :action => "new" }
format.xml { render :xml => @media.errors, :status => :unprocessable_entity }
end
end
end
def update
@media = Media.find(params[:id])
respond_to do |format|
if @media.update_attributes(params[:media])
format.html { redirect_to(@media.project, :notice => 'Media was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @media.errors, :status => :unprocessable_entity }
end
end
end
当前的问题是,在尝试使用验证问题为项目创建新的 media 记录时,出现以下错误:
ActionMedtroller :: Medias中的RoutingError #create 显示D:/test/app/views/medias/new.html.erb,第5行引出:
没有路线匹配{:action =&gt;“destroy”,:controller =&gt;“projects”}
提取的来源(第5行): 2: 3:&lt;%= render'形式'%&gt; 4: 5:&lt;%= link_to'Back',project_path(@project)%&gt;
此错误导致的网址为:http://localhost:3000/projects/16/medias(“新”页面的网址为http://localhost:3000/projects/16/medias/new)。没有任何验证问题,“创造”就会通过。我能够“编辑”现有记录而不会出现问题,其中包括在需要时显示验证错误 - 但是“编辑”验证仅在我将“:update”添加到资源:medias和资源时发生:medias嵌套在:projects中。我不确定这是不是一个好主意。
非常感谢任何有关如何使其发挥作用的见解。