Tag: 路由

Rails:重定向到当前路径但不同的子域

我认为这应该相当容易,但我不熟悉它是如何完成的…… 你如何编写一个filter,用于检查当前请求是否针对某些子域,如果是,则将其重定向到同一个URL但是在不同的子域上? 即: blog.myapp.com/posts/1很好,但blog.myapp.com/products/123重定向到www.myapp.com/products/123 。 我在想像…… class ApplicationController < ActionController::Base before_filter :ensure_domain protected def ensure_domain if request.subdomain == 'blog' && controller_name != 'posts' redirect_to # the same url but at the 'www' subdomain… end end end 你怎么写那个重定向?

覆盖路由助手方法

问题有很多评论。 URL“questions / 123”显示了一个问题。 一个url: “问题/ 123#答案-345” 显示一个问题并突出显示答案。 345-是Answer模型的id,“answer-345”是HTML元素的id属性。 我需要覆盖“answer_path(a)”方法来获取 “问题/ 123#答案-345” 代替 “答案/ 345” 怎么做 ?

:除了不在应用程序控制器中的before_filter中工作。 路由问题?

我的应用程序控制器中有一个before_filter来保持用户的会话处于活动状态(如果已经超时,则将其注销)。 这应该在除/ sessions / new和/ sessions / destroy之外的每个操作上调用,这些操作被路由为/ login和/ logout。 我的应用程序控制器的相关部分看起来像这样; class ApplicationController [:login, :logout] private def update_activity_time if current_user time_out = current_user.setting.remember_me ? 20160 : current_user.setting.user_timeout from_now = time_out.minutes.from_now end if session[:expires_at].blank? session[:expires_at] = from_now else time_left = (session[:expires_at].utc – Time.now.utc).to_i if time_left “window.location.replace(\”#{login_url}\”)” end end end 和我的routes.rb包含以下内容; map.login “login”, :controller => “sessions”, :action […]

Rails可选/:语言环境路由

我正在尝试为我的rails应用程序设置路由系统,允许将可选路由(/:locale)允许到网站的基础。 所以或多或少: / en / home /将转到与/ home / / en / people / – > / people /相同的页面 我唯一的问题是在路由配置中设置它。

Rails:如何使用params范围和使用params默认值的路由

我在routes.rb中有这样的行: scope “/:subdomain/” do resource :order, :only => [:new, :create, :show, :update, :edit, :destroy] do get :cancel, :on => :member put :counter, :on => :member end end 例如,这是典型的url: http : //mydomain.com/some_subdomain/order/new 。 此url映射到具有params [:subdomain] =“some_subdomain”的订单控制器的新操作。 我想使用命名路由new_order_url(:subdomain =>“some_subdomain”)。 但是我想将http://mydomain.com/order/new映射到订单控制器,操作new和params [:subdomain] =“default”。 我想为这样的url使用命名路由new_order_url。 这样做的最佳做法是什么?

如何在覆盖设计注册控制器时编写控制器测试?

我希望覆盖Devise::RegistrationsController来实现一些自定义function。 为此,我创建了一个新的RegistrationsController如下所示: # /app/controllers/registrations_controller.rb class RegistrationsController < Devise::RegistrationsController def new super end end 并设置我的路线,如下所示: devise_for :users, :controllers => { :registrations => “registrations” } 并试图像这样测试它: describe RegistrationsController do describe “GET ‘new'” do it “should be successful” do get :new response.should be_success end end end 但这给了我一个错误: 1) RegistrationsController GET ‘new’ should be successful Failure/Error: get :new AbstractController::ActionNotFound: […]

Rails 3没有id的资源路由

我正在Rails 3上创建一个博客应用程序,我想覆盖为post生成的默认show route resources :posts, :except => :show 为节目路线生成的(我没有排除它), /post/:id 我希望我的路由看起来像这样,其中url_title是我的模型在before_save上生成的字符串,其中它删除非字母数字字符并用连字符替换空格。 /:year/:month/:day/:url_title 我试图用这段代码来完成这个: match “/:year/:month/:day/:url_title”, :to => “posts#show”, :as => :post 从理论上讲,这应该允许我调用post_path(@ post)(其中@post是我的post类的一个实例),它应该能够排除这个路径,它几乎可以工作。 唯一的问题是它试图用年份替换post的id。 其他字段填写正确。 我认为这种情况正在发生,因为rails有一些默认行为使得它真的,真的想在URL中拥有id,并且它不相信我使用我自己的唯一标识符(在本例中为post.url_title)。 我可能错了。 任何人都有这种路由的经验,或知道是什么?

Rails 3 – 通过路由将IP列入白名单

这是一个两部分问题。 我需要将我在开发服务器上的rails网站限制为只有几个IP地址,因此公众无法访问它。 (基本HTTP身份validation不会“完全”工作,因为auth会破坏项目中的Flash上​​传器。) 根据我用Google搜索的内容,这就是我在路线文件中提出的内容…… class WhitelistConstraint def initialize @ips = ‘127.0.0.1’ end def matches?(request) @ips.include?(request.remote_ip) end end MyProject::Application.routes.draw do constraints WhitelistConstraint.new do # all my routing stuff here end end 工作得很好。 但是,我需要修改它以便使用多个IP地址。 我尝试在@ips上使用数组,以及循环遍历每个循环,但都没有工作。 最重要的是,我的问题的第二部分…我可能只需要检查IP的一部分,如’127.0.0’。 我该怎么办?

Rails Restful Routing和Subdomains

我想知道是否有任何插件或方法允许我转换资源路由,允许我将控制器名称作为子域。 例子: map.resources :users map.resource :account map.resources :blog … example.com/users/mark example.com/account example.com/blog/subject example.com/blog/subject/edit … #becomes users.example.com/mark account.example.com blog.example.com/subject blog.example.com/subject/edit … 我知道我可以使用命名路由执行此操作,但想知道是否有某种方法可以保留我目前简洁的routes.rb文件。

Rails 3路由 – 从routes.rb传递params

在rails 2.3.5中你可以在routes.rb文件中做这样的事情: map.root :controller => “pages”, :action => “show”, :id => 3 在rails 3中,我没有找到任何传递特定参数的方法(例如在rails 2.3.5中使用:id => 3)。 我知道我可以从控制器处理它并得到相同的结果(我做了),但我想知道是否有一种方法可以在routes.rb中的rails 3中执行相同的操作或者更改它因为它更好练习出于某种原因?