Rails link_to_if problems
I have a helper method designed generate a link to a certain path depending on the current page the user is on. Basically, site wide the link should point to items_path, unless the user is on the user page. So, I'm trying to work out some DRY logic, but I keep running into trouble:
def items_link(title, options = {}, html_options = {})
  path = request.path
  case path
  when users_path,items_path
    options = request.parameters.merge(options)
  end
  link_to_if(path == users_path, title, users_path(options), html_options) do
    link_to(title, items_path(options), html_options)
  end
end
With this solution items_path throws a No route matches error, although the route is correct. users_path works fine, until I switch around the link_to_if path with link_to's. 
link_to_if(path == items_path, title, items_path(options), html_options) do
  link_to(title, users_path(options), html_options)
end
So I am guessing my problem is somewhere in link_to_if. Am I close? My current wor开发者_C百科king solution is:
def items_link(title, options = {}, html_options = {})
path = request.path
case path
when users_path
  options = request.parameters.merge(options)
  link_to(title, users_path(options), html_options)
when items_path
  options = request.parameters.merge(options)
  link_to(title, items_path(options), html_options)
else
  link_to(title, users_path(options), html_options)
end
end
This works fine, it's just ugly.
Update:
I spent some more time and figured I had to break it out a little more, and this actually helped me in another area, I like having the link_action helper.
  def items_link(title, options = {}, html_options = {})
    link_to(title, items_link_action(options), html_options)
  end
  def items_link_action(options = {})
    path = request.path
    case path
    when users_path,items_path
      options = request.parameters.merge(options)
    end
    if path == users_path
      users_path(options) 
    else
      items_path(options)
    end
  end
Does it make sense to be throwing the same options into either link? That may be the source of your No Route Matches error.
I would look at the current_page? helper and the link_to_unless_current helpers.
Something like this would show a link to the users index action unless they are already at the users index action. The same could be done for the items page. Not sure if this is what you were looking for though. If it were me I'd just drop two links in my layout or a shared partial:
<%= link_to_unless_current "Users", users_path %>
<%= link_to_unless_current "Items", items_path %>
This is as close as I got, works pretty well I think.
def items_link(title, options = {}, html_options = {})
  link_to(title, items_link_action(options), html_options)
end
def items_link_action(options = {})
  path = request.path
  case path
  when users_path,items_path
    options = request.parameters.merge(options)
  end
  if path == users_path
    users_path(options) 
  else
    items_path(options)
  end
end
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论