Tag: url routing

Rails – 基于主机和ID的动态路由

我有一个包含许多用户页面的rails应用程序。 当用户想要在此页面指向域名时,我该怎么做? 现在我已经测试了这个,它的工作原理 – root :to => “controller#show”, :id => 4, :constraints => {:host => “www.exampleurl.com”} 但需要将其转换为动态,以便在将列迁移到名为domain的模型后,它会检查domain并为其提供正确的ID。 就像是 – root :to => ‘controller#show’, :id => ‘:id’, :constraints => {:host => ‘:domain’} 这会是什么样的?

围绕`mount`的`scope`无效?

(虽然这讨论了Blacklight引擎,但我相信这个问题实际上纯粹是关于Rails。) 我正在尝试为我的Blacklight应用添加国际化。 为此,我 将config/routes.rb所有内容config/routes.rb到scope “(:locale)”, locale: /en|ja/ ,和 在app/controllers/application_controller.rb我添加了before_action :set_locale并覆盖了default_url_options 正如Rails i18n指南所建议的那样。 大多数事情都有效,但有一点我无法弄清楚。 我的所有应用程序路由都已正确映射,例如http://www.example.com/en/catalog/12345正确匹配(/:locale)/catalog/:id(.:format) ,并被路由到catalog#show用{:id=>/[^\/]+(?=\.json|\.html|$|\/)/, :locale=>/en|ja/} )显示。 所有Devise的url都很好。 一切正常……除了mount Blacklight引擎。 显然,Blacklight引擎不听scope 。 rake routes显示: Routes for Blacklight::Engine: search_history GET /search_history(.:format) search_history#index …. 而不是(:locale)/search_history(.:format) ,我希望如此。 我修改了Blacklight模板,以便我得到一个语言选择器,用日语和英语指向当前页面,但是当我导航到search_history , url_for在面对:locale参数时突然抛出。 为什么mount会忽略scope ? 如何解决我的问题(引擎路由也响应:locale )? 这是我默认的Blacklight生成的config/routes.rb ,用scope修改: Rails.application.routes.draw do scope “(:locale)”, locale: /en|ja/ do mount Blacklight::Engine => ‘/’ root […]

Github喜欢Rails中的路由

在rails中使用类似链路径的github 我有与此类似的url: ‘localhost:3000/document_managers/[:module_name]’ ‘localhost:3000/document_managers/[:module_name]/1/2/3/.’ # can be any level deep 以下是它们的路径定义: map.connect ‘/document_managers/:module’, :controller => “document_managers”, :action => :new_tree, :module => [“A”,”B”,”C”] map.connect ‘/docuemnt_managers/:module/*path’, :controller => “document_managers”, :action => “new_tree”, :module => [“A”,”B”,”C”] 这是问题所在: 模块名称值不能是任何东西,除了来自上面给定的数组,即(“A”,“B”,“C”),就像在任何时候URL必须是这样的 localhost:3000/document_managers/A/1或 localhost:3000/document_managers/B/221/1或 localhost:3000/document_managers/C/121/1 但即使localhost:3000/document_managers/D/121/1被视为有效url并且模块设置为D,即使“D”不在上面列出的数组中,情况并非如此 我希望URL localhost:3000/document_managers/A也重定向到相同的操作,即new_tree,如果没有提供额外的参数,因为在URL中包含额外的参数 localhost:3000/document_managers/C/121/1然后URL被适当地重定向到所需的控制器和动作,但如果URL只包含路径,直到模块名称Rails返回routes ActionController::UnknownAction我不知道为什么因为我已经定义了控制器和动作。

如何在Rails 5 API中呈现文件?

我有一个用React编写的单页面应用程序和Ruby on Rails后端(API模式)。 Rails也提供静态文件。 我将Rails路由器指向public/index.html ,因此我的SPA可以使用react-router管理自己的路由。 这是通常的做法,以便使直接链接和刷新工作。 的routes.rb match ‘*all’, to: ‘application#index’, via: [:get] application_controller.rb class ApplicationController < ActionController::API def index render file: 'public/index.html' end end 问题是这在API模式下不起作用。 这只是一个空洞的回应。 如果我将父类更改为ActionController::Base一切都按预期工作。 但我不想inheritance全class的膨胀,我需要纤薄的API版本。 我尝试添加ActionController::Renderers::All和AbstractController::Rendering但没有成功。

在嵌套资源中自动添加父模型ID

