想象一下,你有两个定义的路线:
map.resources articles
map.resources categories, :has_many => :articles
Run Code Online (Sandbox Code Playgroud)
都可以通过助手/路径访问
articles_path # /articles
category_articles_path(1) # /category/1/articles
Run Code Online (Sandbox Code Playgroud)
如果您访问/articles,index行动从ArticlesController被执行.
如果你访问/category/1/articles,也会执行index动作ArticlesController.
那么,根据呼叫路由有条件地仅选择范围文章的最佳方法是什么?
#if coming from the nested resource route
@articles = Articles.find_by_category_id(params[:category_id])
#else
@articles = Articles.all
Run Code Online (Sandbox Code Playgroud) 我有两个型号:
class Solution < ActiveRecord::Base
belongs_to :owner, :class_name => "User", :foreign_key => :user_id
end
class User < ActiveRecord::Base
has_many :solutions
end
Run Code Online (Sandbox Code Playgroud)
我在这样的用户中嵌套解决方案:
ActionController::Routing::Routes.draw do |map|
map.resources :users, :has_many => :solutions
end
Run Code Online (Sandbox Code Playgroud)
最后这是我试图规范的行动:
class SolutionsController < ApplicationController
before_filter :load_user
def show
if(@user)
@solution = @user.solutions.find(params[:id])
else
@solution = Solution.find(params[:id])
end
end
private
def load_user
@user = User.find(params[:user_id]) unless params[:user_id].nil?
end
end
Run Code Online (Sandbox Code Playgroud)
我的问题是,我怎么样@user.solutions.find(params[:id])?
这是我目前的规格:
describe SolutionsController do
before(:each) do
@user = Factory.create(:user)
@solution = Factory.create(:solution)
end
describe "GET Show," do …Run Code Online (Sandbox Code Playgroud) 美好的一天,
有人可以帮助我使用嵌套资源及其最佳实践.
我想我的限制:events路由只:show和:index,这是做它的正确方法是什么?
resources :events do
resources :registrations, :except => [:index]
end
resources :events, :only => [:index, :show]
Run Code Online (Sandbox Code Playgroud)
这是最好的方式还是更多Rubyist会处理这种事情的方式?我添加了两行resources :events或有没有办法将它们组合在一个块中?
提前致谢.
routes ruby-on-rails nested-resources nested-routes ruby-on-rails-3
我是Rails的新手.
我创建了一个Web应用程序,我可以通过/posts/123/comments/或访问/posts/123/comments/new,但我不知道如何在索引视图中使用link_to来显示具体的注释,当我尝试链接它时,出现"无路由"或"未定义的符号" .
我在模型和定义的文章和评论之间的嵌套关系have_many routes.rb和 post_comments GET /posts/:post_id/sensors(.:format) comments#index当我执行耙路线出现.
我怎么能这样做?
在Rails 3.2.11中,我有以下路由定义
resources :foos do
resources :bars
resources :bangs, :controller => 'foos/bangs'
end
Run Code Online (Sandbox Code Playgroud)
这导致以下路线
foo_bars GET /foos/:foo_id/bars(.:format) bars#index
POST /foos/:foo_id/bars(.:format) bars#create
new_foo_bar GET /foos/:foo_id/bars/new(.:format) bars#new
edit_foo_bar GET /foos/:foo_id/bars/:id/edit(.:format) bars#edit
foo_bar GET /foos/:foo_id/bars/:id(.:format) bars#show
PUT /foos/:foo_id/bars/:id(.:format) bars#update
DELETE /foos/:foo_id/bars/:id(.:format) bars#destroy
foo_bangs GET /foos/:foo_id/bangs(.:format) foos/bangs#index
POST /foos/:foo_id/bangs(.:format) foos/bangs#create
new_foo_bang GET /foos/:foo_id/bangs/new(.:format) foos/bangs#new
edit_foo_bang GET /foos/:foo_id/bangs/:id/edit(.:format) foos/bangs#edit
foo_bang GET /foos/:foo_id/bangs/:id(.:format) foos/bangs#show
PUT /foos/:foo_id/bangs/:id(.:format) foos/bangs#update
DELETE /foos/:foo_id/bangs/:id(.:format) foos/bangs#destroy
foos GET /foos(.:format) foos#index
POST /foos(.:format) foos#create
new_foo GET /foos/new(.:format) foos#new
edit_foo GET /foos/:id/edit(.:format) foos#edit
foo …Run Code Online (Sandbox Code Playgroud) controllers rails-routing nested-resources ruby-on-rails-3.2
在config/routes.rb中:
resources :posts do
resources :comments
end
resources :pictures do
resources :comments
end
Run Code Online (Sandbox Code Playgroud)
我想允许更多的事情被评论.
我目前正在使用mongoid(mongomapper并不像我想的那样与Rails 3兼容),而注释是一个嵌入式资源(mongoid还不能处理多态关系资源),这意味着我确实需要父资源为了找到评论.
有没有优雅的方法来处理以下一些问题:
在我的控制器中,我需要在找到评论之前找到父母:
if params[:post_id]
parent = Post.find(params[:post_id]
else if params[:picture_id]
parent = Picture.find(params[:picture_id]
end
Run Code Online (Sandbox Code Playgroud)
如果我开始添加更多可评论的内容,那将会变得混乱.
也url_for([comment.parent, comment])不起作用,所以我将不得不在我的Comment模型中定义一些东西,但我认为我还需要在Comment模型中定义索引路径以及可能的编辑和新路由定义.
随着我的进一步发展,我可能会遇到更多问题.
我无法想象我是第一个尝试解决这个问题的人,是否有任何解决方案可以使其更易于管理?
今天我意识到我已经被嵌套资源带走了一点:
resources :organisations do
resources :studies do
resources :settings
end
end
Run Code Online (Sandbox Code Playgroud)
Rails指南(和我自己的想法)表明你不应该嵌套超过1级,所以我重构了这个:
resources :organisations do
resources :studies
end
resources :studies do
resources :settings
end
Run Code Online (Sandbox Code Playgroud)
有谁知道更清洁/更简洁的方式申报上述路线?谷歌给了我很多Rails 2特有的东西.
非常感谢!
我已经完成了大量的form_for嵌套资源问题,无法让任何解决方案适合我.我想时间问一个个性化的问题.
我有两个模型,工作和问题,工作有很多问题和问题属于工作.
我使用scaffolding创建控制器和模型,然后嵌套routes.rb中的资源.
root :to => "pages#home"
resources :jobs do
resources :questions
end
get "pages/home"
get "pages/about"
get "pages/contact"
class Job < ActiveRecord::Base
has_many :questions
end
class Question < ActiveRecord::Base
belongs_to :job
end
Run Code Online (Sandbox Code Playgroud)
现在我正在尝试访问'/ jobs/1/questions/new'并继续获取
问题#new中的NoMethodError
当代码出现时,我开始使用错误No route matches {:controller =>"questions"}
<%= form_for(@question) do |f| %>
Run Code Online (Sandbox Code Playgroud)
我知道这是错的,所以我开始尝试其他组合,但没有一个工作.
我试过了
<%= form_for([@job.questions.build ]) do |f| %>
Run Code Online (Sandbox Code Playgroud)
那
<%= form_for([@job, @job.questions.build ]) do |f| %>
Run Code Online (Sandbox Code Playgroud)
那
<%= form_for(@job, @question) do |f| %>
Run Code Online (Sandbox Code Playgroud)
在众多其他组合中,这些组合无效.
这是我的佣金路线的链接:git clone https://gist.github.com/1032734
任何帮助表示赞赏,如果您需要更多信息,请告诉我,谢谢.
你在父资源中定义了对"id"的约束:
resources :foo, constraints: { :id => /CONST/ } do
resources :bar
end
Run Code Online (Sandbox Code Playgroud)
嵌套资源将为其自己的id继承该约束,因此生成的路由将如下所示:
/foo/:foo_id/bar/:id/edit(.:format)
{:id=>/CONST/, :foo_id=>/CONST/, :action=>"edit", :controller=>"bar"}
Run Code Online (Sandbox Code Playgroud)
所以,我不希望Bar资源的"id"参数受到限制.
目前,我只是逐个手动映射我想要的路由,但我真的想通过资源助手来生成它.我怎样才能做到这一点?
rest ruby-on-rails constraints nested-resources ruby-on-rails-3
使用Yii2框架我找不到任何内置功能来实现nested resourcesRuby on Rails中调用的东西(http://guides.rubyonrails.org/routing.html#nested-resources)
例如,一篇文章有很多评论.因此,我希望/articles/1/comments在使用index操作时,可以通过URL 访问与文章相关的注释; 通过/articles/1/comments/create何时使用create动作......等等
我一定要添加多个动作的方法来ArticlesController叫actionIndexComments(), actionCreateComment()...?
或者我应该?article_id=1通过GET 传递参数并在CommentsController中使用它进行过滤 ?
或者我应该实现UrlManager可以处理嵌套路由的自定义类?(也许有人已经实现了它?)
现在的最佳做法是什么?
nested-resources ×10
routes ×2
bdd ×1
constraints ×1
controllers ×1
form-for ×1
mongoid ×1
php ×1
polymorphism ×1
rest ×1
routing ×1
rspec ×1
ruby ×1
unit-testing ×1
yii2 ×1