我尝试在部分内部使用if语句检查当前URL。这是我遇到的代码和问题:
<%= form_for(@post) do |f| %>
<%= if current_page? edit_post_path(f) %>
此代码不会生成显式错误,但if语句下的代码永远不会生效。如果我没有为(f)
添加edit_post_path
参数,则代码会在edit_post_path
页面上运行,但在每个其他页面上我都会丢失:id错误:
No route matches {:action=>"edit", :controller=>"posts"} missing required keys: [:id]
这是我尝试的另一种方法:
if current_page?(:controller => 'posts', :action => 'edit')
该方法适用于编辑页面,但在每个其他页面上我都会收到此错误:
No route matches {:action=>"edit", :controller=>"posts"}
但是,执行在posts控制器中有一个编辑操作。所以我的问题是:为什么编辑路线不存在于其他视图中?
注意:我用这个方法解决了这个问题:
request.path.include?('edit')