ruby-on-rails-4.2

How to assign a variable the current output of a select tag

时间秒杀一切 提交于 2019-12-11 10:47:51
问题 I'm having some trouble with a select tag. I want to assign a value to a variable depending on which name is shown on the select tag: <%= select('product', 'name' , @products.map { |s| [s.name, s.id] }, {}, {:class => "form-control"}) %> The values display correctly, but I want to assign the currently selected value to a variable so that it can be used later on the same page to display content with the relevant details of the selected product. For example: @product = currently_displayed_name

Rails 4 Authentication model with roles

醉酒当歌 提交于 2019-12-11 09:08:25
问题 I am beginner in Rails world, so hoping I will be able to find an answer here. The project that I am working on, has to have User Authorization with roles, for simple users and for admins. With admin privileges I want to be able reset password for simple users or to add roles for them. I was trying to apply Devise with cancancan gems, but unfortunately, couldn't make it work and I am not sure if that is even possible. So my question is which gems would you recommend to have such behavior. Or

Rails 4: Receiving the following error: First argument in form cannot contain nil or be empty

混江龙づ霸主 提交于 2019-12-11 06:41:52
问题 I am receiving the following error on a project of mine: First argument in form cannot contain nil or be empty. I am trying to create an edit page for my code. Fairly new with Rails and trying to learn without scaffolding. Controller: class BooksController < ApplicationController def new @book = Book.new @authors = Author.all end def edit @book = Book.find(params[:id]) end def show #Notice how the @books is plural here. @books = Book.all @authors = Author.all #@books = Book.where(id: params[

How to run code automatically when launching a Rails console?

偶尔善良 提交于 2019-12-11 03:52:35
问题 Let's say I wanted a greeting every time the Rails console comes up: Scotts-MBP-4:ucode scott$ rails c Loading development environment (Rails 4.2.1) Hello there! I'm a custom greeting 2.1.5 :001 > Where would I put the puts 'Hello there! I\'m a custom greeting' statement? Another Stackoverflow answer suggested, and I've read this elsewhere too, that I can put that in an initializer like this: # config/initializers/console_greeting.rb if defined?(Rails::Console) puts 'Hello there! I\'m a

How can I override the .. and … operators of Ruby Ranges to accept Float::INFINITY?

旧街凉风 提交于 2019-12-11 00:14:38
问题 I want to override the .. and ... operators in Ruby's Range . Reason is, I'm working with infinite date ranges in the database. If you pull an infinty datetime out of Postgres, you get a Float::INFINITY in Ruby. The problem with this is, I cannot use Float::INFINITY as the end of a range: Date.today...Float::INFINITY => Wed, 02 Nov 2016...Infinity DateTime.now...Float::INFINITY # ArgumentError: bad value for range Time.now...Float::INFINITY # ArgumentError: bad value for range ... yet I use .

Rails gem update not working (version 4.1.1 to 4.2.0) as a solution to “warning: circular argument reference” error

只谈情不闲聊 提交于 2019-12-10 19:53:49
问题 I keep getting the following error when I run any sort of rake command, and in particular rake db:seed which will abort without completing: $ rake db:seed /Users/rachel/.rvm/gems/ruby-2.2.0@rails4.2/gems/activesupport-4.1.1/lib/active_support/values/time_zone.rb:285: warning: circular argument reference - now rake aborted! ActiveRecord::UnknownAttributeError: unknown attribute: description I did some research and discovered this is an error (I think?) caused by an incompatible gem in Rails 4

NameError (uninitialized constant Unzipper::Zip) but only on Heroku deploy (Rails)

一世执手 提交于 2019-12-10 14:32:40
问题 I have a class unzipper.rb that unzips a file using Rubyzip. In my local environment, I can succesfully unzip a file without explictly including the dependency using require 'zip' On Heroku though, I get an NameError (uninitialized constant Unzipper::Zip) which I could only resolve by using the explict require Question: Why would this be necessary in the Heroku environment, but not on localhost? I was under the impression that Rails required all gems automatically. app/services/unzipper.rb

Rails Groupify gem

*爱你&永不变心* 提交于 2019-12-08 04:49:59
问题 I set it up as per the documentation (I think): https://github.com/dwbutler/groupify But then in the console: user=User.new group=Group.new group.add user user.in_group?(group) => false Returns false, instead of true. Problem. I don't get it. I've a few different things and no joy. I'm stuck. My latest attempt is here: https://github.com/Yorkshireman/sebcoles/tree/setup3 (setup3 branch) I have a previous attempt on the setup2 branch. There is little difference between the two branches (the

respond_with alternatives in rails 4.2 for backbone

岁酱吖の 提交于 2019-12-07 18:50:17
问题 In rails 4.2 respond_with and respond_to have been moved to the responders gem. I've read that this is not the best practice. I use backbone.js for my app. For render all users in controller I use: class UsersController < ApplicationController respond_to :json def index @users = User.all respond_with @users end end What are the alternatives? 回答1: It's only respond_with and the class level respond_to that have been removed as indicated here. You can still use the instance level respond_to as

Redirect same page to different language with Globalize & Friendly_id

三世轮回 提交于 2019-12-07 01:07:10
问题 I've been scratching my head for the last few hours, looking for an answer but I can't find it anywhere. My gem file: # Use globalize for translating models gem "globalize", github: "ncri/globalize" # for Rails 4.2 gem 'globalize-accessors', '~> 0.1.5' # Use friendly_id for slugs gem 'friendly_id', '~> 5.1.0' gem 'friendly_id-globalize', '~> 1.0.0.alpha1' Here's the situation: I have two languages "en" and "fr" 2 models : pages and pages_translations pages has a slug column, pages