标签: nested-resources

使用RSpec测试嵌套资源

我试图在Rails中为嵌套资源创建测试.相关的路线定义是:

resources :communities do
  resources :contents, :type => 'Content'
end
Run Code Online (Sandbox Code Playgroud)

使用RSpec和factory_girl,我试图开始测试,例如

describe ContentsController do
  it 'should display a content item under a community' do
    content = FactoryGirl.create(:content)
    get :show, :community_id => content.community.id, :id => content.id
  end
end
Run Code Online (Sandbox Code Playgroud)

这些请求总是导致

Failure/Error: get :show, :community_id => content.community.id, :id => content.id
 ActionController::RoutingError:
   No route matches {:community_id=>BSON::ObjectId('4e7773c6ac54c3d1ad000002'),
   :id=>BSON::ObjectId('4e7773c6ac54c3d1ad000001'), :controller=>"contents",
   :action=>"show"}
Run Code Online (Sandbox Code Playgroud)

对于我的生活,我找不到使用RSpec指定到嵌套资源的路径的方法.我在这里做了一些根本错误的事吗?

更新:佣金路线的相关部分是:

    community_contents GET    /communities/:community_id/contents(.:format)             {:action=>"index", :controller=>"contents"}
                       POST   /communities/:community_id/contents(.:format)             {:action=>"create", :controller=>"contents"}
 new_community_content GET    /communities/:community_id/contents/new(.:format)         {:action=>"new", :controller=>"contents"}
edit_community_content GET    /communities/:community_id/contents/:id/edit(.:format)    {:action=>"edit", :controller=>"contents"}
     community_content GET    /communities/:community_id/contents/:id(.:format)         {:action=>"show", …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails nested-resources rspec-rails factory-bot

5
推荐指数
1
解决办法
1775
查看次数

具有视图测试和嵌套资源的Rspec和Rails

我正在使用Yahoo Answers类别的应用程序来提高我的Rails技能.到目前为止,我已经设置了两个模型"问题"和"答案",它们以这种方式嵌套:

  resources :questions do
    resources :answers
  end
Run Code Online (Sandbox Code Playgroud)

我已经对控制器,模型和问题的观点进行了测试,但是我对答案的视图和嵌套路线有点麻烦.我正在使用Rspec和Factory女孩.

我有以下测试:

describe "answers/new.html.erb" do
  before(:each) do
    @question = Factory(:valid_question)
    @answer = Factory(:valid_answer)
    assign(:question, @question)
    assign(:answer, stub_model(Answer,
      :text => "MyString",
      :question_id => 1
    ).as_new_record)
  end

  it "renders new answer form" do
    render
    assert_select "form", :action => question_answers_path(@question), :method => "post" do
      assert_select "textarea#answer_text", :name => "answer[text]"
      assert_select "input#answer_question_id", :name => "answer[question_id]"
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

每当我运行测试时,我收到以下消息:

  3) answers/new.html.erb renders new answer form
     Failure/Error: render
     ActionView::Template::Error:
       No route matches {:controller=>"answers"}
     # ./app/views/answers/new.html.erb:6:in `_app_views_answers_new_html_erb__3175854877830910784_6513500'
     # …
Run Code Online (Sandbox Code Playgroud)

rspec views ruby-on-rails nested-resources factory-bot

5
推荐指数
1
解决办法
1504
查看次数

Rails 3 - 处理控制器中嵌套资源查询的最佳方法?

如果我对Rails 3了解的一件事是,如果我很难做某事,我可能做错了.所以我正在寻求帮助.

我有一些与多对多关系相关的模型.

我能够在没有问题的情况下在模型中创建关联.我的问题在于如何构建控制器以使用这些关系.如果你不知道我要去哪里,我会试着举个例子.

例如...

class Account < ActiveRecord::Base
    has_many :locations
end

class Contact < ActiveRecord::Base
    has_many :locations
end

class Location < ActiveRecord::Base
    has_and_belongs_to_many :accounts
    has_and_belongs_to_many :contacts
end
Run Code Online (Sandbox Code Playgroud)

假设我有上述型号.这将是我的资源......

resources :accounts do
    resources :locations
end

resources :contacts do
    resources :locations
end

resources :locations do
    resources :accounts
    resources :contacts
end
Run Code Online (Sandbox Code Playgroud)

所以为了缩短这一点,我想说我想要一个帐户所有位置的列表.上述路线可能是账号/ 1 /位置.因此登陆我的位置#index.

希望我在这一点上没有搞砸我的例子,但是建立这个行动的最佳方式是什么,因为它确实有多个工作......至少是帐户,联系人和所有地点的位置.

所以我最终得到这样的东西......

