【问题标题】:Github-like username url类似 Github 的用户名 url
【发布时间】:2012-03-23 10:05:53
【问题描述】:

我有这条路线可以让我为像这样的用户构建自定义 url /thisismyname,它工作正常。但是当我查看日志时 我不喜欢的东西...

当我点击 /gregory 这就是发生的事情: 1. 前往 public_profile#public # 好 2. 点击 /assets 并尝试使用资产令牌找到用户 # 不好

我认为我的限制可以避免这种情况,但似乎不是这样......

class PublicProfileConstraint
  def self.matches?(request)
     !['assets', 'admin'].include?(request.session[:token])
  end
end

get "/:token" => "profiles#public", :as => :public_profile,
    :constraints => PublicProfileConstraint

这是日志:

Started GET "/gregory" for 127.0.0.1 at 2012-03-05 12:44:43 -0800
  Processing by ProfilesController#public as HTML
  Parameters: {"token"=>"gregory"}
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE
"users"."id" = ? LIMIT 1  [["id", 1]]
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE
"users"."token" = 'gregorymarcilhacy' LIMIT 1
Rendered profiles/_modal.haml (0.1ms)
....
Rendered profiles/show.haml within layouts/application (154.7ms)
Completed 200 OK in 431ms (Views: 174.0ms | ActiveRecord: 5.5ms)

... Redering js files ...

# I DONT WANT THIS
Started GET "/assets/" for 127.0.0.1 at 2012-03-05 12:44:45 -0800
Served asset  - 404 Not Found (10ms)
  Processing by ProfilesController#public as */*
  Parameters: {"token"=>"assets"}
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE
"users"."id" = ? LIMIT 1  [["id", 1]]
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE
"users"."token" = 'assets' LIMIT 1
Redirected to http://localhost:3000/
Completed 302 Found in 312ms

... Rendering images ...

# AND I DONT WANT THIS
Started GET "/" for 127.0.0.1 at 2012-03-05 12:44:45 -0800
  Processing by LandingController#landing as */*
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE
"users"."id" = ? LIMIT 1  [["id", 1]]
Rendered landing/landing.haml within layouts/landing (0.8ms)
Completed 200 OK in 288ms (Views: 23.5ms | ActiveRecord: 2.2ms)

【问题讨论】:

  • 你能说一下为什么你不想要这些东西并详细说明你想要做什么吗?如果你这样做,也许我可以帮忙。
  • 我不想要这个,因为 /assets 是资产管道的路径。在我的情况下,它会触发我的 profile#public 操作并尝试使用“资产”令牌查找用户。

标签: ruby-on-rails rails-routing


【解决方案1】:

您正在 request.session 搜索令牌,但这总是会失败,因为这是会话存储而不是请求参数。您可能希望约束类中的 params[:token] 等效。 request object documentation 表示request.path_parameters[:token] 可能包含您要查找的值。

【讨论】:

    猜你喜欢
    • 2012-06-13
    • 2020-11-18
    • 2021-08-08
    • 2011-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多