FAQ Database Discussion Community

## Can't map a range of dates in Ruby/Rails

ruby-on-rails,ruby
I'm trying to map a range of dates and pass them to my view as an array, as follows: from, to = Date.parse("2014-01-01"), Date.yesterday date_range = (from..to) @mapped_dates = date_range.map {|date| date.strftime("%b %e")} I reference them in some JS in my view as follows: dateLabels = <%= raw @mapped_dates.to_json %>;...

## Get X days out of an Array

ruby,ruby-on-rails-4
I have an array filled with Datetime objects: [Mon, 22 Jun 2015, Tue, 23 Jun 2015, Wed, 24 Jun 2015, Thu, 25 Jun 2015, Fri, 26 Jun 2015, Sat, 27 Jun 2015, Sun, 28 Jun 2015] I know how to select what I want from the array ex: week.select{|x|x.monday? ||...

## Rails less url path change

ruby-on-rails,ruby,url,path,less
Developing a Rails application with the less-rails gem I found something unusual : // app/assets/common/css/desktop/typo.less @font-face{ font-family:'SomeFont'; src:url("fonts/db92e416-da16-4ae2-a4c9-378dc24b7952.eot?#iefix"); // ... } The requested font is app/assets/common/css/fonts/db92e416-da16-4ae2-a4c9-378dc24b7952.eot This font is compiled with less and the results is : @font-face { font-family: 'SomeFont'; src: url("desktop/fonts/db92e416-da16-4ae2-a4c9-378dc24b7952.eot?#iefix"); //... } Do you know why is...

## Controller method: Contact form should render different page depending on where the form is used

ruby-on-rails,ruby,forms,ruby-on-rails-4,controller
I have a contact form on a special page dedicated to this form. The dedicated controller includes: def new @message = Message.new end def create @message = Message.new(message_params) if @message.valid? MessageMailer.new_message(@message).deliver_now flash[:success] = "Thank you!" redirect_to contact_path else render 'new' end end Now I would also like to include this...

## Recursive solution doesn't iterate correctly

ruby,algorithm,search,recursion
I'm working through a toy problem in Ruby: how to produce all possible 10-digit phone numbers where each successive number is adjacent to the last on the keypad. I've represented the adjacent relationships between numbers, and have a recursive function, but my method isn't iterating through the whole solution space....

## metriks log to file not working

ruby,code-metrics

## How to map browser dialog using capybara/selenium

ruby,selenium,capybara
What is the best way to handle browser dialogs (with ok/cancel buttons) using capybara/selenium? The way I found easiest is by running the following: dialog = page.driver.browser.switch_to.alert dialog.accept Any thoughts for a better approach? Whoever downed this post please give a valid reason... it's not helpful to down vote with...

## How to use Rails #update_attribute with array field?

ruby-on-rails,ruby,postgresql,ruby-on-rails-4,activerecord
Each question has an array of tags. schema.rb: create_table "questions", force: true do |t| t.text "tags", default: [], array: true How to atomically append to tags? How to prevent dups within the array? I tried question.update_attribute tags: tags << :ruby, but this doesn't work. Rails 4.17 and Postgres. EDIT: This...

## Call method to generate arguments in ruby works in 1.8.7 but not 1.9.3

ruby-on-rails,ruby,ruby-1.9.3
This is something that I had working in ruby 1.8.7, but no longer works in 1.9.3, and I am not sure what changes make this fail. Previously, I had something like this myFunction(submitArgs()) where submitArgs was a helper method that could be called with some options def submitArgs(args={}) #Some logic/manipulations...

## Rails basic auth not working properly

ruby-on-rails,ruby,authentication
I am building a small API that uses basic authentication. What I have done, is that a user can generate a username and password, that could be used to authenticate to the API. However I have discovered that it is not working 100% as intended. It appears that a request...

## Stop SSH password prompts in serverspec

ruby,net-ssh,serverspec

## Ruby - at least one item in array ALSO in line

ruby,regex
Given this array: ["/Item A/", "/Item B/"] and a line: Here's a line of text that includes /Item B/ what's a good, idiomatic, Ruby way of checking if the line includes at least one phrase from the array?...

## Ruby Dir.glob mystery: Where are the FNM_xxx flags described?

ruby
Note: This has already been posted on Ruby Forum some weeks ago. I'm crossposting it here, because I didn't get any response so far Dir.glob provides an optional parameter, usually referred as 'flags'. Where can I find a documentation about what flags are possible? The Ruby 2.0 docs just say...

## How to get return value from a forked / spawned process in Ruby?

ruby,process,output,fork,spawn
My simple test program: pid = Process.spawn("sleep 10;date") How can I place the output (eg stdout) of the "date" command in a variable when it is available? I don't want to use a file for the data exchange....

## In Ruby, how can I convert this string format to a hash

ruby
The string format is like this "a.b.c = 1" "a.b.d = 2" => its hash will be => {'a'=> {'b' => {'c'=>1, 'd'=>2 } } } It gets tricky with arrays "a.e[0].f = 1" "a.e[0].g = 2" "a.e[1].h = 3" => its hash will be => {'a' => {'e' =>...

## how to define extra method for rails form text_field

ruby-on-rails,ruby
After removing dimensions from my table, my scaffolded view hit this error: undefined method 'dimensions' for this code: <div class="field"> <%= f.label :dimensions %><br> <%= f.text_field :dimensions %> </div> I want to keep this dimention field in the form and in the controller I want to split this string into...

## Couldn't find User with 'id'=show

ruby-on-rails,ruby,devise
a Rails/Devise Question for you today. I'm working on user profiles for a site and I am hitting this error ActiveRecord::RecordNotFound at /users/show Couldn't find User with 'id'=show Not sure exactly what's happening as I haven't really run into anything like this before. Here are some necessary files to take...

## Heroku RAM not increasing with upgraded dynos

ruby-on-rails,ruby,ruby-on-rails-3,memory,heroku
I have a massive function i have been calling manually through the heroku rails console. I have been receiving the error rapid fire in my logs: 2015-06-22T14:56:42.940517+00:00 heroku[run.9877]: Process running mem=575M(112.4%) 2015-06-22T14:56:42.940517+00:00 heroku[run.9877]: Error R14 (Memory quota exceeded) A 1X dyno is suppose to have 512 MB of RAM. I...

## Allowing some enabled and disabled option on collection_select

ruby-on-rails,ruby
I am trying to populate a dropdown box on a view that has all the states. This works just fine: <%= f.collection_select :state_id, @states, :id, :name %> Now, I need to make the following: Some states are going to be disabled for choosing, but they still have to appear on...

## Switch between windows with frames

ruby,capybara
I have main window with frames and a popup where I do some operations: within_frame("MainFrame") do find("btnNewItem").click #opens popup window end within_window(windows.last) do within_frame("frmFrame2"){ some_operations find("btnOK").click #closes a popup } end within_window(switch_to_window(windows.first)) do within_frame("MainFrame") do find("btnDeleteItem").click #deletes item accept_popup_dialog end end But when operations are done in popup window and...

## RoR: removing sensitive field from response

ruby-on-rails,ruby,rails-activerecord
Is there any way to remove sensitive fields from the result set produced by the default ActiveRecord 'all', 'where', 'find', etc? In a small project that I'm using to learn ruby I've a reference to User in every object, but for security reasons I don't want to expose the user's...

## Having trouble calling a Proc within a Method within a Class

ruby,proc
I am struggling to debug my issue; I am trying to use a Proc within a method, all inside a class. When I don't type in the "class" details, my program compiles fine; when I specify the class name, however, I get a variety of different errors. Note: I posted...

## Saving to a database using a Nokogiri (json?) rake task

ruby-on-rails,ruby,json,nokogiri
RoR noob here! I have a rake task doing what I want to do I am just stuck on how to get the results saved to my language table. I want the results from this rake task to populate the values of the language field on my language table. I'm...

## What is wrong with this ST_CONTAINS statement PostGIS - Find point in polygon

sql,ruby,gis,postgis
I'm trying the following: Event.where('ST_Contains(?,ST_SetSRID(location, 4326)::geography)', search_polygon::geography) add getting the error *** NoMethodError Exception: undefined method geography' for but without that (::geography) I get a message telling me to cast, what do I do? HINT: No function matches the given name and argument types. You might need to add explicit...

## Undefined local variable post

ruby-on-rails,ruby,variables,undefined,local
I write this code: <% @user.posts.each do |post| %> <%= render 'post', locals: { post: post, user: @user} %> <% end %> Then in _post.htm.erb I write follow code: <div class="post-title"> <img src="<%= @user.avatar.url%>" class="img-rounded post-image"> <h4 id="post-name"><%= @user.first_name + ' ' [email protected]_name %> </h4> <div id="post-date"><%= post.created_at.strftime('%d %b -...

## Error when trying to install app with mysql2 gem

mysql,ruby-on-rails,ruby,ruby-on-rails-4
Im trying to install an open source rails 3.2.21 application that uses the mysql2 gem, but when i try and run the bundle commant I get the following error: Fetching: mysql2-0.3.18.gem (100%) Building native extensions. This could take a while... p ERROR: Error installing mysql2: ERROR: Failed to build gem...

## Ruby each loop based on array

ruby,sinatra
I have an array like this: @airports = [ ['Malaysia', 'Alor Setar', 'AOR'], ['Malaysia', 'Bintulu', 'BTU'], ['Malaysia', 'Ipoh', 'IPH'], ['Malaysia', 'Johor Bahru', 'JHB'], ['Indonesia', 'Kuching', 'KCH'], ['Indonesia', 'Labuan', 'LBU'], ['Indonesia', 'Langkawi', 'LGK'], ['Indonesia', 'Miri', 'MYY'], ['Indonesia', 'Penang', 'PEN'], ] then in my view: <select name="from" class="form-control select2"> <% @airports.each do...

## When to use (:method) or (&:method)

ruby
Why does: respond_to? in: class Wolf def howl; end end Wolf.new.respond_to?(:howl) # => true not require & while map in: ["1", "2", "3"].map(&:to_i) # => [1, 2, 3] does? Also, are there any technical names for this?...

## Does rails have an opposite of parameterize for strings?

ruby-on-rails,ruby
I used parameterize method. I want to de-parameterize it. Is there a method to do the opposite of parameterize?

## Rails - link_to path based on object's name + refactoring multiple custom actions

ruby-on-rails,ruby,refactoring
I'm looking to simplify the link_to path based on thr object's name and also am looking into refactoring multiple custom actions. I've managed to get this working below. <% ServiceMenu.all.each do |menu| %> <tr class=" <%= cycle('odd', 'even') %>"> <td><%= link_to menu.name, ("tech/""#{menu.name.parameterize}") %></td> </tr> <% end %> I feel...

## Loop until i get correct user

ruby,redis
I have users stored in Redis and want to be able to call only certain subsets from a set, if i don't get the correct user back i want to put it back in the set and then try again until i get one of the desired users @redis =...