class LocationController < ApplicationController
    def index
        if params[:account_id]
            @locations = Location.find_all_by_account_id(params[:account_id])
        elsif params[:contact_id]
            @locations = Location.find_all_by_contact_id(params[:account_id])
        else
            @locations = Location.all
        end

        respond_with @locations
    end
end
Run Code Online (Sandbox Code Playgroud)

更新#1:澄清一下,因为我得到一些答案,表明我改变了我的模型关系.我正在使用遗留系统,在此系统中我无法改变关系.最终我的目标是清理数据库和关系,但现在我不能.所以我需要找到一个适用于此配置的解决方案.

ruby activerecord ruby-on-rails nested-resources ruby-on-rails-3

4
推荐指数
1
解决办法
4574
查看次数

使用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
Run Code Online (Sandbox Code Playgroud)

意见/型材/ new.html.erb

<h1>About You</h1>
<div class="row">
  <div class="span6 offset3">
    <%= form_for(@profile) do |f| %>
    <%= render 'shared/error_messages' %>

      <%= f.label :name %>
      <%= f.text_field :name %>

      <%= f.label :surname %>
      <%= f.text_field :surname %>

      <%= f.submit "Create my account", …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails controllers form-for nested-resources ruby-on-rails-3

4
推荐指数
1
解决办法
2484
查看次数

在 WPF 中共享应用程序资源

这个页面,我读到:

如果应用程序使用自定义控件并在 ResourceDictionary(或 XAML 资源节点)中定义资源,建议您在应用程序或窗口对象级别定义资源,或者在自定义控件的默认主题中定义资源。在自定义控件的 ResourceDictionary 中定义资源会对该控件的每个实例产生性能影响。

好的...现在,我有一个定义以下资源的 UserControl:

<UserControl ...>
    <UserControl.Resources>
        <Namespace:ImagesConverter x:Key="ImagesConverter" ...
        <Storyboard x:Key="AnimationHide" ...
    </UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)

因此,由于我在运行时创建不少于 100 个实例,正如 MSDN 教程所述,最好将这些资源移动到主窗口或应用程序级别。将它们转移到哪里是最佳位置?主窗口级别、应用程序级别还是资源文件?为什么?

然后...我如何从新位置使用它们?假设我的 UserControl 中有以下代码:

m_AnimationHide = (Storyboard)Resources["AnimationHide"];
Run Code Online (Sandbox Code Playgroud)

我应该如何修改它以反映这些更改?我应该如何修改以下 UserControl XAML 片段?

Source="{Binding Source={x:Static Properties:Resources.MyImage}, Converter={StaticResource ImagesConverter}}"
Run Code Online (Sandbox Code Playgroud)

c# wpf nested-resources

4
推荐指数
1
解决办法
7101
查看次数

RSpec路由测试嵌套资源中params的问题

我有一个奇怪的问题.. rspec在spec/routing中生成了一个名为menus_routing_spec.rb的类

测试失败,因为菜单是餐馆的嵌套资源.

这是我的测试:

    describe MenusController do

  before :each do
    @restaurant = FactoryGirl.create(:random_restaurant)
    @menu = FactoryGirl.create(:menu)
  end

  describe 'routing' do
    it 'routes to #index' do
      params = {}
      params['restaurant_id'] = @restaurant


      #get('/restaurants/:restaurant_id/menus').should route_to('menus#index')
      #get(restaurant_menus_path(@restaurant)).should route_to('menus#index')
      #get(restaurant_menus_path, { :restaurant_id => @restaurant  }).should route_to('menus#index')

      get restaurant_menus_path, { :restaurant_id => @restaurant.to_param  }
      expect(response).should route_to('menus#index')
    end
Run Code Online (Sandbox Code Playgroud)

rake路由中的路径如下所示:

restaurant_menus_path    GET     (/:locale)/restaurants/:restaurant_id/menus(.:format)   menus#index
Run Code Online (Sandbox Code Playgroud)

我总是收到此错误消息:

Failure/Error: get restaurant_menus_path, @restaurant.to_param
     ActionController::UrlGenerationError:
       No route matches {:action=>"index", :controller=>"menus"} missing required keys: [:restaurant_id]
Run Code Online (Sandbox Code Playgroud)

我也尝试了其他的..但同样的错误..有谁能看到我在做错误的地方?

这是spec/controllers/menus_controller_spec.rb中的测试,它可以正常工作

it 'renders the index template' do …
Run Code Online (Sandbox Code Playgroud)

ruby rspec ruby-on-rails param nested-resources

4
推荐指数
1
解决办法
3118
查看次数

如何在Rails中使用带有嵌套资源的will_paginate?

我是Rails的新手,我在使用set_paginate处理嵌套资源方面遇到了很大的麻烦.

我有两个模型,声明和发票.will_paginate正在处理Statement,但我无法让它在Invoice上工作.我知道我会做些傻事,但我无法弄明白,我在谷歌上找到的例子对我不起作用.

