简体   繁体   中英

Sinatra easily get current path

I am using Sinatra and in erb, every time I had to generate a link, I have to hardcode it like this:

<li><a href=<%="/admin/users?page=#{i}"%>><%=i%></a></li>

Is there an easier, more "scalable" way to get the string "/admin/users?page=" and do this?

Look at this and make a helper .

# Assuming you're building a modular sinatra app but its not required.
require 'sinatra/base'

module Sinatra
  module UserLinkHelper
    def user_url(id)
      url("/admin/users?page=" + id.to_s)
    end
  end

  helpers UserLinkHelper
end

# Assuming you're using haml in your view, once again not required
%a{:href => user_url(i)}

I didn't test this but this should encompass the idea you're looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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