Tag: 嵌套资源

嵌套资源的表单

我已经完成了大量的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 现在我正在尝试访问’/ jobs / 1 / questions / new’并继续获取 问题#new中的NoMethodError 当代码出现时,我开始使用错误No route matches {:controller =>“questions”} 我知道这是错的,所以我开始尝试其他组合,但没有一个工作。 我试过了 那 那 在一堆其他组合中,并没有工作。 这是我的rake路线的链接:git clone […]

Rails 3使用嵌套资源进行路由错误

在我的Rails应用程序中,有很多游戏,每个游戏都有自己的排行榜。 因此,将排行榜嵌套在游戏中是有道理的,因此您只能通过游戏进入排行榜。 我设置了我的routes.rb文件(重要部分): resources :games do resources :leaderboards end 所以我更新了我的控制器,以便从传入的game_id获得适当的游戏,并从中获取排行榜信息。 但是,我的问题来自我的观点。 在本节中(从脚手架自动生成的视图): ‘Are you sure?’, :method => :delete %> 代码破解说: No route matches {:action=>”edit”, :controller=>”leaderboards”, :game_id=>#} 这一行,结果是错误:(我的代码中的第19行) 删除此行,视图呈现正常。 因此,URL部分已损坏,但我该如何解决? 奇怪的是,我在Show视图中确实有“edit_game_leaderboard_path”,它运行正常……我做错了什么?

Rails:嵌套资源

以下路径引发错误: = link_to ‘Subscribers’, user_subscribers_path(current_user) 的未定义方法`user_subscribers_path’ 我不知道为什么。 我已经定义了我的路线如下: resources :users, :only => [:show, :index], :has_many => :subscribers, :shallow => true 谢谢! EDIT rake路线没有显示任何特别有用的东西。 订阅者只有两行: users GET /users(.:format) users#index {:has_many=>:subscribers} user GET /users/:id(.:format) users#show {:has_many=>:subscribers}

从URL获取父资源

我有两个具有has_many关系的对象(@stacks和@stories)(通过@anthologies)。 故事可以单独查看(/ stories / 1 /)或作为堆栈中的集合(/ stacks / 1 / stories / 1 /)查看。 map.resources :stories map.resources :anthologies map.resources :stacks, :has_many => :stories do |stack| stack.resources :stories end 我已经有了这个基本框架,我正在尝试识别故事作为堆栈的一部分呈现的时间,以便我可以在故事之间构建一些导航。 我认为最简单的方法是检查主控制器的URL,但1)我不知道如何做到这一点,2)它似乎非常不Railsy,就像应该有一个更直接的方式。 仍然关注Rails的一些基础知识 – 关于如何实现所需function的任何建议?

如何嵌套收集路线?

我在Rails 3应用程序中使用以下路由配置。 # config/routes.rb MyApp::Application.routes.draw do resources :products do get ‘statistics’, on: :collection, controller: “statistics”, action: “index” end end StatisticController有两个简单的方法: # app/controllers/statistics_controller.rb class StatisticsController < ApplicationController def index @statistics = Statistic.chronologic render json: @statistics end def latest @statistic = Statistic.latest render json: @statistic end end 这将生成由StatisticsController成功处理的URL /products/statistics 。 如何定义导致以下URL的路由: /products/statistics/latest ? 可选:我尝试将工作定义置于关注点但它失败并显示错误消息: undefined method ‘concern’ […]

Rails – 与两个父母嵌套资源

假设我有一个带有两个父模型的子模型: Event has_many tickets Person has_many tickets Ticket belongs_to Event Ticket belongs_to Person 路由已映射,因此Ticket始终嵌套在Event或Person中: resource :people do resources :tickets end resources :events do resources :tickets end 如何根据父资源调整ticket_Controller CRUD操作的范围? 现在我正在测试params并使用条件语句: class TicketController before_filter :get_person before_filter :get_event def index if @person do … elsif @event do … end respond_to … end end 对于每一个动作来说,这似乎有点乏味。 有更多的轨道式DRY方式吗?

Rails嵌套奇异资源路由

我有一个简单的用户模型,带有一个单一的嵌套Profile资源,因此在我的routes.rb中我有: resources :users do resource :profile, :only => [:edit, :update, :show] end 这会生成预期的路线: edit_user_profile GET /users/:user_id/profile/edit(.:format) {:action=>”edit”, :controller=>”profiles”} user_profile GET /users/:user_id/profile(.:format) {:action=>”show”, :controller=>”profiles”} user_profile PUT /users/:user_id/profile(.:format) {:action=>”update”, :controller=>”profiles”} 我创建了一个简单的控制器更新方法来更新模型,然后在成功更新时重定向: def update @profile = Profile.find_by_user_id(params[:user_id]) @user = User.find_by_id(params[:user_id]) respond_to do |format| if @profile.update_attributes(params[:profile]) format.html { redirect_to( user_profile_path(@user, @profile), :notice => ‘Profile was successfully updated.’) } else […]

Rails:嵌套资源冲突,如何根据被调用的路由确定索引操作的范围

想象一下,你有两个定义的路线: map.resources articles map.resources categories, :has_many => :articles 都可以通过助手/路径访问 articles_path # /articles category_articles_path(1) # /category/1/articles 如果你访问/articles ,则执行ArticlesController index操作。 如果你访问/category/1/articles ,那么也会执行ArticlesController index操作。 那么,根据呼叫路由有条件地仅选择范围文章的最佳方法是什么? #if coming from the nested resource route @articles = Articles.find_by_category_id(params[:category_id]) #else @articles = Articles.all

Rails命名空间与嵌套资源

假设我的应用程序有两个型号,Foo和Bar。 Foo可选择belongs_to Bar。 现在我可以查看单个Foo,或者搜索特定的Foo,而FoosController可以处理所有这些。 我的URL就像: foos/1和foos/new 有时我想看一个酒吧。 BarsController处理它,我得到它: bars/1或bars/1/edit 。 如果我正在看一个酒吧,我可能想浏览那个酒吧的所有Foos。 所以,我想使用bars/1/foos/来看看那些Foos。 嵌套资源非常简单,它看起来像这样: resources :foo resources :bar do resources :foo end 然而,作为酒吧的一部分的Foos有点特别,与常规的Foos不同。 所以,举例来说,如果我加载foos/1或bars/1/foos/1 ,我会看同样的Foo,但我会关注每种情况下的不同信息。 因此,我一直在考虑使用BarFoos控制器来处理Foos,因为它们处于Bar的环境中。 但是,如果我将BarFoos嵌套在Bar下,那么我的助手就像bar_bar_foos_path和new_bar_bar_foo_path 。 这似乎是多余的。 所以,现在我正在考虑命名空间,这是我以前从未研究过的。 我在rails指南中看到我可以定义: namespace “bar” do resources :foos end 如果我这样做,我可以在app/bar/下创建第二个FoosController ,并且FoosController可以使用bar_foo_path(:id)而不是bar_bar_foo_path(:id)等好帮手来处理Bar内部的Foos。 但是,如果我这样做,我的BarsController会发生什么? 如果不是resources :bars请求如何被路由到BarsController resources :bars我有namespace “bar” ? 而且,最后,在我的辅助FoosController中我需要做些什么特别的事情来确保与顶级FoosController没有名称冲突? 我意识到路由说“命名空间”,但其余的ruby代码如何知道app/bar/foos_controller和app/foos_controller不是同一个类? 谢谢!

使用has_one和belongs_to的嵌套路由和form_for和模型

如何使用嵌套路由映射has_one模型以及如何在RESTful数据库之后为/localhost:3000/users/1/profile/new,html.erb添加form_for? 用户有一个个人资料。 楷模 class Profile < ActiveRecord::Base attr_accessible :name, :surname belongs_to :user end class User < ActiveRecord::Base attr_accessible :email, :email_confirmation, :password, :password_confirmation has_secure_password has_one :profile, dependent: :destroy end resources :users do resources :profiles (note: has_one profile) resources :progress_charts resources :calories_journals end 意见/型材/ new.html.erb About You controller:Profiles_controller.rb我发现了两个错误,因为我不太明白为什么它不起作用。 class ProfilesController < ApplicationController def index end def show […]