简体   繁体   English

Ruby on Rails link_to:method =>:delete语法

[英]Ruby on Rails link_to :method => :delete syntax

This is a nub question. 这是一个小问题。 I have a resource Project which has_many Feeds . 我有一个资源Project 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. 当我在“项目”页面上查看嵌套的源列表时,我有一个“ Delete按钮以从项目中删除该源。 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. 但这然后将其定向到列出所有提要的页面,而我希望它重定向回用户所在的Project页面。 Is it a matter of changing [feed.project, feed] or is it something else? 是更改[feed.project, feed]还是其他问题? I don't quite understand the syntax of link_to well enough yet. 我还不太了解link_to的语法。

EDIT: In my feeds_controller.rb, I changed the redirect line to :back 编辑:在我的feeds_controller.rb中,我将重定向行更改为: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 . 它应该在app/controllers/projects_controller地方,应该有一个名为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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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