使用Rails 3中的嵌套资源路由,如下所示: resources :magazines do resources :ads end 我已经定义了诸如magazine_ad_path类的助手,我必须同时传递杂志和广告,如果我只是引用了广告,这是不方便的: magazine_ad_path(@ad.magazine, @ad) 有没有一种很好的方法来设置一个ad_path帮助器,它接受@ad并返回包含杂志ID的相应地址? (这样也可以使用link_to @ad , redirect_to @ad等,它会自动调用与模型类对应的ad_path助手。)

Rails嵌套奇异资源路由

我有一个简单的用户模型,带有一个单一的嵌套Profile资源,因此在我的routes.rb中我有: resources :users do resource :profile, :only => [:edit, :update, :show] end 这会生成预期的路线: edit_user_profile GET /users/:user_id/profile/edit(.:format) {:action=>”edit”, :controller=>”profiles”} user_profile GET /users/:user_id/profile(.:format) {:action=>”show”, :controller=>”profiles”} user_profile PUT /users/:user_id/profile(.:format) {:action=>”update”, :controller=>”profiles”} 我创建了一个简单的控制器更新方法来更新模型,然后在成功更新时重定向: def update @profile = Profile.find_by_user_id(params[:user_id]) @user = User.find_by_id(params[:user_id]) respond_to do |format| if @profile.update_attributes(params[:profile]) format.html { redirect_to( user_profile_path(@user, @profile), :notice => ‘Profile was successfully updated.’) } else […]

如何从主机应用程序可用的Rails 3引擎中创建路由?

我有一个Rails 3应用程序,其中包含多个包含其他function的引擎。 每个引擎都是一个单独的服务,客户可以购买访问权限。 但是,我对来自引擎的路径存在问题,这些路由不易用于控制器和视图。 控制器: class ClassroomsController < ApplicationController .. respond_to :html def index respond_with(@classrooms = @company.classrooms.all) end def new respond_with(@classroom = @company.classrooms.build) end .. end app/views/classrooms/new.html.haml : = form_for @classroom do |f| .. f.submit 引擎中的config/routes.rb : MyEngineName::Engine.routes.draw do resources :classrooms end app中的config/routes.rb : Seabed::Application.routes.draw do mount MyEngineName::Engine => ‘/engine’ … end 引擎中的lib/my_engine_name.rb : module […]

如何根据标题重写URL?

这是一个相当开放的问题,我只是在寻找最好的途径。 用户可以发布带有标题的链接。 我希望我的URL用于搜索引擎优化目的,以显示这些标题。 我相信stackoverflow的工作原理大致相同。 所以,如果标题是“我的foobar只是用我的猫做出来的” 我希望url为: www.website.com/posts/My-foobar-just-made-out-with-my-cat 有任何想法吗? 非常感谢提前!!

Rails 3.2友好的URL按日期路由

我想实现具有以下能力的blog \ news应用程序: 显示root上的所有post: example.com/ 显示所有回答某年的post: example.com/2012/ 显示所有回答某些年份和月份的post: example.com/2012/07/ 显示一些post的日期和slug: example.com/2012/07/slug-of-the-post 所以我为routes.rb文件创建了一个模型: # GET /?page=1 root :to => “posts#index” match “/posts” => redirect(“/”) match “/posts/” => redirect(“/”) # Get /posts/2012/?page=1 match “/posts/:year”, :to => “posts#index”, :constraints => { :year => /\d{4}/ } # Get /posts/2012/07/?page=1 match “/posts/:year/:month”, :to => “posts#index”, :constraints => { :year => […]

AngularJS Restful Routing

我正在尝试使用Restful / Ruby convension //[method]/[id]构建我的应用程序。 以前在使用像CodeIgniter这样的服务器端MVC框架时,我是如何基于URI动态路由的: 恩。 www.foo.com/bar/baz/1 然后应用程序将在控制器/类bar使用方法baz并返回views/bar/baz.php (填充来自bar->baz数据) 我想在Angular中做同样的事,但我不确定它是否支持这个(如果确实如此,我不确定如何去做)。 目前我正在使用$routeProvider的when方法来指定每个案例。 $ location.path()看起来可能有我需要的东西,但我不认为我可以在app.js使用它(在config() )。 我想做的是这样的: .config([ ‘$routeProvider’, function($routeProvider) { $routeProvider .when(// controller exists resource+’/’+method, { “templateURL”: “views/” + resource + “/” + method + “.html”, “controller”: resource } ).otherwise({ “redirectTo”:”/error” }); } ]); 并且路由器自动调用适当的方法。 编辑另外,当我指定when(‘/foo/bar’, {…})时,为什么$ routeProvider会惊慌失措 ? 编辑2 Per Lee的建议,我正在考虑做这样的事情: $routeProvider .when( ‘/:resource/:method/:id’, […]