Tag: ruby on rails 3.2

ActiveModel :: MassAssignmentSecurity ::嵌套属性出错

嗨我试图在我的rails应用程序中创建一对多的关系。 拳头我创建我的模型 class Produto < ActiveRecord::Base attr_accessible :compra, :descricao, :estoque, :venda has_many :precos accepts_nested_attributes_for :precos end class Preco < ActiveRecord::Base attr_accessible :compra_decimal, :produto_id, :venda_decimal belongs_to :produto end 然后我创建了我的控制器 class ProdutosController < ApplicationController def new @produto = Produto.new @produto.precos.build end def create @produto = Produto.new(params[:produto]) if @produto.save? redirect_to produtos_path end end end 在此之后我创建了我的.html.erb页面: _形成 builder %> […]

使用Multipart Form在Ruby on Rails 3上无法使用文件上传

我有一个非常简单的配置来上传文件。 我只想将图像上传到数据库(我知道,我知道……)并附上说明。 而已。 这是我的模型的样子(我从这里学到的)。 基本上,它正是设置示例的方式,只有attr_protected行才能使用Rails 3.2: Photo < ActiveRecord::Base # note: there is also a :description attribute!! attr_protected :file_name, :content_type, :binary_data def image_file=(input_data) self.file_name input_data.original_filename self.content_type = input_data.content_type.chomp self.binary_data = input_data.read end end 这是我非常简单的视图: New Photo Upload a Photo Photo 最后(仅为完整性),这是我的Create动作(简单的脚手架): def create @photo = Photo.new(params[:photo]) respond_to do |format| if @photo.save format.html { redirect_to @photo, […]

如何在轨道上的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 […]

Rails 3 has_and_belongs_to_many关联:如何分配相关对象而不将它们保存到数据库

使用has_and_belongs_to_many_association class Category has_and_belongs_to_many :projects end 我想在保存类别之前使用before_filter来设置项目 before_filter :set_projects, :only => [:create, :update] def set_projects @category.assign_attributes({projects: Project.all}) end 这种方法很有效,除非无法保存类别并且存在回滚。 项目仍在数据库中更新。 为什么这一行 @category.assign_attributes({projects: Project.all}) 立即生成这些数据库记录? BEGIN INSERT INTO “categories_projects” (“category_id”, “project_id”) VALUES (86, 1) INSERT INTO “categories_projects” (“category_id”, “project_id”) VALUES (86, 2) INSERT INTO “categories_projects” (“category_id”, “project_id”) VALUES (86, 3) COMMIT 在提交这些新的categories_projects关系之前,我想等待@ category.save。 如何推迟这些提交? 请注意,我无法修改主“更新”操作。 我必须在filter和回调之前/之后使用,以覆盖我的应用程序的当前function。 […]

如何使用rails保护质量分配中的关联保存到数据库

尝试几个小时后,我无法保存到数据库。 上下文是这样的:我有两种类型的用户,一种用于我只需要非常基本的信息[用户名,电子邮件,密码]和另一种用户,我需要大量的信息[年龄,性别,城市等等] ] 我没有使用STI,因为表中会有大量的Null值。 所以我创建了这三种模式,其中用户具有简档(简档表)或不具有取决于其类型[1或2],并且该简档的字段是该用户所居住的城市,其与另一个表相关。数据库,城市表 class User < ActiveRecord::Base has_one :profile has_one :city, through: :profile end class Profile < ActiveRecord::Base belongs_to :user belongs_to :city […a bunch of fields here] end class City < ActiveRecord::Base has_many :profiles has_many :users, through: :profiles end 当我在rails控制台中玩它们时一切顺利: usr = User.new(name: “roxy”, email: “roxy@example.me”, password: “roxanna”, password_confirmation: “roxanna”, utype: 1) cty […]

Rails如何在2个日期之间创建每月的图表数据?

在我的模型中,我有: def self.chart_data(start, slut) if start.nil? start = 2.weeks.ago end if slut.nil? slut = Date.today end range = start.to_date..(slut.to_date + 1.day) if start.to_date.beginning_of_month.to_s != slut.to_date.beginning_of_month.to_s kliks = count( :group => ‘date(month)’, :conditions => { :created_at => range } ) # CREATE JSON DATA FOR EACH MONTH – PROBLEM HERE (range).map(&:beginning_of_month).uniq.map(&:to_s).each do |month| { created_at: month, […]

