gpt4 book ai didi

ruby-on-rails - Rails 3,嵌套资源,没有路由匹配 [PUT]

转载 作者:数据小太阳 更新时间:2023-10-29 07:13:04 26 4
gpt4 key购买 nike

我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和 stackoverflow 上的答案,但仍然无法正常工作。

我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配 [PUT] "/galleries/1/photos"

表格在这里:/galleries/1/photos/1/edit

路线.rb

resources :galleries do
resources :photos
end
resources :galleries
resources :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})

更新

这里是部分佣金路线。

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| %>

关于ruby-on-rails - Rails 3,嵌套资源,没有路由匹配 [PUT],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7419895/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com