Tag: ruby on rails 4

Rails 4嵌套表单 – 没有将Symbol隐式转换为Integer

在我的rails 4 app中,我有一个三重嵌套路线: devise_for :users do resources :foo do resources :marflar end end 我有一个用于创建带有嵌入式Marflar对象的新Foo的表单: 但是当我提交表格时,我得到: TypeError in FoosController#create no implicit conversion of Symbol into Integer 我的Foo控制器的相关部分如下所示: def new @foo = current_user.foos.build @foo.marflars.build end def create @foo = Foo.new(foo_params) if @foo.save redirect_to @foo else render action: ‘new’ end end .. def foo_params params.require(:foo).permit(:foo_attr, marflars_attributes: [:marflar_attr]) end […]

包含,选择,排序,限制多个模型(单个查询)

我需要创建一个包含以下表中数据的查询: *对话 :在用户之间对消息进行分组的模型 class Conversation :conversation_participants ## Attributes title, created_at, updated_at end * ConversationParticipant :跟踪对话用户的模型 class ConversationParticipant < ActiveRecord::Base ## Associations belongs_to :conversation belongs_to :user ## Attributes conversation_id, user_id, seen, created_at, updated_at end *消息 :保持跟踪内容和发件人的模型 class Message “User” ## Attributes sender_id, content, conversation_id, created_at, updated_at end *用户 :具有属性name的模型 如何在单个查询中获取以下内容? (5)来自uniq Conversation 消息的最近消息的限制 来自ConversationParticipant的 user_id = […]

Rails – 后台作业中的ActionDispatch :: Http :: UploadedFile

我正在使用与导入csv和excel Railscast类似的想法,但由于该剧集中的标准代码需要一些时间来处理(使用ActiveRecord为文件中的每一行创建新记录)我在Heroku上获得超时并希望将导入过程移至后台作业。 我没有成功将文件变量(类型为ActionDispatch :: Http :: UploadedFile)发送到作业,所以我发送了file.original_filename和file.path的各个变量。 作业失败,错误file /var/folders/q3/xn0bp7yd2m56_4lbq0069jj80000gn/T/RackMultipart20150319-72431-1a4pnja.xlsx does not exist ,我认为这种情况正在发生,因为该文件在作业开始之前已被删除: 上传的文件是临时文件,其生命周期是一个请求。 当对象完成后,Ruby取消链接文件,因此不需要使用单独的维护任务来清除它们。 ActionDispatch :: HTTP :: UploadedFile的 使用ActionDispatch :: Http :: UploadedFile上传的文件是否可以在后台作业中使用? 我正在使用Rails 4.2,ActiveJob和Resque

如何在Rails 4中访问模型中的polymorphic_path?

非常简单,我想在Rails 4模型中使用polymorphic_path方法。 是的,我知道关注点很差。 我知道Rails.application.routes.url_helpers ,但是polymorphic_path不在那里。

具有嵌套属性的Best_In_Place内联编辑

我目前正在尝试使用best_in_place gem来在HTML表格中进行内联编辑。 我在购物车的展示视图中显示了一个购物车。 在购物车的展示视图中,我可以添加lineItems。 创建LineItem时,还会使用lineItem_id创建新的可用记录,然后在购物车中显示其lineitem。 Cart和LineItem表都来自外部数据库,因此我无法添加列,这就是为什么我不能只为LineItem添加一个可用的布尔属性。 **cart.rb class Cart << AR::Base has many LineItems end **line_item.rb class LineItems <<AR::Base belongs_to Cart has_one :available accepts_nested_attributes_for :available end **available.rb class Available<<AR::Base belongs_to LineItems end **views/cart/show.html.erb @cart.lineitems.each do |line_items| line_items_path, :type => type: :checkbox, collection: %w[No Yes] %> end 我希望能够使用best_in_place编辑html表中的line_item.available.boolean,该表位于购物车展示视图中,但我没有任何运气..任何帮助都会令人惊叹! =]我知道在阅读之后,使用嵌套属性是不可能的,但是如果我能以某种方式摆脱可用模型并在show table中有一个字段,我可以为line_item编辑以查看lineItem是否可用那也很棒。 我对任何想法持开放态度!

CarrierWave文件上传无法在rails中运行

我需要将图像上传到我的电影collections应用程序我使用carrierwave来执行此操作( 按照railscasts步骤 ) 步骤1我将gem’carrierwave’,’〜> 0.9’添加到我的Gemfile然后运行bundle步骤2 rails g uploader image然后rails g scaffold filmes name moviestype rake db step 3 rails g migration add_image_to_filmes image:string然后rake db 其他步骤与railscasts相同 在我的电影模型中 attr_accessible :name, :moviestype, :image mount_uploader :image, ImageUploader 在我的_form.html.erb中 {:multipart => true} do |f| %> prohibited this filme from being saved: 在show.html.erb中 Name: Moviestype: | 问题是它没有上传任何图像,但电影名称和其他字段正在插入db.how我能解决这个问题吗?需要快速帮助。

范围内的find_by正在触发2个查询

我正在使用Rails 4.2.3和ruby 2.2.1 我在角色模型中写了一个范围如下: 应用程序/模型/ role.rb scope :default, -> { find_by(default: true) } 现在我跑的时候 > Role.default #this is the output I got. Role Load (0.1ms) SELECT `roles`.* FROM `roles` WHERE `roles`.`default` = 1 LIMIT 1 Role Load (0.1ms) SELECT `roles`.* FROM `roles` => [] 正如您所看到的,这会触发2个查询并返回错误的结果。 我尝试使用类方法而不是范围 def self.default self.find_by(default: true) end 现在我跑的时候 Role.default #this is […]

Rails redirect_to使用Ruby 2.2 / Rails 4.2的新行为?

尝试从Ruby 2.0 / Rails 4.0升级到Ruby 2.2 / Rails 4.2,登录我的应用程序时遇到了一个令人惊讶的错误(基于M. Hartl的Rails Tutorial): Controller: SessionsController#create Instruction: redirect_to root_url Error message: wrong number of arguments (2 for 1) 这是会话控制器: class SessionsController < ApplicationController def new end def create user = User.find_by_login(params[:session][:login]) if user && user.authenticate(params[:session][:password]) sign_in user redirect_to root_url else flash.now[:error] = 'Invalid login/password combination' render 'new' end […]

Rails 4:未定义的方法`primary_key_name’

我使用Rails 4.0.0.beta收到以下错误: NoMethodError: undefined method `primary_key_name’ for #<ActiveRecord::Reflection::AssociationReflection 使用Rails 3.2.x时,我没有得到exception。 我在Rails 3.2.13和Rails 4.0.0.beta上使用Ruby 1.9.3-p194。 问题源于以下has_many声明: class Store :relationships, :source => :user, :conditions => { :relationships => { :description => “Customer” } } do def < “Customer”, :user => user) end end end 我有以下支持课程: class User :relationship end class Relationship < ActiveRecord::Base belongs_to :store belongs_to :user […]

Rails 4多文件上传解决方案

有人有解决方案吗? 你能指出一个不是http://railscasts.com/episodes/381-jquery-file-upload的,因为它已经过时了。