Tag: ruby on rails

Rails AJAX错误

我有: Click here 在控制器中: def index @all=Person.all @person=Person.where(id: params[:id]) respond_to do |format| format.html format.js end end 和: $(‘#click’).on(‘click’,function(){ $.ajax({ type: “POST”, data: ‘id=’+id, url: “/index” }); } index.js.erb的 $(“#show”).html(“”); _persons.html.erb index.html.erb 路线 get’/ index’,to:“main #index”put’/ index’,to:“main #index” 现在,当我得到索引动作时,我得到了 找不到id = nil的人 甚至在加载页面之前。 为什么@person实例变量直接执行? 我甚至无法进入索引页面。 rails ajax如何工作? 怎么知道@person变量是通过ajax执行的?

UTC格式的ROR + TodayDate

如果我有@today = Date.today.to_s ,如何将@today into UTC转换@today into UTC (仅适当的日期)? 我需要的只是2011-03-08的日期,即2011年3月8日。请提出建议? 我正在寻找昨天的日期?

查找其他用户在post上收到的用户总票数

我正在使用这个gem评论: https : //github.com/lml/commontator 哪个设置很容易插入这个gem投票评论: https : //github.com/ryanto/acts_as_votable 我正在使用rails 4 btw,它与两个gem兼容。 在我的用户模型中: class User < ActiveRecord::Base acts_as_voter acts_as_commentator has_many :comments end 在我的评论模型中: class Comment < ActiveRecord::Base acts_as_votable belongs_to :user end 一切似乎都很好。 但是在尝试计算用户总票数时(用户在所有评论中收到的总票数)(业力) 我收到这个错误 undefined method `votes’ for # 所以我尝试了这个: 这导致了另一个错误: SQLite3::SQLException: no such column: commontator_comments.commontator_id: SELECT “commontator_comments”.* FROM “commontator_comments” WHERE “commontator_comments”.”commontator_id” = ? AND “commontator_comments”.”commontator_type” […]

Gem的模型遗失了

我在我的Rails 4应用程序中使用邮箱 。 gem ‘mailboxer’ 我最近将它从0.11更新到0.12.4 ,它停止工作,我无法找出原因。 现在,我收到错误: uninitialized constant Message 我检查了gem的GitHub repo ,它确实有Message模型。 我检查了我的本地版本的gem,它匹配。 Puma:mailboxer Jeff$ pwd /Users/Jeff/.rvm/gems/ruby-2.0.0-p247/gems/mailboxer-0.12.4/app/models/mailboxer Puma:mailboxer Jeff$ ls conversation conversation.rb mailbox.rb message.rb notification.rb receipt.rb 为什么我的应用程序找不到该模型? 我该如何解决?

Controller中的NameError

我在尝试运行我编写的代码时遇到错误。 它是生成xml文件的代码,它从数据库中读取以获取所有信息。 错误是由两行,Document.new和Element.new引起的 如果我注释掉这两行,那么代码就可以了。 我不确定为什么这样做,因为那些类应该工作得很好。 我的代码如下: def create_google_file @products = Product.find(:all) file = File.new(‘dir.xml’,’w’) doc = Document.new root = Element.new “rss” root.add_attribute(“xmlns:g”, “http://base.google.com/ns/1.0”) root.add_attribute(“version”, “2.0”) channel = Element.new “channel” root.add_element channel title = Element.new “title” title.text = “Sample Google Base” channel.add_element title link = Element.new “link” link.text = “http://base.google.com/base/” channel.add_element link description = Element.new “description” […]

为什么这个Ruby on Rails代码不能像我想要的那样工作?

所以我试图在这个问题中构建我所问的内容: 修复投票机制 但是,此解决方案不起作用。 用户仍然可以多次投票他或她想要。 我怎么能修复这个和/或重构? def create @video = Video.find(params[:video_id]) @vote = @video.video_votes.new @vote.user = current_user if params[:type] == “up” @vote.value = 1 else @vote.value = -1 end if @previous_vote.nil? if @vote.save respond_to do |format| format.html { redirect_to @video } format.js end else respond_to do |format| format.html { redirect_to @video } format.js {render ‘fail_create.js.erb’} end […]

如何在页面加载时停止显示闪存错误消息,直到在轨道上的ruby中单击按钮后?

我正在试图找出一种方法来阻止此Flash消息在页面加载时显示: class SearchesController < ApplicationController def index @users = User.search params[:search] @default_image = "/assets/default_avatar.jpg" if @users.empty? || params[:search].blank? flash[:error] = "Sorry no user(s) found!" if @users.empty? flash[:error] = "Please give us something to search for!" if params[:search].blank? render 'index' end end end 我明白为什么它显示(显然当访问页面时,搜索参数已经是空白)。 在ruby轨道上必须有一些技巧,我可以在这个flash消息上使用它来阻止它点击,直到点击搜索按钮,例如 flash[:error] = “Please give us something to search for!” if params[:search].blank? […]

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 %> […]

没有具有belongs_to class_name关系的此类列

SQLite3::SQLException: no such column: organizations.user_id: SELECT “organizations”.* FROM “organizations” WHERE “organizations”.”user_id” = 2 我已经设置了这样的模型: 用户: User has_many :organizations 组织: attr_accessible :name, :founder, :founder_id belongs_to :founder, :class_name => ‘User’ 架构: create_table “organizations”, :force => true do |t| t.string “name” t.integer “founder_id” 当我在rails-admin中编辑用户时,我收到以下消息: `SQLite3::SQLException: no such column: organizations.user_id: SELECT “organizations”.* FROM “organizations” WHERE “organizations”.”user_id” = 2` 我想在组织上访问创始人,其中创始人是用户。 […]

TableA.create的底层代码是什么(TableB.all.map(&:attributes))?

例如,如果我在mongo ruby​​驱动程序中使用重命名方法,我可以在这里查看代码 当我使用map(&:attributes)时到底发生了什么? 我认为这意味着tags.map(&:attributes.to_proc).join(”),但我不知道为什么我得到“未定义的方法`each_pair’for Arrayxxxxx”错误与此命令: TableA.create(TableB.all.map(&:attributes)) 任何见解将不胜感激