Tag: ruby on rails 4

rails 4 ActiveModel :: ForbiddenAttributesError

我尝试使用rails 4进行简单的创建 我的控制器: class AdsController < ApplicationController def new @ad = Ad.new end def create @ad = Ad.new(params[:ad]) @ad.save end def show @ad = Ad.find(params[:id]) end def index @ads = Ad.first(3) end private def ad_params params.require(:ad).permit(:title, :price, :description) end end 形成: 从我的角度来看它没问题,但是我得到了这个错误ActiveModel::ForbiddenAttributesError我做错了什么? 更新: 我的问题是在创建操作中将错误的值传递给新方法:解决方案是将ad_params传递给它

如何在Rails中关闭MySQL严格模式

升级到Rails 4,似乎MySQL严格模式现在默认为Rails连接。 我这样说是因为我的Rails应用程序在保存超过255个字符的字符串值时会收到“Mysql2 :: Error:数据太长了”。 然而,我将相同的查询粘贴到MySQL控制台(其中报告全局严格模式已关闭)并且它正常工作,只是截断警告。 作为进一步的证据, 它在这里说 “Rails 4默认使用严格模式”。 我的问题是如何从Rails应用程序中关闭严格模式? 我宁愿避免升级所有东西来支持它。

在Windows上安装MySQL2 Gem

我有一些问题需要在我的Windows 8.1机器上安装mysql2 gem。 我按照这篇文章中的说明操作: 在Windows 7上安装Ruby MYSQL2 gem 安装mysql2 gem,我没有得到任何错误消息。 我跟着这个 bundle install 命令,并确认安装了以下gem(使用bundle show),确认我安装了以下gem: Using devise (3.2.2) Using mysql2 (0.3.14) 然后我尝试做: rails generate devise:install 这就是我得到的: C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/mysql2-0.3.14/lib/mysql2.rb:8:in `require’: 126: The specified module could not be found. – C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/mysql2-0.3.14/lib/mysql2/mysql2.so (LoadError) from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/mysql2-0.3.14/lib/mysql2.rb:8:in `’ from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/bundler-1.5.1/lib/bundler/runtime.rb:76:in `require’ from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/bundler-1.5.1/lib/bundler/runtime.rb:76:in `block (2 levels) in require’ from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/bundler-1.5.1/lib/bundler/runtime.rb:72:in `each’ […]

启用turbolinks重新渲染特定的js文件

我正在使用Rails 4并希望使用内置的Turbolink gem。 我很满意它所做的一切,除了我希望它在每个页面请求时重新加载一个js文件。 换句话说,我想要一个特定的js文件来忽略turbolinks,我该怎么做呢?

activeadmin中的嵌套表单不保存更新

我在ActiveAdmin中为这些模型设置了一个嵌套表单(a:class_section has_many:class_dates): class ClassDate true validates :end_time, :presence => true end 和 class ClassSection < ActiveRecord::Base belongs_to :class_course has_many :class_dates belongs_to :location accepts_nested_attributes_for :class_dates end 当我看到表格时,一切似乎都在正确的位置。 但是,当我更新class_date时,它不会保存。 ActiveAdmin.register ClassSection do permit_params :max_students, :min_students, :info, :class_course_id, :location_id form do |f| f.inputs “Details” do f.input :class_course, member_label: :id_num f.input :min_students, label: “Minunum Students” f.input :max_students, label: “Maxiumum […]

如何更改强参数(更改为小写)

所以我熟悉轨道和轨道4。 所以这就是我控制器底部的内容 def post_params params.require(:post).permit(:title, :content, :category) end 哪个工作正常,但我想要做的是弄清楚如何在post_params方法中或者稍后在控制器中单独访问这些参数。 具体来说,我想在创建post时使用参数之前将:category值更改为小写(因此在表中所有类别都是小写的)。 编辑:也许更好的措辞我的问题是,在允许参数后,我怎样才能访问和操纵它们。我可以像往常一样使用params [:title]吗? 我试过了 params.require(:post).permit(:title, :content, :category) params[:category].downcase 和 params.require(:post).permit(:title, :content) params.require(:post).permit(:category).downcase 但我得到undefined method ‘downcase’

咨询锁或NOWAIT以避免等待锁定的行?

在我的Rails 4应用程序中,我对Postgres 9.4数据库进行了此查询: @chosen_opportunity = Opportunity.find_by_sql( ” UPDATE \”opportunities\” s SET opportunity_available = false FROM ( SELECT \”opportunities\”.* FROM \”opportunities\” WHERE ( deal_id = #{@deal.id} AND opportunity_available = true AND pg_try_advisory_xact_lock(id) ) LIMIT 1 FOR UPDATE ) sub WHERE s.id = sub.id RETURNING sub.prize_id, sub.id” ) 非常受dba.SE相关答案的启发 。 我只是希望我的查询找到并更新第一个(随机,带LIMIT )行,其中available = true并将其更新为available = false ,我需要在执行此操作时锁定该行,但不要让新请求等待发布之前的锁定,因为有许多并发调用将使用此查询。 […]

导轨4带有涡轮和窗户负载

我正在使用Rails 4。 我正在尝试使用此脚本来显示谷歌地图。 我有一个不同地方的页面,每个地方都有一个地址。 它显示在谷歌地图中。 所以我使用分页,在每个页面上我有4个位置。 gmap的4个脚本。 但是这个脚本仅在页面重新加载(ctrl + R或F5)时初始化,这是因为turbolinks。 我怎样才能让它以最简单的方式工作? function initialize() { var myLatlng = new google.maps.LatLng(, ); var mapOptions = { zoom: 16, center: myLatlng }; var map = new google.maps.Map( document.getElementById(“map-canvas-“), mapOptions); var marker = new google.maps.Marker({ position: myLatlng, map: map, title: ” }); } google.maps.event.addDomListener(window, ‘load’, initialize); 这是剧本。 每个地图的div看起来像这样: <div […]

使用accepts_nested_attributes_for将现有has_many记录添加到新记录

将现有项模型添加到新的付款模型时,会出现错误“无法找到ID = 123的项目,ID = 123”。 这是一个has_many关系并使用accepts_nested_attributes_for。 class Payment < ActiveRecord::Base has_many :items accepts_nested_attributes_for :items … class Item < ActiveRecord::Base belongs_to :payment … 付款和项目模型是假设的,但问题是真实的。 在保存付款(在创建操作中完成)之前,我需要将项目与付款关联(就像我在新操作中一样)。 用户在创建付款时需要修改项目的属性(添加帐单代码就是一个例子)。 具体来说,错误发生在控制器的创建操作中: # payments_controller.rb def new @payment = Payment.new @payment.items = Item.available # where(payment_id: nil) end def create @payment = Payment.new payment_params # <– error happens here … end def payment_params […]

在Windows上安装Puma错误

尝试在Windows上安装Puma并收到此错误 – Temporarily enhancing PATH to include DevKit… Building native extensions with: ‘–with-opt-dir=c:\openssl’ This could take a while… ERROR: Error installing puma: ERROR: Failed to build gem native extension. C:/RailsInstaller/Ruby2.1.0/bin/ruby.exe extconf.rb –with-opt-dir=c:\openssl checking for BIO_read() in -lcrypto… no checking for BIO_read() in -llibeay32… no *** extconf.rb failed *** Could not create Makefile due to some […]