ruby-on-rails-3.2

Rails database defaults and model validation for boolean fields

无人久伴 提交于 2019-11-27 14:58:40
In a Rails model I have an attribute is_subscriber , when I constructed a db migration to add this column to the database I specified the default value to be false: t.boolean "is_subscriber", :default => false I also specified in the model that this attribute needs to be present: validates :is_subscriber, presence: true So why do I get this error when I create a model instance without specifying this attribute? 2012-05-08T21:05:54+00:00 app[web.1]: ActiveRecord::RecordInvalid (Validation failed: Is subscriber can't be blank): From here If you want to validate the presence of a boolean field

Devise log after auth failure

我怕爱的太早我们不能终老 提交于 2019-11-27 14:03:10
问题 I need to write a log when somebody failes to log in to my app (to track bruteforce attempts). Also I decided to log successful authentications. So I created a SessionsController < Devise::SessionsController and tried to override the sessions#create method like that: https://gist.github.com/3884693 The first part works perfectly, but when the auth failes rails throws some kind of an exception and never reaches the if statement. So I don't know what to do. 回答1: This answer to a previous SO

Using Rails link_to for links that post

放肆的年华 提交于 2019-11-27 11:44:47
I have a link that I need to submit a post request with. Normally, I'd use jQuery and prevent the link's default behavior and then submit a form to the destination. This seems like something Rails should be able to help me out with. Sure enough, the link_to method has an option for specifying a POST http method: link_to "Profile", 'http://example.com/profile', method: :post That works, but I need to add 2 parameters too. I tried: link_to "Profile", 'http://example.com/profile', method: post, param1: 'value1', param2: 'value2' That just added those parameters to the <a> HTML element, but didn't

strong parameters permit all attributes for nested attributes

泪湿孤枕 提交于 2019-11-27 11:34:48
Is there a way in strong parameters to permit all attributes of a nested_attributes model ? Here is a sample code. class Lever < ActiveRecord::Base has_one :lever_benefit accepts_nested_attributes_for :lever_benefit end class LeverBenefit < ActiveRecord::Base # == Schema Information # id :integer not null, primary key # lever_id :integer # explanation :text end For lever strong parameters i am writing currently this def lever params.require(:lever).permit(:name,:lever_benefit_attributes => [:lever_id, :explanation]) end Is there a way for nested attributes i can write to permit all attributes

rake assets:precompile RAILS_ENV=production not working as required

不想你离开。 提交于 2019-11-27 11:08:47
问题 I am trying to precompile assets using the command rake assets:precompile RAILS_ENV=production , but I always get the error below. ** Invoke assets:precompile (first_time) ** Execute assets:precompile /usr/bin/ruby /usr/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets --trace ** Invoke assets:precompile:all (first_time) ** Execute assets:precompile:all ** Invoke assets:precompile:primary (first_time) ** Invoke assets:environment (first_time) ** Execute assets

How to get ActiveAdmin to work with Strong Parameters?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 10:07:52
问题 Update: this question was asked before there was a solution for it already in ActiveAdmin. As Joseph states, the ActiveAdmin documentation now contains this information, but the answers here are provided for those working with older versions of ActiveAdmin. When the strong_parameters 0.1.4 is used with ActiveAdmin 0.5.0 in Rails 3.2.8, if the model you are using is using StrongParameters by including: include ::ActiveModel::ForbiddenAttributesProtection then you get the following error in the

Using send_file to download a file from Amazon S3?

给你一囗甜甜゛ 提交于 2019-11-27 06:16:07
I have a download link in my app from which users should be able to download files which are stored on s3. These files will be publicly accessible on urls which look something like https://s3.amazonaws.com/:bucket_name/:path/:to/:file.png The download link hits an action in my controller: class AttachmentsController < ApplicationController def show @attachment = Attachment.find(params[:id]) send_file(@attachment.file.url, disposition: 'attachment') end end But I get the following error when I try to download a file: ActionController::MissingFile in AttachmentsController#show Cannot read file

How to set a background image in rails from css?

我是研究僧i 提交于 2019-11-27 05:31:15
问题 I am using rails 3.2 and i have to set a background for one of the page and i have tried many ways and nothing went right, so looking for some good help. I have tried background: url(<%= asset_path 'background.jpg' %>) background: url("public/background.jpg"); background-image:url('/assets/images/background.jpg') and nothing worked. Please help me. 回答1: In your CSS: background-image: url(background.jpg); or background-image: url(/assets/background.jpg); In environments/production.rb : #

Ruby on Rails Best practices - Big Controller vs Small Controller

眉间皱痕 提交于 2019-11-27 05:13:12
I need some informations for the best practices in Ruby on Rails, especially with Controller who have to do a lot of things , so, a simple "show" action is now up to lines. I know, it's not really good, and I have specific code in it. Here is a sample code: def show sound = Sound.find(params[:id]) @xml_path = File.dirname(sound.file.path) s3 = AWS::S3.new( :access_key_id => 'XXX', :secret_access_key => 'XXX') @url = s3.buckets['dev'].objects[sound.file.path[1..-1]].url_for(:read, :expires => 10*60) if sound.id_job != 0 && sound.transcript_progress != 100 @response = Savon.client("http://srap

Rails Routes based on condition

送分小仙女□ 提交于 2019-11-27 04:31:41
I have three roles: Instuctor, Student, Admin and each have controllers with a "home" view. so this works fine, get "instructor/home", :to => "instructor#home" get "student/home", :to => "student#home" get "admin/home", :to => "admin#home" I want to write a vanity url like below which will route based on the role of the user_id to the correct home page. get "/:user_id/home", :to => "instructor#home" or "student#home" or "admin#home" How do I accomplish this? tadman You can't do this with routes because the routing system does not have the information required to make this decision. All Rails