我想创建一个新动作,我称之为"showemployees".这就是我已经做过的事情:
- >在控制器中:
def showemployees
end
Run Code Online (Sandbox Code Playgroud)
- >创建app/views/employees/showemployees.html.erb
- >在config/routes中
match "/employees/showemployees" => "employees#showemployees"
我认为这足以通过localhost:3000/employees/showemployees打开showemployees.html.erb,但似乎Rails仍然通过show action(来自资源:employees)进行路由,并且不接受showemployees-action,因为它告诉我
ActiveRecord::RecordNotFound in EmployeesController#show
Couldn't find Employee with ID=showemployees
Run Code Online (Sandbox Code Playgroud)
我需要改变什么才能让Rails接受showemployees-action?
我的路线的源代码:
System::Application.routes.draw do
match "/employees/showemployees" => "employees#showemployees" #für showemployees.html.erb
root :to => "employees#index"
resources :course_to_dos
resources :current_qualifications
resources :expected_qualifications
resources :skills
resources :employees
resources :positions
resources :admin
end
Run Code Online (Sandbox Code Playgroud) 我有资源生物和视图和添加新生物的链接是:
= link_to "Add new bio", [:new, :admin, :bio]
Run Code Online (Sandbox Code Playgroud)
如果我把资源 :bio 放在这样的范围内:
namespace :admin do
scope "/:bio_type", :defaults => {:bio_type => "company"} do
resources :bios
end
end
Run Code Online (Sandbox Code Playgroud)
这不起作用
= link_to "Add new bio", [:new, :admin, :bio, { bio_type: params[:bio_type] }]
Run Code Online (Sandbox Code Playgroud)
我的问题是如何将范围参数添加到 url_for 助手?默认情况下,rails 可以执行此操作吗?
ps new_admin_bio_path({bio_type: params[:bio_type]}) 工作正常,但只是好奇
我在版本化命名空间中有我的API,
namespace :api do
namespace :v1 do
resources :posts
end
end
Run Code Online (Sandbox Code Playgroud)
但是现在当我有我的控制器时,redirect_to @post我得到一个路由错误,Post因为没有为它定义路由.
def create
@post = Post.new(params[:post])
if @post.save
redirect_to @post
else
# ...
end
end
undefined method `post_url' for #<Api::V1::PostsController:0x007fb9c5fc8858>
Run Code Online (Sandbox Code Playgroud)
我知道我可以更新我的控制器以重定向到指定的路由,而不是世界末日:
def create
@post = Post.new(params[:post])
if @post.save
redirect_to api_v1_post_url(@post)
# or redirect_to [:api, :v1, @post]
else
# ...
end
end
Run Code Online (Sandbox Code Playgroud)
有没有办法让redirect_to @post自动检测到它应该是正确的api版本(因此导轨可以使用api_v1_post_url)?我知道我可以覆盖,redirect_to但实际上我想要一个名称空间感知url_for,也许rails为此提供了一些钩子?
我是Rails的新手,并且一直在学习教程。
我一直在摆弄route.rb,现在对它什么时候寻找show方法以及什么时候寻找索引方法(如果没有明确提及)完全感到困惑。
我有一个管理部分,其中包含目录的controllers子目录.即,该目录app/controllers/admin/包含一组文件,每个文件包含用于处理管理部分的单独部分的控制器.
现在,我想创建一个非常简单的"管理主页",只是说"欢迎来到管理部分",但我想避免为此目的创建一个完整的控制器,或者放置"action"方法这个视图在其他一些任意控制器中.
所以,问题是,有没有办法将''直接路由到'视图'' - 直接路由到HTML文件?(并且,我不想从请求的路径路由到HTML文件/some-file.html;我需要从类似的路径路由/admin/.)
在这里提出这个问题几乎不值得创建一个存根控制器所花费的时间,但我相信我将来会再次有这样的需求.
我在routes.db文件中有这个代码:
resources :users do
member do
get :following, :followers
end
end
Run Code Online (Sandbox Code Playgroud)
我收到错误:
can't use member outside resource(s) scope (ArgumentError)
Run Code Online (Sandbox Code Playgroud)
我该如何解决?
更新:这是我的routes.db文件的所有代码:
SampleApp::Application.routes.draw do
get "sessions/new"
resources :users
member do
get :following, :followers
end
resources :sessions, :only => [:new, :create, :destroy]
resources :microposts, :only => [:create, :destroy]
resources :relationships, :only => [:create, :destroy]
match '/signup', :to => 'users#new'
match '/signin', :to => 'sessions#new'
match '/signout', :to => 'sessions#destroy'
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个通用路由,允许我通过控制器的操作来引用它.
我正在使用以下行:
match ':action' => 'pages#:action', :as => 'action'
Run Code Online (Sandbox Code Playgroud)
让我们说一下页面控制器中名为`foobar'的动作.我希望能够写作
link_to 'Click Me', pages_foobar_path
Run Code Online (Sandbox Code Playgroud)
在一个视图中.问题是Invalid route name: ':action'当我尝试编写该路由时出现错误.
请注意,行
match ':action' => 'pages#:action'
Run Code Online (Sandbox Code Playgroud)
没有:as参数完美的工作,但后来我必须手动编写路径,如下:
link_to 'Click Me', '/pages/foobar'
Run Code Online (Sandbox Code Playgroud)
任何方式?
我有一个问题模型,允许用户添加问题.
我已经设置了Friendly_id,所以当用户添加问题时,网址是:
http://localhost:3000/questions/this-is-a-question
Run Code Online (Sandbox Code Playgroud)
但是,我希望网址看起来像这样
http://localhost:3000/this-is-a-question
Run Code Online (Sandbox Code Playgroud)
你对于我如何用friendly_id做到这一点有什么想法吗?
谢谢
更新:
这是我的friendly_id有问题.rb
extend FriendlyId
friendly_id :title, use: [:slugged, :finders]
def slug_candidates
[
:title,
[:title, :id]
]
end
def should_generate_new_friendly_id?
new_record?
end
Run Code Online (Sandbox Code Playgroud)
结束
ruby-on-rails url-routing rails-routing friendly-id ruby-on-rails-4
我的角度应用程序中有以下代码.
app.config(function($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$routeProvider
.when("/users/:u_id/restaurants/:r_id/menus/:m_id/sections/:sec_id/items", { templateUrl: "/assets/menus/items.html", controller: "MenuItemCtrl" })
});
Run Code Online (Sandbox Code Playgroud)
当我使用导航到路径时,它工作正常
<a href="./sections/{{section.id}}/items">View Items</a>
Run Code Online (Sandbox Code Playgroud)
但是,当我刷新页面或直接转到URL时,它会抛出rails路由错误
No route matches [GET] "/users/3/restaurants/3/menus/4/sections/4/items"
Run Code Online (Sandbox Code Playgroud)
如果我在routes.rb中创建此路由,它只返回与响应关联的JSON,并且不会路由到正确的模板.
有谁知道如何解决这个问题?
ruby-on-rails rails-routing angularjs ruby-on-rails-4 angular-routing
我有3个型号:FamilyTree,Node,Comment.
FamilyTree上的每个条目都是一个节点.节点可以是注释.
模型如下:
FamilyTree.rb
# == Schema Information
#
# Table name: family_trees
#
# id :integer not null, primary key
# name :string(255)
# user_id :integer
# created_at :datetime
# updated_at :datetime
#
class FamilyTree < ActiveRecord::Base
attr_accessible :name
belongs_to :user
has_many :memberships, dependent: :destroy
has_many :members, through: :memberships, source: :user, dependent: :destroy
has_many :nodes, dependent: :destroy
end
Run Code Online (Sandbox Code Playgroud)
Node.rb
# == Schema Information
#
# Table name: nodes
#
# id :integer not null, primary key
# name :string(255) …Run Code Online (Sandbox Code Playgroud) rails-routing ×10
ruby-on-rails ×10
angularjs ×1
friendly-id ×1
routing ×1
ruby ×1
simple-form ×1
url-routing ×1