ruby-on-rails-3

newbie: error message when 'rake -T'

為{幸葍}努か 提交于 2020-01-01 09:10:11
问题 I am using Ruby Enterprise Edition for my project. When I check all my rake task by run the command rake -T , I got the following error message: You have already activated rake 0.9.2.2, but your Gemfile requires rake 0.9.2. Using bundle exec may solve this. The error message implies that I can use bundle exec to solve the problem, but I am not sure how? So, how to get rid of this error message? ------------------------------ more --------------------------- I prefer to update my Gemfile

Rails number of weeks in month

青春壹個敷衍的年華 提交于 2020-01-01 09:01:41
问题 How can I in rails calculate the number of weeks in a given month? Thanks 回答1: i dont know exactly what you want... But maybe you want something like this: (Time::days_in_month(05,2010).to_f / 7) #> 4.42857142857143 回答2: I needed to know how many weeks including partial weeks there were in a month. Think of it like rows in a calendar. How many rows do you need? You have to consider the number of days and also what day the month starts on. October 2011 actually has 6 unique weeks for example.

How do I get only the query string in a Rails route?

余生长醉 提交于 2020-01-01 08:57:12
问题 I am using a route like this match "/v1/:method" => "v1#index" My intention here is capture the name of the api method and then send the request to that method inside the controller. def index self.send params[:method], params end I figured this would send the other parameters as an argument to the method, but it didn't work. So my question is how can I pass the non-method parameters in a query string? 回答1: #query_parameters does exactly what you want: request.query_parameters It's also the

Regex to check alphanumeric string in ruby

你离开我真会死。 提交于 2020-01-01 08:53:53
问题 I am trying to validate strings in ruby. Any string which contains spaces,under scores or any special char should fail validation. The valid string should contain only chars a-zA-Z0-9 My code looks like. def validate(string) regex ="/[^a-zA-Z0-9]$/ if(string =~ regex) return "true" else return "false" end I am getting error: TypeError: type mismatch: String given. Can anyone please let me know what is the correct way of doing this? 回答1: You can just check if a special character is present in

Regex to check alphanumeric string in ruby

别等时光非礼了梦想. 提交于 2020-01-01 08:53:06
问题 I am trying to validate strings in ruby. Any string which contains spaces,under scores or any special char should fail validation. The valid string should contain only chars a-zA-Z0-9 My code looks like. def validate(string) regex ="/[^a-zA-Z0-9]$/ if(string =~ regex) return "true" else return "false" end I am getting error: TypeError: type mismatch: String given. Can anyone please let me know what is the correct way of doing this? 回答1: You can just check if a special character is present in

Can store hash in a cookie?

落花浮王杯 提交于 2020-01-01 08:43:38
问题 Anyone know if I can put a hash in the cookie? Something like this: cookies [: test] = {: top => 5,: middle => 3,: bottom => 1} Thanks 回答1: I woud look into serializing the hash to store it. Then deserialize it to retrieve it. When you serialize a hash, the result will be an encoded string. This string can be decoded to get the original object back. You could use YAML or JSON for this. Both are nicely supported in Ruby. A YAML example require "yaml" cookies[:test] = YAML::dump {a: 1, b: "2",

Devise 'translation missing' error with subclassed controller

橙三吉。 提交于 2020-01-01 08:40:12
问题 I've subclassed Devise::RegistrationsController . The subclassed controller, in my case, is AdminRegistrationsController . But, when I sign up a new admin, for example, the flash shows the message: translation missing: en.devise.admin_registrations.admin.signed_up I've verified an entry exists in config/locales/devise.en.yml . Here's the snippet: en: devise: registrations: signed_up: 'Welcome! You have signed up successfully.' What am I missing? 回答1: You must do this : en: devise: admin

Rails utf-8 problem

☆樱花仙子☆ 提交于 2020-01-01 08:30:13
问题 I there, I'm new to ruby (and rails) and having som problems when using Swedish letters in strings. In my action a create a instance variable like this: @title = "Välkommen" And I get the following error: invalid multibyte char (US-ASCII) syntax error, unexpected $end, expecting keyword_end @title = "Välkommen" ^ What's happening? EDIT: If I add: # coding: utf-8 at the top of my controller it works. Why is that and how can I slove this "issue"? 回答1: See Joel spolsky's article "The Absolute

What is the difference between render and yield in Rails

主宰稳场 提交于 2020-01-01 08:04:23
问题 Can someone explain the difference between " <%= render %> " and " <%= yield %> with <% content_for :partial do %> / <% end %> "? specifically how the routing changes when switching from one to another, the benefits of using one over the other, when is it practical to use one over the other. THIS is the closest explanation I have found, but isn't quite clear enough for me. I have been trying for several days to wrap my head around this, but it seems that each configuration I try either comes

404 error with open-uri in a rake task… what's causing it?

北战南征 提交于 2020-01-01 08:04:09
问题 I have a rake task that fetches JSON data from an API, parses it, and saves it to the database: task :embedly => :environment do require 'json' require 'uri' require 'open-uri' Video.all.each do |video| json_stream = open("http://api.embed.ly/1/oembed?key=08b652e6b3ea11e0ae3f4040d3dc5c07&url=#{video.video_url}&maxwidth=525") ruby_hash = JSON.parse(json_stream.read) thumbnail_url = ruby_hash['thumbnail_url'] embed_code = ruby_hash['html'] video.update_attributes(:thumbnail_url => thumbnail_url