statement.rb
class Statement < ActiveRecord::Base
  has_many :invoices

  def self.search(search, page)
    paginate :per_page => 19, :page => page,
      :conditions => ['company like ?', "%#{search}%"],
      :order => 'date_due DESC, company, supplier'
  end
end

statements_controller.rb  <irrelevant code clipped for readability>
def index #taken from the RAILSCAST 51, will_paginate podcast
  @statements = Statement.search(params[:search], params[:page])
end

I call this in the view like so, and it works:
  <%= will_paginate @statements %>
Run Code Online (Sandbox Code Playgroud)

但我无法弄清楚如何让它适用于发票:

invoice.rb
class Invoice < ActiveRecord::Base
  belongs_to :statement

   def self.search(search, page)
     paginate …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails nested-resources will-paginate

3
推荐指数
1
解决办法
5572
查看次数

路由中没有父级 ID 的嵌套资源

我有两个资源:

resources :users do
 resources :cars
end
Run Code Online (Sandbox Code Playgroud)

该协会是:

  • 一个用户可以拥有多辆车
  • 一辆车属于一个用户

当进行这种嵌套资源时,我得到的网址如下:

/users/:id/cars/new 
Run Code Online (Sandbox Code Playgroud)

我的问题是:

如果只做/cars/new(没有 /users/:id )更有意义,因为我:id从登录的 current_user 中获取 ,我将如何在路由中解决这个问题?

routes nested-resources ruby-on-rails-3

3
推荐指数
1
解决办法
2256
查看次数

正确的单一嵌套资源的路由

我有2个资源,一个是另一个的嵌套资源:

parent_resourcechild_resource.

这给了我以下路线:

somesite.com/parent_resources/14
somesite.com/parent_resources/14/child_resources/1
Run Code Online (Sandbox Code Playgroud)

然而child_resource,每个parent_resource人只有一个单独,所以对于使用该网站的人来说,这是非常令人困惑的.我希望child_resource路径看起来像这样:

somesite.com/parent_resource/14/child_resource
somesite.com/parent_resource/14/child_resource/edit
etc
Run Code Online (Sandbox Code Playgroud)

这样做的正确方法是什么?

我的routes.rb

resources :parent_resources do

   resource :child_resource do
   end

end 
Run Code Online (Sandbox Code Playgroud)

从导轨到导航:

A singular resourceful route generates these helpers:

new_geocoder_path returns /geocoder/new
edit_geocoder_path returns /geocoder/edit
geocoder_path returns /geocoder
Run Code Online (Sandbox Code Playgroud)

那节目怎么样?

我的路线由佣金路线产生:

parent_resource_child_resource      POST   /parent_resources/:parent_resource_id/child_resource(.:format)                 child_resources#create


new_parent_resource_child_resource  GET    /parent_resources/:parent_resource_id/child_resource/new(.:format)             child_resources#new

edit_parent_resource_child_resource GET    /parent_resources/:parent_resource_id/child_resource/edit(.:format)            child_resources#edit

                                    GET    /parent_resources/:parent_resource_id/child_resource(.:format)                 child_resources#show

                                    PUT    /parent_resources/:parent_resource_id/child_resource(.:format)                 child_resources#update

                                    DELETE /parent_resources/:parent_resource_id/child_resource(.:format)                 child_resources#destroy
Run Code Online (Sandbox Code Playgroud)

ruby url routes nested-resources ruby-on-rails-3

3
推荐指数
1
解决办法
628
查看次数

我无法使用accepts_nested_attributes_for创建模型对象.它不会创建嵌套对象

我的模型结构如下所示:

董事会has_many主题.主题has_many帖子.

应用程序/模型/ board.rb

class Board < ActiveRecord::Base
    has_many :topics
end
Run Code Online (Sandbox Code Playgroud)

应用程序/模型/ topic.rb

class Topic < ActiveRecord::Base
    belongs_to :user
    belongs_to :board
    has_many :posts

    accepts_nested_attributes_for :posts

    validates :title, presence: true, length: { maximum: 255 }
    validates :user_id, presence: true
    validates :board_id, presence: true
    ...
end
Run Code Online (Sandbox Code Playgroud)

应用程序/模型/ post.rb

class Post < ActiveRecord::Base
    belongs_to :user
    belongs_to :topic

    validates :user_id, presence: true
    validates :topic_id, presence: true
    validates :content, length: { minimum: 8 }
end
Run Code Online (Sandbox Code Playgroud)

以下是我创建新主题的观点.fields_for部分用于在新帖子上创建:内容

应用程序/视图/主题/ new.html.erb

<div>
    <%= form_for [@board, @topic] do |f| …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails associations nested-resources nested-attributes ruby-on-rails-4

3
推荐指数
1
解决办法
1847
查看次数