Tag: ruby on rails

如何在Object.get的输出中获取文件流

我在aws-sdk-ruby的帮助下编写了一个Rails API,它从AWS检索文件并在API的响应中返回。 我可以在object.get的响应中以某种方式得到文件流,我可以直接从Rails API返回。 s3 = Aws::S3::Resource.new bucket_name = “my_bucket” bucket = s3.bucket(bucket_name) object = bucket.object(“a/b/my.pdf”) Rails.logger.info ‘Downloading file to AWS’ downloaded_data = object.get({}) send_data(downloaded_data, :filename => “my.pdf”, :type => “mime/type” ) 但它不会返回文件。 我知道的一个选项是首先使用以下行在本地保存文件: object.get(response_target: ‘/tmp/my.pdf’) 我可以返回此文件, 但有没有办法跳过此步骤并直接返回object.get的响应而不保存在本地 。 我无法使用此解决方案,因为我的URL不公开,我只是创建一个REST API。 当我尝试这个解决方案时,我得到了屏幕。

Rails:从远程URL / API调用获取JSON数据

我试图从我的rails应用程序中获取一个json数据。 如何进行这个api通话。 { “name”: “MagicList”, “count”: 20, “frequency”: “realtime”, “version”: 5, “newdata”: true, “lastrunstatus”: “success”, “lastsuccess”: “Sat Apr 26 2014 14:34:40 GMT+0000 (UTC)”, “results”: { “collection1”: [ { “Title”: { “href”: “http://www.magicbricks.com/propertyDetails/2-BHK-746-Sq-ft-Multistorey-Apartment-FOR-Sale-Sector-79-in-Gurgaon&id=C0gt4k2Fa9tzpSvf+uAgZw==”, “text”: “2 BHK Multistorey Apartment in Sector-79” }, “Price”: “37.99 Lac(s)”, “Rate”: “5,092”, “Size”: “746 Sq-ft”, “Project Name”: “”, “New_Resale”: “New Property” }, […]

named_scope和方法有什么区别?

named_scope或scope与class方法有何不同。 named_scope :active, :conditions => {:status => ‘Active’} def self.active self.find(:all, :conditions => {:status => ‘Active’} end 这两者有什么区别?

如何让paginate工作而不是重定向到’public / index.html’?

我的主页使用’public / index.html’,它覆盖了我在这里阅读的所有内容。 因此,我已将所有已登录的用户重定向到http://localhost:3000/home ,我将其定义为get ‘/home’ => ‘static_pages#home’, :as => :home in my路线 我面临的问题是,在登录用户(/ home)的主页上,有分页。 当我尝试点击第2页http://localhost:3000/?page=2 ,它会被发送到public / index.html。 我尝试了链接http://localhost:3000/home/?page=2作为测试,但它给了我一个空白的分页。 没有显示任何项目。 我怎样才能解决这个问题? 这是控制器 class StaticPagesController 1, :per_page => 10) else redirect_to root_path end end 然后在我看来 这是_item.html.erb部分 <li id="”> posted made a comment shared this 150, :omission=>’ …(continued)’) %>

当我尝试做rake db:migrate时,我收到一个错误:ActiveRecord :: NODatabaseError角色“ubuntu”不存在

我通过这样做启动了postgresql服务器: sudo service postgresql start 然后我连接到服务: sudo sudo -u postgres psql 然后我创建了一个数据库(我正在尝试为我的应用添加一个投票系统): postgres=# CREATE DATABASE “votes”; 但我仍然有同样的问题。 还有,当我这样做的时候 rake db:create 我得到一个角色“ubuntu”不存在错误 这是我的database.yml: # SQLite version 3.x # gem install sqlite3 # # Ensure the SQLite 3 gem is defined in your Gemfile # gem ‘sqlite3’ # # default: &default # adapter: sqlite3 # pool: 5 […]

如何在轨道上的ruby中显示选择字段值而不是相关的存储整数?

例如,我允许我的用户在表单中选择他们的性别和婚姻状况,然后发布该表单,以便他们的选择存储在数据库中。 表单可能如下所示: Select Single Dating In relationship Married Living Together Divorced Separated Widowed 要访问它并在视图中显示存储的数据,我会这样做@ profile.marital_status。 问题是它将显示存储在db中的整数。 为了解决这个问题,我目前正在使用辅助方法: def get_gender(number) if number == 1 ‘Male’ elsif number == 2 ‘Female’ end end def get_marital_status(number) if number == 1 ‘Single’ elsif number == 2 ‘Dating’ elsif number == 3 ‘In relationship’ elsif number == 4 ‘Married’ elsif […]

ahoy events在rails中发布错误

我最近启动了代码轨道,我需要弄清楚这个问题。 我特别为我的项目使用此function。 所以我自定义名称和属性变量。 ahoy.track(name, properties); 在本地没有问题,但在实时版本中,当跟踪方法运行时,我收到404错误。 此行创建对/ ahoy / events的POST请求,并且实时版本中没有路径/ ahoy /事件。

如何在用户登录后立即将用户发送到他们的个人资料?

我有一个与设计相关的用户模型。 Userinfo模型属于User模型。 userinfo模型通过表单接收一些信息(姓名,电子邮件,学校……)。 现在,只要用户注册或登录,程序就会检查current_user是否填写了信息,如果没有,他们将被路由以填写信息页面。 如果他们填写了信息,他们将被路由到主页,即userinfo索引页面。 索引页面显示站点上所有用户的userinfo。 路由的工作方式: Userinfo模型: class Userinfo < ActiveRecord::Base belongs_to :user has_many :videos, through: :user def info_complete? name? && email? && college? && gpa? && major? end end 应用控制器: def after_sign_in_path_for(resource) if resource.userinfo.info_complete? root_path else new_user_info_path end end 问题 当用户登录时,如果用户填写了userinfo,我希望它转到用户的个人资料页面,而不是索引或根路径。 配置文件路径是“userinfo #show”。 对于userinfo #index,不需要id,但要转到“userinfo#show”,需要id。 因为userinfo #index是’/ userinfos’,而userinfos #show是’/ userinfos / userinfo_id’。 我的问题是,如何在用户登录后将用户路由到userinfo#show,个人资料页面以及是否填写了userinfo数据? […]

Rails嵌套属性被复制

这个问题打败了我。 我整个周末一直在这里,但无法弄清楚发生了什么。 当我创建一个新的employee对象时,我accepts_nested_attributes_for :ee_pay在我的employee模型中使用accepts_nested_attributes_for :ee_pay创建多个新的ee_pay记录。 为新员工创建ee_pay记录取决于员工所属公司的company_pay对象数量。 所以在下面的例子中有2个company_pay对象 – >“Basic”[id:2]和“Time + 1/2”[id:3](在before_action返回)并且我想为每个对象创建一个ee_pay有问题的员工 这是代码: – employees_controller.rb before_action :set_company_pay_types, only: [:new, :update, :edit] def new @ee_pay_types = [] @taxable_pays.each do |pt| @ee_pay_types << @employee.ee_pay.build(company_pay_id: pt.id) end end private def set_company_pay_types @taxable_pays = CompanyPay.where(['company_id = ? AND pay_sub_head_id = ? AND description ?’, current_company.id, 1, “Salary”]).order(:id) end def employee_params […]

升级Ruby打破了Rails?

因此,我将Ruby升级到1.8.7以使特定的gem工作。 万岁,它现在有效! 但是……现在铁路没有。 我尝试通过gem重新安装rails,但这也没有帮助。 我的Windows框不再将Rake识别为有效命令,我甚至无法在没有完整错误的情况下启动服务器。 我唯一的想法是,也许我不应该复制我的gems文件(一个教程提到它是通过升级在Windows上保存我的gem的唯一方法)。 我将尝试重新安装1.8.7而不更改任何内容,并根据需要重新安装所有内容。 编辑:好的,只是简单的1.8.7确实有效,但是手动重新安装我的所有gem令人沮丧…在Windows中有更好的解决方案吗?