Rails 3, verschachtelte Ressource, No route matches [PUT]
Ich bin literately verrückt mit diesem. Ich habe die Suche die Antwort und versuchen, alles, was ich finden, einschließlich der damit verbundenen Fragen & Antworten hier auf stackoverflow und kann immer noch nicht funktioniert.
Arbeite ich mit einer verschachtelten Ressource und kann ich nicht machen, die form der Arbeit. Ich bekomme immer Fehler, wie No route matches [SETZEN] "/galleries/1/"Fotos"
Form ist hier: /Galerien/1/Fotos/1/edit
Routen.rb
resources :galleries do
resources :photos
end
resources :galleries
resources :photos
photos_controller.rb
def new
@gallery = Gallery.find(params[:gallery_id])
@photo = @gallery.photos.build
respond_to do |format|
format.html
end
end
def edit
@gallery = Gallery.find(params[:gallery_id])
@photo = Photo.find(params[:id])
end
def create
@gallery = Gallery.find(params[:gallery_id])
@photo = @gallery.photos.build(params[:photo])
respond_to do |format|
if @photo.save
format.html { redirect_to @photo, notice: 'Photo was successfully created.' }
else
format.html { render action: "new" }
end
end
end
def update
@gallery = Gallery.find(params[:gallery_id])
@photo = Photo.find(params[:id])
respond_to do |format|
if @photo.update_attributes(params[:photo])
format.html { redirect_to @photo, notice: 'Photo was successfully updated.' }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: @photo.errors, status: :unprocessable_entity }
end
end
end
_form.html-Code.erb
<%= form_for([@gallery, @photo], :url => gallery_photos_path(params[:gallery_id]), :html => {:multipart => true}) do |f| %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.file_field :image %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Habe ich auch schon versucht form_for([@gallery, @photo], :html => {:multipart => true})
und form_for(@photo, :url => gallery_photos_path(@gallery), :html => {:multipart => true})
UPDATE
Hier ist ein Teil der rake-Routen.
gallery_photos GET /galleries/:gallery_id/photos(.:format) {:action=>"index", :controller=>"photos"}
POST /galleries/:gallery_id/photos(.:format) {:action=>"create", :controller=>"photos"}
new_gallery_photo GET /galleries/:gallery_id/photos/new(.:format) {:action=>"new", :controller=>"photos"}
edit_gallery_photo GET /galleries/:gallery_id/photos/:id/edit(.:format) {:action=>"edit", :controller=>"photos"}
gallery_photo GET /galleries/:gallery_id/photos/:id(.:format) {:action=>"show", :controller=>"photos"}
PUT /galleries/:gallery_id/photos/:id(.:format) {:action=>"update", :controller=>"photos"}
DELETE /galleries/:gallery_id/photos/:id(.:format) {:action=>"destroy", :controller=>"photos"}
- Das ist sehr seltsam. Ich geändert, um die form zu
form_for(@photo)
auf und ging zu dieser URL: photos/1/edit und wenn ich das Formular abschicken bekomme ich die Routing-Fehler No route matches {:action=>"edit", :controller=>"Fotos"}
Du musst angemeldet sein, um einen Kommentar abzugeben.
Brauchen Sie nicht zu geben Sie die URL, die war falsch für das update sowieso. Versuchen
Antwort aus Spitze von meinem Kopf. Ich habe keine Beispiel, aber hoffe es hilft.
In der Tat, nach Ihren
rake routes
Sie nicht über eine route:Wenn ich mich nicht Irre, Ihre form Punkte, um ungültige Aktion:
gallery_photos_path
einen index der Fotos in der Galerie mit der id:gallery_id
. Ich denke, :url-parameter der form sollte so etwas wie:ODER können Sie es ohne Verwendung von Schienen Helfer:
Auch, wenn Sie versuchen, erstellen Sie verschachtelte Ressource, ich glaube nicht, dass Sie müssen diese Zeilen in der Routen-Datei:
No route matches {:action=>"show", :controller=>"photos", :format=>nil, :id=>#<Photo id: 1, title...