Tag: ruby on rails 3.1

如何在rails 3.1.1中将图像写入文件系统?

我觉得我应该非常接近,但我不能让这个工作得恰到好处。 我正在使用filegnsfer从我的phonegap应用程序发送图像到我的rails应用程序(3.1.1 ruby​​ 1.8.7顺便说一句)。 我想将文件写入文件系统。 这是params哈希: Parameters: {“file”=>#<ActionDispatch::Http::UploadedFile:0x7f1e2baa94e8 @tempfile=#, @headers=”Content-Disposition: form-data; name=\”file\”; filename=\”image.jpg\”\r\nContent-Type: image/jpeg\r\n”, @content_type=”image/jpeg”, @original_filename=”image.jpg”>} 基本上,我可以通过params [:file]访问该文件。 现在我需要将该图像写入我的文件系统。 我没有图像对应的模型,所以我真的不需要回形针。 现在,我能想到的最好的是: open(params[:file]) do |image| File.open(“theiamge.jpg”,”wb”){|file| file.puts image.read } end 这是我得到的错误: TypeError (can’t convert ActionDispatch::Http::UploadedFile into String): 编辑: 所以,这很奇怪。 我可以在我的irb中使用这些命令: File.open(‘newimage.jpg’, ‘wb’) do |f| f.write File.open(‘oldimage.jpg’).read end 但是,这不起作用: File.open(“ihatedetails.jpg”,’wb’) do |f| f.write File.open(params[:file]).read end 我收到此错误:TypeError(无法将ActionDispatch :: […]

使用js jQuery的Rails 3.1中的错误 – “模板丢失”

我根据这篇文章http://stjhimy.com/posts/7-creating-a-100-ajax-crud-using-rails-3-and-unobtrusive-javascript创建了应用程序,但收到错误: Missing template posts/create, application/create with {:handlers=>[:erb, :builder, :coffee, :haml], :formats=>[:html], :locale=>[:en, :en]}. Searched in: * “/home/ember/Projects/test_app/app/views” 也许在Rails 3.1中,动作默认呈现一些东西。 此外,没有格式js(:formats => [:html])。 但我找不到任何关于这一点。 我这样做的时候: respond_to do |format| format.js {render :content_type => ‘text/javascript’} end 或没有content_type – 浏览器重定向到空白页面。 在这种情况下: respond_to do |format| format.js format.html {render :nothing => true} end 浏览器显示相同的空白页面。 如果没有重定向,我怎么能这样做? 也许这是旧方法?

使用Rails将日历转换为表单?

问题 :如何将日历转换为呈现日期选择以匹配日历日而不是当前日期的表单? 我正在尝试做什么:我想创建一个显示骑车者锻炼的日历。 在每个日历日内创建锻炼,自动渲染与表单中给定日期匹配的锻炼日期 每次锻炼都有很多间隔。 每个日历日都有一次锻炼。 插件:从https://github.com/p8/table_builder中提取table_builder.rb和calendar_helper.rb(gem和插件都不使用rails 3.1进行部署) class WorkoutsController.rb def new @workout = Workout.new @date = params[:month] ? Date.parse(params[:month].gsub(‘-‘, ‘/’)) : Date.today 3.times do @workout.intervals.build end end Workout.rb attr_accessible :workoutdate, :intervals_attributes has_many :intervals, :dependent => :destroy accepts_nested_attributes_for :intervals, :reject_if => lambda { |a| a[:interval_name].blank? }, :allow_destroy => true Interval.rb attr_accessible :interval_name, :workout_id belongs_to :workout […]

可以在Rails 3.1中使用<<运算符插入多个对象吗?

我可以写以下内容吗? raw_data.categories.each do |category| obj.categories << category end 如下呢? … obj.categories << raw_data.categories

取消删除acts_as_paranoid在设计登录时删除用户

我有一个Rails 3.1.3应用程序,它使用devise进行用户身份validation,并使用acts_as_paranoid对其进行软删除。 我希望在密码重新创建,用户注册和用户登录时取消删除帐户,因此如果他们提供已删除的电子邮件,我会抓取该帐户,重新启用该帐户,然后继续操作(密码重新创建或登录) )。 但是在Users::SessionsController#create #create操作中,取消删除用户后会收到Unauthorized错误(但用户现在应该可见)。 代码是: def create # Take into account acts_as_paranoid deleted users resource = resource_class.only_deleted.find_by_email(params[resource_name][:email]) resource.undelete! if resource resource = warden.authenticate!(:scope => resource_name, :recall => “#{controller_path}#new”) set_flash_message(:notice, :signed_in) if is_navigational_format? sign_in(resource_name, resource) respond_with resource, :location => after_sign_in_path_for(resource) end 如果我在取消删除后添加一个resource.reload调用它不会改变任何东西。 如果我再次登录,用户通常会登录,因为它在之前的尝试中未被删除。 为什么会这样? 如何在一次create调用中取消删除并登录?