我的开发环境已经消失,我在尝试修复它时遇到了一系列错误

所以我和我的一个朋友搞乱了gem,我不小心删掉了一个。 我不确定发生了什么事,所以我只是把它擦掉了。 然后我去为我的旧应用程序运行’rails server’,我一直在努力,没有任何事情发生在2分钟,然后最终它吐出一个错误。 /Users/Nick/.rvm/bin/ruby: line 6: /Users/Nick/.rvm/bin/ruby: Argument list too long /Users/Nick/.rvm/bin/ruby: line 6: /Users/Nick/.rvm/bin/ruby: Undefined error: 0 那么我做了下面提出的所有建议,我遇到了各种各样的错误。 如果我要为每个错误创建一个单独的问题,我认为我会弄乱堆栈溢出,所以我想在这里给出一些解决这个问题的顺序。 现在我正在试图回溯并弄清楚发生了什么……每当我在答案中运行这些不同的命令时我就会改变一些事情而我不知道发生了什么。 当我尝试安装ruby版本时,我收到此错误: apple-gcc42 is not available in PATH, try: brew unlink apple-gcc42 && brew link apple-gcc42 and make sure that apple-gcc42 is available in your PATH: which gcc-4.2 我在某处读到安装xCode会解决这个问题,所以我安装了它。 虽然我得到了同样的错误。 当我尝试运行该错误的第一个建议时,我得到了这个: Unlinking /usr/local/Cellar/apple-gcc42/4.2.1-5666.3… 0 […]

寻找一个好主意和一个良好的Ruby结构

我正在编写一个Rake任务来填充数据库以用于开发目的,以便使用一些数据。 (但在测试中我使用的是FactoryGirl,所以不要谈论它。) 那么假设我的演示数据库中有两个示例组织,所以我已经像这样定义了它们: sample_organizations = [ ’37 Signals’, ‘Fog Creek’] 然后是一个小方法来填充数据库,如下所示: def create_organizations sample_organization.each { |item| Organization.first_or_create( name: item, time_zone: ‘Central’ ) } end 好的是,从现在开始两个月,如果我想添加第三个组织,我只需要查看我的代码,然后在该数组中输入另一个组织名称。 方法仍然有效并创建它。 现在开始这个问题:我有更多的表来填充! 例如, Summaries表悬挂在Organization表之外,因此每个组织可以有许多Summaries,因此我们在Summary表中也有一个organization_id foreign key来填充。 但是我仍然希望有一个像create_summaries这样的方法,类似于上面的create_organization方法,但这次填写了Summaries表,而棘手的部分是填写表的organization_id字段。 我该怎么办呢? 假设Summaries表包含以下字段: id (auto generate by Rails) , organization_id, name, member_count

轨道上的ruby中的嵌套属性

我想在ruby中创建评论,但我有问题 1)posts_controller.rb def comment Post.find(params[:id]).comments.create(params[:comment]) flash[:notice] = “Added your comment” redirect_to :action => “show”, :id => params[:id] end 2)show.html.erb “comment”, :id => @post %> 3)post.rb class Post include Mongoid::Document include Mongoid::Timestamps::Created include Mongoid::Timestamps::Updated field :title, type: String field :content, type: String field :user_id, type: Integer field :tag, type: String field :owner, type: String embeds_many :comments […]

无法将印象派模型添加到Solr

为了跟踪独特的观点,我添加了印象派gem 。 由于流量较高, 展示次数表会以更快的速度增长。 当我想要显示连续几周/几个月之间的比较时,这将导致问题。 所以我想到将impression’s模型与solr联系起来。 为了实现这一点,我首先在app/models/impression.rb文件夹下创建了一个名为impression的 app/models/impression.rb 。 class Impression [ :updated_at] do text :message, :boost=> 2.0 text :referrer text :ip_address integer :user_id time :created_at time :updated_at string :message string :view_name string :ip_address string :impressionable_type string :controller_name string :action_name string :session_hash string :request_hash integer :id integer :impressionable_id end end 然后我查看了一些实施印象派的博客文章,然后进行了重新索引。 bundle exec rake sunspot:reindex[,Impression] […]