Tag: 路由

18n_routing并重定向到正确的URL

我在Rails 3中开发了一个关于食物和食谱的多语言社交网络。 我使用了http://github.com/kwi/i18n_routing中的i18n_routing gem。 一切正常,但我们遇到了问题。 如果用户(使用英文版)共享一个项目,例如在Facebook上,我们得到一个这样的URL: http : //domain.com/en/the-best-food-recipe/recipes/123 。 现在,如果另一个用户使用意大利语登录并单击该URL,则社交网络将以英语打开,即使我有用户会话并且我知道他的语言是意大利语。 有没有办法将URL翻译回意大利语,以便我可以轻松地将用户重定向到: http : //domain.com/it/the-best-food-recipe/ricette/123 ? 提前致谢

在link_to中显示的路径

我是刚刚完成rails僵尸的rails(railsforzombies.org)的新手,我正在尝试构建我的第一个应用程序(博客)。 我已经搭建了一个基本结构并进行了更改,包括更改路径,添加部分内容以及对视图的其他改进以及安装Blueprint css框架。 我遇到的问题是我的所有链接(使用link_to创建)最终看起来像这样: test post(/post/1) 在链接本身之后打印链接的路径。 但是,我无法使用链接的路径复制文本。 万一它有帮助这就是我的routes.rb看起来像: Blog::Application.routes.draw do |map| root :to => “Posts#index” match ‘/post/:id’ => ‘Posts#show’, :as => ‘show’ match ‘new’ => ‘Posts#new’, :as => ‘new_post’ 有帮助吗? 编辑: 我的link_to调用如下: 编辑: 在这张图片中可以看到问题。 此外,如果我更改为show_url,则url将显示在括号中而不是路径中。

设置’link_to’语句的’:controller’参数时出现问题

我正在使用Ruby on Rails 3.0.9,我想知道为什么我得到下面描述的错误,我该如何解决。 在我的/views/articles/categories/_content.html.erb文件中,我有: … content[:article_controller], :action => ‘new’}) %> … 如果我将content[:article_controller]设置为(对于:only_path选项,则设置为true和false ) 1. content[:article_controller] = ‘articles’ 2. content[:article_controller] = ‘/articles’ 3. content[:article_controller] = ‘/articles/’ 4. content[:article_controller] = ‘/’ 4. content[:article_controller] = ” 我分别得到以下错误(注意:controller值): 1. `ActionView::Template::Error (No route matches {:controller=>”articles/categories/articles”, :action=>”new”})` 2. `ActionView::Template::Error (No route matches {:controller=>”articles//articles”, :action=>”new”})` 3. `ActionView::Template::Error (No route matches {:controller=>”articles/”, […]

如何在资源上命名路由?

如果我有一个带有单个动作index的控制器Home ,我知道我可以写: get ‘home’ => ‘home#index” 我将自动拥有命名路径home_path 。 但如果我这样定义它: resources :home, only: :index 我得到了路线home_index_path 。 为什么这样,如果我使用resources约定,如何创建命名路由home_path 。

Rails 3 / devise似乎忽略了自定义控制器

使用Rails 3应用程序的设计,我已经阅读了如何自定义后注册路由的wiki / docs,我正在使用可确认模块,所以我想我需要覆盖after_inactive_sign_up_path_for 我想我已经做得很好,但它完全忽略了我的自定义控制器,并在注册后仍然路由到root_path。 使我抓狂。 我的注册是使用用户模型,我使用generate任务复制了devise的视图; 如果我将它们移动到视图/注册设计回落到默认视图(在我猜的gem中),所以它似乎不是’注意’我的控制器 我的路线中有这个: devise_for :users, :controllers => { :registrations => “registrations” } match ‘sign_up_done’ => ‘home#sign_up_done’, :as => :after_sign_up 这是我的控制器:(controllers / registrations_controller.rb) class RegistrationsController < Devise::RegistrationsController def after_inactive_sign_up_path_for(resource) after_sign_up_path end def after_sign_up_path_for(resource) after_sign_up_path end end (添加after_sign_up_path_for以防万一,使用确认) 它似乎完全忽略了我的控制器,命名错了吗? 谢谢你的任何输入!

rails路由:id无效

我有一个名为Schedule的模型属于一个项目。 一个项目有一个时间表。 我试图遵循CRUD惯例,但有一些困难。 我有一个列出所有项目的页面,并在每个项目旁边都有一个链接来创建计划。 我在路径文件中开始使用以下内容: resources :schedules 这是问题所在。 在“新计划”页面的url中,需要有一个:id,指的是计划所属的项目,当创建计划时,它将属于正确的项目。 我不知道如何用资源做到这一点,所以我将路由代码更改为: match ‘schedules/new/:id’, to: ‘schedules#new’, as: :new_schedule, via: [:get, :post] resources :schedules, except: [:new, :create] 出于某种原因,此页面为空白。 它只是白色的。 如何修复路线? 谢谢。 更新: 我也尝试将我的路线更改为以下内容: resources :projects do resources :schedules end 这使得新计划的URLforms为: /projects/:project_id/schedules/new(.:format) 我认为这是应该如何完成的,但是,新计划的表格是写成的 form_for @schedule 并产生以下错误: undefined method `schedules_path’ 有任何想法吗?

