Ruby on Rails link_to :方法 => :删除语法

发布于 2024-12-02 10:40:27 字数 756 浏览 0 评论 0原文

这是一个棘手的问题。我有一个资源 Project,其中 has_many Feeds。当我在“项目”页面上查看嵌套 Feed 列表时,我可以使用 Delete 按钮从项目中删除该 Feed。它使用以下语法:

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

但随后会定向到列出所有提要的页面,而我希望它重定向回用户刚刚所在的项目页面。是更改 [feed.project, feed] 的问题还是其他原因?我还不太了解 link_to 的语法。

编辑: 在我的 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幽梦紫曦~ 2024-12-09 10:40:27

您必须查看该资源的控制器。它应该位于像 app/controllers/projects_controller 这样的地方,其中应该有一个名为 destroy 的操作。执行重定向的代码必须在那里。您必须将以下行更改

redirect_to project_feeds_url(project)

为:

redirect_to :back

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
感悟人生的甜 2024-12-09 10:40:27

在你的控制器中

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

in your controller

def destroy
  @project = Project.find(params[:project_id])
  @feed = @project.feeds.find(params[:id])
  @feed.destroy
  redirect_to @project
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文