Remove nested attribute rails -
i have post model
has_many photos
the post model has
accepts_nested_attributes_for :photos
in post edit form show images associated post
<% photo in @post.photos %> <%= image_tag(photo.avatar.url(:thumb)) %> <%= link_to "remove photo", post_photo_path(@post, photo), :method => :delete %> <% end %>
when hovering on over remove photo link path looks so
http://localhost:3000/posts/17/photos/45
i uninitialized constant photoscontroller, that's expected there isn't one.
my question create 1 , have destroy action in there delete photo.. way go or there better way?
edit
photos controller
def destroy @photo = photo.find(params[:id]) @photo.destroy redirect_to edit_post_path, :notice => "successfully deleted photo" end
routes
resources :posts resources :photos resources :departments end
rake routes
edit_post /posts/:id/edit(.:format)
thanks
my opinion best thing create photoscontroller
handle destroy action. way preserve single responsibility principle, , have less workaround adding custom routes.
also, there option put 1 action in postscontroller
if sure won't adding additional actions connected photos (like tagging photos, or whatever).
it's you, believe first 1 easier.
if going second option, sure create route destroy photo action in postscontroller
, link should pointing @ exact action.
your routes.rb
:
resources :posts resources :photos, :only => [:destroy] resources :departments end
Comments
Post a Comment