Ruby on Rails link_to :方法 => :删除语法
这是一个棘手的问题。我有一个资源 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须查看该资源的控制器。它应该位于像
app/controllers/projects_controller
这样的地方,其中应该有一个名为destroy
的操作。执行重定向的代码必须在那里。您必须将以下行更改为:
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 nameddestroy
. The code that do the redirect must be in there. You'll have to change the following line:to this
在你的控制器中
in your controller