使用Rails 3.2.8与以下宝石的应用程序
gem 'friendly_id', '~> 4.0'
gem 'route_translator'
Run Code Online (Sandbox Code Playgroud)
在/config/initializers/i18n.rb中
TLD_LOCALES = {
"com" => :en,
"jobs" => :en,
"net" => :en,
"in" => :en,
"de" => :de,
"ch" => :de,
"at" => :de,
"br" => :pt,
"ar" => :es,
"cl" => :es,
"mx" => :es
}
Run Code Online (Sandbox Code Playgroud)
在/app/controllers/application_controller.rb中,使用before-filter为每个请求设置区域设置:
before_filter :set_auto_locale
def set_auto_locale
I18n.locale = TLD_LOCALES[request.host.split('.').last]
end
Run Code Online (Sandbox Code Playgroud)
在routes.rb中
localized do
match "label_vacancies/:view_job"=>"job_seekers#view_job"
get "label_aboutus", :to => "home#about_us", :as => "about_us"
end
Run Code Online (Sandbox Code Playgroud)
当用户请求更改语言区域设置时,域下方应根据请求的区域设置加载.
在初始化器中
domain_based_on_locale = {
:en => "xxxxx.com",
:de => "xxxxx.de", …Run Code Online (Sandbox Code Playgroud) ruby ruby-on-rails internationalization rails-routing ruby-on-rails-3.2
我有一个名为User的资源和另一个名为Order的资源.
我希望Order嵌套在Users中,这样我就可以拥有这些路径:
/users
/users/:id
/users/:id/new
/users/:id/edit
/users/:user_id/orders
/users/:user_id/orders/:id
/users/:user_id/orders/:id/new
/users/:user_id/orders/:id/edit
Run Code Online (Sandbox Code Playgroud)
我怎么能用activeadmin做到这一点?
我想在子目录中组织我的控制器.这是一个例子:
routes.rb中:
resources :locations do
resources :users
end
Run Code Online (Sandbox Code Playgroud)
我想将我的控制器放在适当的子目录中:
app/controllers/locations/users_controller.rb
Run Code Online (Sandbox Code Playgroud)
并且网址是(标准):
/locations/1/users
/locations/1/users/new
/locations/1/users/10/edit
...
Run Code Online (Sandbox Code Playgroud)
如果我的路由中有一个命名空间,我可以将users_controller.rb更改为
class Locations::UsersController < LocationsController
end
Run Code Online (Sandbox Code Playgroud)
但它不适用于嵌套资源,而是出现以下错误:
Routing Error
uninitialized constant UsersController
Run Code Online (Sandbox Code Playgroud)
如果我添加:
resources :locations do
resources :users
end
match 'locations/:location_id/users' => "locations/users#index"
Run Code Online (Sandbox Code Playgroud)
但我必须为每个动作和嵌套资源添加一个路由......
我在我的主应用程序的应用程序控制器中有一个before_filter挂钩,它执行以下操作:(它不只是在闪存中放置一个链接,有一条消息,但它与问题无关,它只是访问路径中的方法)
class ApplicationController < ActionController::Base
before_filter :set_link
def set_link
flash[:notice] = items_path
end
end
Run Code Online (Sandbox Code Playgroud)
这适用于应用程序,但是当我进入控制器的引擎时,我得到了异常
No route matches {:controller=>"items", :action=>"index"}
Run Code Online (Sandbox Code Playgroud)
据我所知,在引擎中,除非加上前缀,否则路径助手都是引擎 main_app
所以将应用程序控制器中的方法更改为
def set_link
flash[:notice] = main_app.items_path
end
Run Code Online (Sandbox Code Playgroud)
摆脱异常,但我真的不想这样做.是否有其他解决方案让引擎识别main_app路由?
编辑:
如果应用程序布局调用路径助手,也会发生这种情况.因此,如果引擎被设计为集成到main_app的布局中,那么这个问题也将在那里进行.
我正在构建一些rails示例应用程序,其中我有两个模型User和Projects.两者之间的关联是用户has_many项目.现在的问题是我愿意为用户的项目提供一些下拉菜单,如果有人点击那个项目,那么将项目显示页面.但是如果用户在一个项目的编辑页面并从下拉列表中选择其他项目我想让他到显示页面的任何想法到达新选项目的编辑页面相同的情况.我正在寻找的解决方案是: - 我们可以使用params [:controller]找到当前控制器,使用params [:action]查找当前操作如何找到当前路径.Thanx提前
如果有人想看看我的代码是什么: - 这里是github的链接: - ' https://github.com/peeyushsingla/session_test.git/这里我使用简单的链接,但实际上我会提供一些单一的下拉菜单将显示适用于所有编辑,显示,新操作的所有操作
我在Rails中的动词遇到了麻烦 ......
查看资源(狗)的页面has_many(跳蚤).嵌入在狗中的show.html.haml是一个render @dog.fleas自动(?)查找并使用"fleas/_flea.html.haml"中的模板来列出与所述狗相关的每个跳蚤的调用.
这显示正确.噢!现在,在每个跳蚤的旁边,我放了一个"Kill Flea"链接进入网址://localhost:3000/dogs/1/fleas/7.由哪个生成:
= link_to("Kill Flea", [ flea.dog, flea ], :method => :delete, :confirm => "Sure? A bunny will die")
Run Code Online (Sandbox Code Playgroud)
但是每次点击该链接都没有确认...并且它会呈现跳蚤的show.html页面.这是因为如果它使用GET上/dogs/1/fleas/7,而不是删除?!?
ps-不担心蜘蛛和机器人在我的数据库中删除东西......我只是想学习Rails ......并了解发生了什么
我有一个名为的模型ImplicitTest.它被称为有一个Ruby对象,Test只是在Rails中打破了很多东西.
但是,我仍然希望将其作为RESTful资源公开test(例如/tests,/test/1/edit依此类推).此外,保持控制器的效果会很好TestsController,尽管不那么重要.
我通过resources :tests在routes.rb文件中使用简单的行来实现这一点,但这对于RESTful表单来说是失败的(例如<%= form_for @test ... >- 这会发现@test对象的类型为ImplciitTest,并尝试查找implicit_test_path不存在的行.
我尝试添加form_for选项,但得出的结论是,为了使表单适用于两者new和edit操作,没有一种单一,统一的方式要求form_for()使用不同的前缀进行路径名查找.
所以我一直试图从路由方面解决问题.有没有我可以添加到路径文件的行,这将允许我:
我知道我正在离开Golden Path来做这件事,但Rails并没有让我使用Test作为模型名称,但这是用户希望在此资源的URL中看到的名称,所以我希望有是启用此功能的简单路由选项.
所以我一直遇到以下错误:
No route matches {:action=>"show", :controller=>"users"}
我尝试运行rake路由,我可以看到路由存在:
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
Run Code Online (Sandbox Code Playgroud)
但每次我尝试访问该页面"/users/1"(1为用户ID)时,我都会收到上述错误.有任何想法吗?谢谢!
这是我的routes.rb:
SampleApp::Application.routes.draw do
root to: 'static_pages#home'
resources :users
resource :sessions, only: [:new, :create, :destroy]
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
Run Code Online (Sandbox Code Playgroud)
这是我的users_controller.rb:
class UsersController < ApplicationController
before_filter :signed_in_user, only: [:index, :edit, :update]
before_filter :correct_user, only: [:edit, :update] …Run Code Online (Sandbox Code Playgroud) 使用{:trailing_slash => true}可以很容易地在链接中添加尾部斜杠,但这并不能说明用户是否键入了非斜线网址.有没有办法通过路由器中的重定向强制执行尾部斜杠?
get "/:controller/:id" => redirect{|params| "/#{params[:controller]}/#{params[:id]}/" }
Run Code Online (Sandbox Code Playgroud)
以上导致循环循环.
为什么?
"./subclass"的相对链接
/parent/1
Run Code Online (Sandbox Code Playgroud)
与...大不相同
/parent/1/
Run Code Online (Sandbox Code Playgroud) 我有一些像这样的命名路线rake routes:
birthdays GET /birthdays(.:format) birthdays#index
Run Code Online (Sandbox Code Playgroud)
在rakefile中,我只是希望能够birthdays_url在我的视图中调用.
task :import_birthdays => :environment do
url = birthdays_url
end
Run Code Online (Sandbox Code Playgroud)
但是我收到了一个错误 undefined local variable or method 'birthdays_url' for main:Object