简体   繁体   中英

Ruby on Rails link_to :method => :delete syntax

This is a nub question. I have a resource Project which has_many Feeds . When I am viewing the list of nested Feeds on the Project page, I have a Delete button to remove that Feed from the Project. It works with this syntax:

<%= link_to 'Delete', [feed.project, feed], :confirm => 'Are you sure?', :method => :delete %>

But that then directs to the page listing all the Feeds, and I instead want it to redirect back to the Project page the user was just at. Is it a matter of changing [feed.project, feed] or is it something else? I don't quite understand the syntax of link_to well enough yet.

EDIT: In my feeds_controller.rb, I changed the redirect line to :back

def destroy
project = Project.find(params[:project_id])
@feed = project.feeds.find(params[:id])
@feed.destroy
redirect_to :back

respond_to do |format|
  format.html { redirect_to :back }
  format.xml  { head :ok }
end
end

You must have a look at the controller for this resource. It should be somewhere like app/controllers/projects_controller , where's there should be an action named destroy . The code that do the redirect must be in there. You'll have to change the following line:

redirect_to project_feeds_url(project)

to this

redirect_to :back

in your controller

def destroy
  @project = Project.find(params[:project_id])
  @feed = @project.feeds.find(params[:id])
  @feed.destroy
  redirect_to @project
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM