Rails 3,嵌套资源,无路由匹配[PUT]

时间:2011-09-14 16:42:11

标签: ruby-on-rails ruby forms routes

我真的很生气。我一直在寻找答案并尝试我发现的一切,包括相关的问题&这里有关于stackoverflow的答案,但仍无法使其正常工作。

我正在使用嵌套资源,但我无法使表单生效。我总是得到错误,例如没有路线匹配[PUT]“/ galleries / 1 / photos”

表格在这里:/ 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})

更新

以下是佣金路线的一部分。

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"}

2 个答案:

答案 0 :(得分:6)

您无需指定URL,这对于更新来说是错误的。尝试

<%= form_for([@gallery, @photo], :html => {:multipart => true}) do |f| %>

答案 1 :(得分:3)

回答我的问题。我没有工作的例子,但希望它有所帮助。

根据您的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