Ruby on Rails:同一表单中的多个相同输入字段

有一个页面,其中有相同的东西的多个输入字段,post。 现在,当用户输入问题时,假设3个字段,保存到数据库的唯一字段是最后一个。 然而,它应该保存所有三个并给它们每个自己的post_id。 也; 如果用户没有为其他字段输入任何内容,则它也不应保存在数据库中。

巨大的json对象出类拔萃?

我有一个PHP应用程序,它有一个超过10000行的表,我试图通过我的ROR应用程序将其导出到excel表,但我的请求是通过PHP应用程序在服务器上超时。所以我想知道是否有任何优雅的方式来解决这个问题。 我想出了两个解决方案。 首先是进行批处理(需要阅读我的新内容),另一个是我的php应用程序将发送一个大的json对象,我的ruby应用程序将读取json对象将数据写入excel表并发回excel sheet。所以我想问一下是否有更好的方法来处理这个问题? 我怎样才能将json转换为excel我做了google并且确实找到了excel到json但反之亦然。 有什么建议?

是Rails 3.1 Edge打破XmlMarkup :: Builder?

网上有很多例子(例如http://techoctave.com/c7/posts/32-create-an-rss-feed-in-rails ),展示了如何使用Builder制作精美的RSS源。 规范模板是这样的: xml.instruct! :xml, :version => “1.0” xml.rss :version => “2.0” do xml.channel do xml.title “Your Blog Title” xml.description “A blog about software and chocolate” xml.link posts_url for post in @posts xml.item do xml.title post.title xml.description post.content xml.pubDate post.posted_at.to_s(:rfc822) xml.link post_url(post) xml.guid post_url(post) end end end 这在Rails 3.0.7中工作正常。 在Rails 3.1 Edge中,每个命令似乎都会产生…… Rendered home/index.rss.builder (25.2ms) […]

Rails 3.1:使用非null布尔mysql列保存记录的问题(false保存为NULL)

使用Rails 3.1rc5并设计1.4.2。 我在users表上有以下列 add_column :users, :has_dummy_password, :boolean, :default => false, :null => false 如果我使用现有用户记录执行以下操作,则不使用:null => false子句… user.has_dummy_password = false user.save …然后在mysql中,列的值为NULL。 使用:null => false子句我(毫不奇怪)得到以下mysql错误: Column ‘has_dummy_password’ cannot be null 我可以通过这样做来解决这个问题 user.has_dummy_password = 0 user.save 因为“引擎盖下”布尔值在mysql中实现为tinyint。 这看起来有点不幸。 是否可以在Rails 3.1而不是1/0中实际设置布尔列值为true / false?

在db中始终使字段为小写的简单方法

目前我正在模型中执行以下操作: before_save :to_lower before_create :to_lower def to_lower self.name = self.name.downcase end 对我来说似乎很重复。