leo*_*nel 5 ruby forms routes ruby-on-rails
我真的很生气.我一直在寻找答案并尝试我发现的所有内容,包括有关stackoverflow的相关问题和答案,但仍无法使其正常工作.
我正在使用嵌套资源,我无法使表单工作.我总是得到错误,例如没有路线匹配[PUT]"/画廊/ 1 /照片"
表格在这里:/ galleries/1/photos/1/edit
的routes.rb
resources :galleries do
resources :photos
end
resources :galleries
resources :photos
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
_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 %>
Run Code Online (Sandbox Code Playgroud)
我也曾尝试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"}
Run Code Online (Sandbox Code Playgroud)
您无需指定URL,这对于更新来说是错误的.尝试
<%= form_for([@gallery, @photo], :html => {:multipart => true}) do |f| %>
Run Code Online (Sandbox Code Playgroud)