是否可以命名整个rails应用程序(即添加到url路径)

我有一个rails应用程序,在routes.rb中有大约30个匹配项 match ‘/send-email’ => ‘index#send_email’ match ‘/forgot-password’ => ‘users#forgot_password’, :as => :forgot_password match ‘/forgot-password-confirmation’ => ‘users#forgot_password_confirmation’ match ‘/user/save-password’ => ‘users#save_password’, :as => ‘edit_users_password’ match ‘/user/home’, :to => ‘users#home’ match ‘/users/:id’, :to => ‘users#show’, :as => “user_show” 添加前置路线的最佳方法是什么? like /user/home -> /app/user/home, /user/show/:id -> /app/user/show/:id 有没有办法在全球范围内应用? 然后为root添加例外? 和domain.com/user-name等漂亮url的单一路线?

在Rails 2.1.x中处理RoutingError的最佳方法是什么?

我正在使用Rails 2.1中的routing.rb代码,并尝试将其设置为可以使用在找不到合适路径时抛出的RoutingErrorexception。 这是一个有点棘手的问题,因为有一些类别的URL只是简单的BAD:/ azenv.php机器人攻击,人们在URL中键入/ bar / foo / baz等等…我们不想要那。 然后是微妙的路由问题,我们确实希望得到通知:/ artists /例如,或///。 在这些情况下,我们可能会想要抛出错误,或者不是……或者我们让Google向我们发送曾经有效但不再因为人们删除它们的url。 在每种情况下,我想要一种方法来包含,分析和过滤我们得到的路径,或者至少有一些Railsy方法来管理路由超过正常的’后备catchall’url。 这存在吗? 编辑: 所以这里的代码是: # File vendor/rails/actionpack/lib/action_controller/rescue.rb, line 141 def rescue_action_without_handler(exception) log_error(exception) if logger erase_results if performed? # Let the exception alter the response if it wants. # For example, MethodNotAllowed sets the Allow header. if exception.respond_to?(:handle_response!) exception.handle_response!(response) end if consider_all_requests_local || […]

安装Active Admin并获取ArgumentError

我正在尝试第一次使用ActiveAdmin w / Rails 4.安装所有相关gem后,我尝试运行安装程序,即: rails generate active_admin:install 这样做会给我以下错误: in `add_route’: Invalid route name, already in use: ‘admin_root’ (ArgumentError) 但是,我在routes.rb中没有任何’admin_root’路由,所以我有点困惑。 这是运行’rake routes’的输出: Prefix Verb URI Pattern Controller#Action exams GET /exams(.:format) exams#index POST /exams(.:format) exams#create new_exam GET /exams/new(.:format) exams#new edit_exam GET /exams/:id/edit(.:format) exams#edit exam GET /exams/:id(.:format) exams#show PATCH /exams/:id(.:format) exams#update PUT /exams/:id(.:format) exams#update DELETE /exams/:id(.:format) exams#destroy […]

Rails 3,自定义操作和HTML请求方法

我不太了解使用“post”与“get”vs“put”请求,自定义控制器操作以及是否使用链接或表单/按钮的专业人士和骗局。 所以,假设我有一个简单的待办事项列表,包含任务和任务控制器,我想要一个“完整”的操作,我在数据库中找到一个特定的任务,并将其状态属性从“不完整”更新为“完成”。 def complete @task = Task.find(params[:id]) if @task.update_attributes(:status => “complete”) redirect_to tasks_url, :notice => “Completed!” else redirect_to tasks_url, :error => “Whoops.” end end 定义此路由的最佳实践方法是什么,我应该使用哪种HTML请求方法(post?put?get?),我应该使用普通链接还是表单? (并注意:假设我的用户安全模型都是用设计计算出来的,在filter之前是合适的,等等) 最重要的是,我将如何在Rails 3 routes.rb文件中清楚地表达这一切? 注意,下面的代码并不适合我: #routes.rb resources :tasks do members do post ‘complete’ end end 所以目前我正在使用它: #routes.rb match ‘tasks/:id/complete’, ‘tasks#complete’, :as => “complete_task” #view = link_to “Complete”, complete_task_path(:id => @task.id) […]