I'm having trouble figuring out how to get an error message displayed - in fact to even get the page displayed on error.
Here's what I currently have:
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
The current problem is that when trying to create a new media record for a project with a validation problem, I get the following error:
ActionController::RoutingError in Medias#create Showing D:/test/app/views/medias/new.html.erb where line #5 raised:
No route matches {:action=>"destroy", :controller=>"projects"}
Extracted source (around line #5): 2: 3: <%= render 'form' %> 4: 5: <%= link_to 'Back', project_path(@project) %>
the url it gets to for this error is: http://localhost:3000/projects/16/medias (the url of the 'new' page is http://localhost:3000/projects/16/medias/new). Without any validation problems, the 'create' goes through. I'm able to 'edit' an existing record without issue, and that includes having a validation error displayed if required - however the 'edit' validation only occurs since I added the ':update' to the resources :medias and to the resources :medias nested in :projects. I'm not sure if that's a great idea.
Any insight into how to get this to work is greatly appreciated.
精彩评论