[英]Rails 3, nested resource, No route matches [PUT]
我真的很生氣。 我一直在尋找答案並嘗試我發現的所有內容,包括有關stackoverflow的相關問題和答案,但仍無法使其正常工作。
我正在使用嵌套資源,我無法使表單工作。 我總是得到錯誤,例如沒有路線匹配[PUT]“/畫廊/ 1 /照片”
表格在這里:/ galleries / 1 / photos / 1 / edit
的routes.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.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 %>
我也嘗試過form_for([@gallery, @photo], :html => {:multipart => true})
和form_for(@photo, :url => gallery_photos_path(@gallery), :html => {:multipart => true})
UPDATE
這是耙路線的一部分。
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"}
您無需指定URL,這對於更新來說是錯誤的。 嘗試
<%= form_for([@gallery, @photo], :html => {:multipart => true}) do |f| %>
回答我的問題。 我沒有工作的例子,但希望它有所幫助。
根據您的rake routes
,您沒有路線:
[PUT] "/galleries/1/photos"
如果我沒有弄錯,您的表單指向無效操作: gallery_photos_path
將返回圖片庫中的照片索引,其中包含id :gallery_id
。 我認為,:表單的url參數應該是這樣的:
:url => gallery_photo_path(params[:gallery_id], params[:id])
或者您可以在不使用Rails助手的情況下指定它:
:url => "/galleries/#{params[:gallery_id]}/photos/#{params[:id]}"
此外,如果您嘗試創建嵌套資源,我認為您的路由文件中不需要這些行:
resources :galleries
resources :photos
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.