繁体   English   中英

Ruby on Rails ActiveResource无法正常工作

[英]Ruby on Rails ActiveResource not working

我是RoR的新手,我正在尝试构建使用ActiveResource ActiveRecord的简单Web应用程序

我在localhost:3001上托管了一个简单的Rails项目。 应用程序具有如下所示的欢迎控制器

class WelcomeController < ApplicationController
  def index
    @persons = []
    num = 0
    until num < 10 do
      @persons[num] = Person.new
      @persons[num].name = [*('A'..'Z')].sample(8).join
      @persons[num].surname = [*('A'..'Z')].sample(64).join 
      @persons[num].dob = Time.at(rand * Time.now.to_i)      
      num+1      
    end

    respond_to do |format|
      format.xml { render :xml => @persons}
    end
  end
end

人员类如下所示:

class Person
  attr_accessor :name, :surname, :dob
end

此Rails应用程序应用作localhost:3000上托管的其他应用程序的REST服务

letter应用程序中的模型如下所示:

class Person < ActiveResource::Base
   self.site = "http://localhost:3001"
end

现在,我的问题是如何列出视图中的所有10个人?

我尝试在Person控制器中将Person模型用作ActiveResource:

class PersonController < ApplicationController
  def index
   @persons= Person.find(":all")   
  end
end

我在PersonController#index中收到消息ActiveResource :: ResourceNotFound

提前致谢。

首先,我不确定为什么要在WelcomeController中创建10个人,但这是一种更好的方法。

class WelcomeController < ApplicationController
  def index
    10.times do
      person = Person.new
      person.name = [*('A'..'Z')].sample(8).join
      person.surname = [*('A'..'Z')].sample(64).join
      person.dob = Time.at(rand * Time.now.to_i) 
      person.save # This part is necessary for data to persist between requests
    end

    @persons = Person.all

    respond_to do |format|
      format.xml { render :xml => @persons}
    end
  end
end

然后,当然可以在PersonController中使用

@persons = Person.all

要么

@persons = Person.find(:all)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM