开发者

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 开发者_如何学Goa 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