我需要将以下成员方法添加到许多资源中,有没有办法干掉它?
member do
get :votes
post :up_vote
post :down_vote
end
Run Code Online (Sandbox Code Playgroud)
在我的routes.rb
resources :news do
resources :comments do
member do
get :votes
post :up_vote
post :down_vote
end
end
end
resources :downloads do
resources :whatever do
member do
get :votes
post :up_vote
post :down_vote
end
end
end
Run Code Online (Sandbox Code Playgroud)
编辑
我实际上把它移到了一个像这样的模块中:
module Votable
module RoutingMethods
def votable_resources
member do
get "up_votes"
get "down_votes"
post "up_vote"
post "down_vote"
end
end
end # RoutingMethods
end
Run Code Online (Sandbox Code Playgroud)
现在,我的routes.rb看起来像这样:
require 'votable'
include Votable::RoutingMethods
MyApp::Application.routes.draw do
namespace :main, :path => …Run Code Online (Sandbox Code Playgroud) 我想将一个参数传递给索引操作,但我只是得到了show动作.
routes.rb中:
Test1::Application.routes.draw do
resources :blog
end
Run Code Online (Sandbox Code Playgroud)
blog_controller.rb:
def show
# code
end
def index
# code
end
Run Code Online (Sandbox Code Playgroud)
查看发送以显示操作的网址而不是索引操作:
<a href="/blog/myvar"> My link </a>
Run Code Online (Sandbox Code Playgroud)
我应该在路线文件或视图中添加什么?
输出我的路线:
$ rake routes
blog GET /blog(.:format) {:action=>"index", :controller=>"blog"}
blog GET /blog/:id(.:format) {:action=>"show", :controller=>"blog"}
Run Code Online (Sandbox Code Playgroud) ruby ruby-on-rails parameter-passing rails-routing ruby-on-rails-3
我想改变我在routes.rb中的现有"游戏"路由,但由于SEO我还需要设置301重定向旧链接.
我的旧路由:
match 'games/:permalink/:id/(:page)' => 'games#show'
Run Code Online (Sandbox Code Playgroud)
新路由:
match 'gierki/:permalink/(:page)' => 'games#show'
Run Code Online (Sandbox Code Playgroud)
这是我试图做的重定向:
match 'games/:permalink/:id/(:page)' => redirect {|params| "/gierki/#{params[:permalink]}" + params[:page].nil? ? "" : "/#{params[:page]}" }
Run Code Online (Sandbox Code Playgroud)
上面的重定向不起作用,这是一个错误:
wrong number of arguments (1 for 2)
Run Code Online (Sandbox Code Playgroud) 我有一个应用程序(my_test_app),内置了i18n支持.目前,有两种语言文件可用,FR&EN,如果我在它们之间来回切换,一切正常,因为我希望看到非引擎功能,如用户索引/显示/编辑/删除(ISED)选项.
在my_test_app中,我安装了一个Rails引擎(my_engine),它有一个控制器和模型集(engine_job).所以,一个可行的URL应该是
http://0.0.0.0:3000/fr/my_engine/engine_job
Run Code Online (Sandbox Code Playgroud)
然而,无论我选择何种语言,它总是出现在EN中.检查参数显示:
--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
locale: fr
action: index
controller: my_engine/engine_job
Run Code Online (Sandbox Code Playgroud)
然而,所选择的翻译是EN.
my_test_app route.rb:
MyTestApp::Application.routes.draw do
scope "(:locale)", locale: /#{I18n.available_locales.join("|")}/ do
mount MyEngine::Engine, at: "/my_engine"
end # scope locale
match '*path', to: redirect("/#{I18n.default_locale}/%{path}"), constraints: lambda { |req| !req.path.starts_with? "/#{I18n.default_locale}/" and !req.path == "/#{I18n.default_locale}/"}
match '', to: redirect("/#{I18n.default_locale}/")
end
Run Code Online (Sandbox Code Playgroud)
my_engine route.rb:
MyEngine::Engine.routes.draw do
resources :my_jobs
end
Run Code Online (Sandbox Code Playgroud)
耙路线:
my_engine (/:locale)/my_engine MyEngine::Engine {:locale=>/en|fr/}
/*path(.:format) :controller#:action
/ :controller#:action
users GET (/:locale)/users(.:format) users#index {:locale=>/en|fr/}
POST (/:locale)/users(.:format) users#create {:locale=>/en|fr/}
new_user GET (/:locale)/users/new(.:format) users#new {:locale=>/en|fr/}
edit_user …Run Code Online (Sandbox Code Playgroud) 我有一个rspec规范:
require "spec_helper"
describe ApplicationHelper do
describe "#link_to_cart" do
it 'should be a link to the cart' do
helper.link_to_cart.should match /.*href="\/cart".*/
end
end
end
Run Code Online (Sandbox Code Playgroud)
和ApplicationHelper:
module ApplicationHelper
def link_to_cart
link_to "Cart", cart_path
end
end
Run Code Online (Sandbox Code Playgroud)
这在访问站点时有效,但规范失败,并显示有关路由的RuntimeError不可用:
RuntimeError:
In order to use #url_for, you must include routing helpers explicitly. For instance, `include Rails.application.routes.url_helpers
Run Code Online (Sandbox Code Playgroud)
因此,我Rails.application.routes.url在我的规范中包含了spec_helper-file,甚至包括ApplicationHelper它本身,都无济于事.
编辑:我正在通过spork运行测试,也许这与它有关并导致问题.
在使用Spork运行时,如何包含这些路线助手?
对于Reasons™,我想让一个控制器处理html请求,另一个处理json api请求.
我的路线中有这个:
scope module: :api, as: :api do
constraints format: :json do
resources :users
end
end
constraints format: :html do
resources :users
end
Run Code Online (Sandbox Code Playgroud)
当url运行后缀时,这很有用:/users.json通过我的Api::UsersControlleràla api_users_path; /users.html经过我UsersController点菜users_path.
但是,当url中没有后缀时,这不会按预期运行.将约束实现为lambda会显示出错:
#=> visiting /users
scope module: :api, as: :api do
constraints ->(request){ puts "In api: #{request.format}"; request.format == :json } do
resources :users
end
end
constraints ->(request){ puts "In html: #{request.format}"; request.format == :html } do
resources :users
end
#=> In api: text/html …Run Code Online (Sandbox Code Playgroud) 在URL和rails路由中,使用斜杠字符与井号(井号)字符有什么区别?
这些工作
get "/static_pages/about"
get 'about', to: 'static_pages#about', as: :about
Run Code Online (Sandbox Code Playgroud)
这些没有
get "/static_pages#about"
get 'about', to: 'static_pages/about', as: :about
get 'about', to: '/static_pages#about', as: :about
Run Code Online (Sandbox Code Playgroud)
什么代码控制着这种行为,背后的深层原因是什么?
回答:
(这两个人回答得非常好,我很难选择哪一个标记为接受的答案.我希望以不同的方式表达我对答案的理解,这可能对人们有所帮助.)
使用/符号后,字符串将被识别为附加到基本URL的URL字符串.所以'#'字符将被解释为url的一部分,而url不喜欢使用'#'字符.
在不使用/字符的情况下,第一个单词以某种方式被识别为控制器名称,您可以使用"#"和操作名称进行跟进.
我想在Rails应用程序中重命名资源,并将所有旧路由重定向到新路由,以便与旧链接向后兼容。例如,将“用户”重命名为“成员”,并将所有任意路由(例如example.com/user/1/posts/all?order=date重定向到)example.com/member/1/posts/all?order=date
如何设置routes.rb文件以将所有路径重定向到另一条路线,但在我要匹配的内容之后保留所有路径参数(包括url参数)?
它也应该工作,例如for example.com/user/no/real/path?foo=bar,它应该重定向到example.com/member/no/real/path?foo=bar
基本上我想将任何重定向path到path.sub(/\A\/user/, '/member')
我签出了这个答案,但它似乎不适用于这些参数。
这是我到目前为止的解决方案,非常糟糕:
get 'user/*path', to: redirect{|params, request|
path = params[:path].sub('/user', '/member')
params = request.params.except(:path).map{|k,v| "#{k}=#{v}"}.join('&')
if params.presence
"#{path}?#{params}"
else
path
end
}
Run Code Online (Sandbox Code Playgroud) 今天我遇到了一些奇怪的(并且非常不方便)Ruby on Rails行为,即使持续梳理网络也没有产生令人满意的答案.注意:我将方法和路由名称翻译为更容易用英语阅读,并希望我没有引入任何不一致.
Ruby on Rails 4.2.0在Ruby 2.0下执行(也在Ruby 2.2.0下测试)
考虑使用这些操作的控制器,其中包括:
class AssignmentsController < ApplicationController
def update
...
end
def takeover_confirmation
...
end
end
Run Code Online (Sandbox Code Playgroud)
自从我用了很多手工定义路线的,我也没有在routes.rb中使用的资源.有问题的路线定义如下:
...
post 'assignments/:id' => 'assignments#update', as: 'assignment'
post 'assignments/takeover_confirmation' => 'assignments#takeover_confirmation'
...
Run Code Online (Sandbox Code Playgroud)
相关产出rake routes:
assignment POST /assignments/:id(.:format) assignments#update
assignments_takeover_confirmation POST /assignments/takeover_confirmation(.:format) assignments#takeover_confirmation
Run Code Online (Sandbox Code Playgroud)
当我assignments_takeover_confirmation_path对它进行POST时,rails会将其路由到该update方法.服务器日志:
Started POST "/assignments/takeover_confirmation" for ::1 at ...
Processing by AssignmentsController#update as HTML
Run Code Online (Sandbox Code Playgroud)
如果我把update路由定义后的takeover_confirmation一个,它按预期工作(没有检查的支撑柱, …
我有一个名为的模型Organization.它的定义是app/models/organization.rb
class Organization < ActiveRecord::Base
... code
end
Run Code Online (Sandbox Code Playgroud)
我有一个名为的控制器Admin::Organization::ActivitiesController.它的定义是app/controllers/admin/organization/activities_controller.rb.它有一个索引动作.
class Admin::Organization::ActivitiesController < ApplicationController
def index
@organization = Organization.new
... more code
end
end
Run Code Online (Sandbox Code Playgroud)
执行上面的索引操作时,我收到以下消息:
NameError in Admin::Organization::ActivitiesController#index
uninitialized constant Admin::Organization::ActivitiesController::Organization
Run Code Online (Sandbox Code Playgroud)
由于某种原因,它在控制器类中确定了组织模型.如果我改变使用index方法
@organization = ::Organization.new
Run Code Online (Sandbox Code Playgroud)
然后它工作正常.
此行为似乎没有出现在pry控制台中.如果我binding.pry在索引方法中添加一个调用,那么我可以调用Organization.new或::Organization.new从命令行调用它可以正常工作.
应用程序中的每个其他模型都能正常工作,并且没有这种奇怪的行为.我最初没有写代码,所以我想弄清楚发生了什么.
我认为它可能与routes.rb文件中的命名空间有关.有一个使用该organization单词的命名空间.
namespace :admin do
namespace :organization
resources :activities
end
end
Run Code Online (Sandbox Code Playgroud)
作为测试,我将命名空间更改为:organizations,并且我能够在不需要的情况下使用它::.有没有办法构建事物或路由设置,所以我们可以有一个名称空间:organization,不会干扰名为的模型Organization?
rails-routing ×10
routing ×2
ruby ×2
helper ×1
rails-i18n ×1
rspec ×1
spork ×1
syntax ×1
url ×1