In Ruby, why does defining a class evaluate to nil? Same goes for defining a method: why does it evaluate to nil? Wouldn't it be useful if defining a class would evaluate as the class?
Hi,
what the syntax is in Action Mailer Basics rails guide ?
class UserMailer < ActionMailer::Base
def welcome_email(user)
recipients user.email
from "My Awesome Site Notifications <[email protected]>"
subject "Welcome to My Awesome Site"
sent_on Time.now
body {:user => user, :url => "http://example.com/login"}
end
end
How should i understand the construction, like
from "Some text for this field"
Is it an assignment the value to a variable, called "from" ?
i dont understand class_eval in ruby.
http://pastebin.com/RMnvTCyz
what does the % mean?
what does class_eval do?
and where is (val) coming from?
thanks
Does Ruby have an equivalent to .NET's Encoding.ASCII.GetString(byte[])?
Encoding.ASCII.GetString(bytes[]) takes an array of bytes and returns a string after decoding the bytes using the ASCII encoding.
Hello.
I am interested in modeling a Malthusian growth model in Ruby. Does anyone have any ideas, or are there any interesting libraries that cover this? Any help is appreciated.
http://en.wikipedia.org/wiki/Malthusian_growth_model
I have a log file that is constantly growing, how can I watch and parse it via a ruby script. The script will parse each new line as it is written to the file and output something to the screen when the new line contains the string 'ERROR'
it seems like i have begin rescue end statements everywhere in my code. this doesn't seem like the correct thing to do. i am learning process, can anyone suggest how can i catch any exceptions without having to place everything inside begin,rescue, end.
anyway to just tell ruby to shut up and just keep going even if exception is raised ?
I was trying to add a method to the String class. This method should mutate the current string (of course it would be possible to write a not mutating version but I'd prefer the mutating one). I had no idea how to do this and after some googling I found the method rb_str_modify which makes a given string mutable. That's exactly what I need but I couldn't find an equivalent in the Ruby language. Did I miss something or is there really no possibility in the language itself?
Hi everybody, I need comunicate a rails ap with skype, more specific, I need than(previously aprovation) a user can call other user using skype and keep both id private, is that possible?, thank so much!!!
In ruby, how can I get current time in a given timezone? I know the offset from UTC, and want to get the current time in the timezone with that offset.
I am a beginner is rails, every now and then I come across dependencies. I tried to figure out what it means, but was not able to. Can someone please explain me what it means?
I'm using Rails 3 w/ Mongoid, (so no ActiveRecord). Mongoid uses ActiveModel's "to_json" method, and by default that method includes the root object in the JSON (which I don't want).
I've tried putting this in an initializer:
ActiveModel::Base.include_root_in_json = false
But get the error
uninitialized constant ActiveModel::Base
Any ideas how I can change this? I changed the default directly in the source-code and it worked fine, but obviously I'd like to do it properly.
The variable is defined at the top of this file:
Github - activemodel/lib/active_model/serializers/json.rb
From the docs:
"The option ActiveModel::Base.include_root_in_json controls the top-level behavior of to_json. It is true by default."
How can i replace all > with > and < with < inside a <code> tag with ruby?
For example:
<code><script>alert('I steal cookies');</script></code>
With:
<code><script>alert('I steal cookies);<script><code>
The reason for this is because the h() method escapes all the < and >
Thanks, Micke
I am implementing some rubyonrails code tweet stuff for my users. I am creating the proper oauth link...something like
http://twitter.com/oauth/authorize?oauth_token=y2RkuftYAEkbEuIF7zKMuzWN30O2XxM8U9j0egtzKv
But after my test account grants access to twitter, it pulls up a page saying "You've successfully granted access to . Simply return to and enter the following PIN to complete the process. 1234567"
I have no idea where the user should enter this PIN and why they have to do that. I don't think this should be a necessary step. Twitter should be redirecting the user to the callback URL I provided in the application settings. Does anyone know why this is happening?
UPDATE
I found this article that states I need to send my users to this URL (note "authenticate" instead of "authorize"):
http://twitter.com/oauth/authenticate?oauth_token=y2RkuftYAEkbEuIF7zKMuzWN30O2XxM8U9j0egtzKv
I made the change but Twitter redirects the user to the authorize path after he clicks "Allow" which then gives him the 7 digit PIN again!
In Rails3, I have the following line:
@messages = Message.where("recipient_deleted = ?", false).find_by_recipient_id(@user.id)
In my view, I loop through @messages and print out each message, as such:
<% for message in @messages %>
<%= message.sender_id %>
<%= message.created_at %>
<%= message.body %>
<% end %>
This works flawlessly when there are several messages.
The problem is that when I have one message, I get an error thrown at me:
undefined methodeach'`
How do I force rails to always return an array of messages even if there's only one message so that each always works?
Thanks!
Hi,
I'm developing an application in Rails and want the user to be able to signup and provide their card details on one form. I'm using the Braintree API and their transparent redirect, which means that the form data is posted directly to Braintree.
How can I store and later retrieve the non-payment related information provided by the user from that form e.g. account name, username? These values are not returned in the response provided by Braintree.
If you look at the Basecamp signup process, this is the result I want to achieve.
Thanks
Robin
I use hpricot gem in rubyonrails to parse a webpage and extract the meta-tag contents. But if the website has a <noscrpit> tag just after the <head> tag it throws an exception
Exception: undefined method `[]' for nil:NilClass
I even tried to update the gem to the latest version. but still the same.
this is the sample code i use.
require 'rubygems'
require 'hpricot'
require 'open-uri'
begin
index_page = Hpricot(open("http://sample.com"))
puts index_page.at("/html/head/meta[@name='verification']")['content'].gsub(/\s/, "")
rescue Exception => e
puts "Exception: #{e}"
end
i was thinking to remove the noscript tag before giving the webpage to hpricot.
or is there anyother way to do it??
I have this code in my every model.
Class people
def before_validation
@attributes.each do |key,value|
self[key] = nil if value.blank?
end
end
end
Now i want to put my loop in separate module. Like
Module test
def before_validation
@attributes.each do |key,value|
self[key] = nil if value.blank?
end
end
end
And i want to call this before_validation this way
Class people
include test
def before_validation
super
.....Here is my other logic part.....
end
end
Are there any way to do it like that in rails??
Rails is doing my head in.
I'm trying now to put something together to pull screen scraped data from site X through to client Y via a ruby script on server Z
I don't want views, I just want the request to look like domain.com/action/method
Inside routes.rb I have:
match ':controller(/:action(/:id(.:format)))'
But it still won't work. I just get ActionView::MissingTemplate in the log.
Achtung!
If I deliberately put a faulty method in that subsequently calls render - the log file indicates the method executed badly, so I don't think it's something wrong with the "action" controller.
hi all
I have a ruby / rails application that integrates in and outgoing email directly into the app. The app is going to be running on multiple domains each with posible many users sending and recieving email.
I have looked into sendgrid, mailchimp and mad mimi as hosted services and also looked to create my own email server.
There are advantages and disadvantages of both solutions and i am not sure which one to go down and am hoping someone can give me advice ??
Any help will be great. I know email is a hassle to manage but once set up correctly cant be that bad ??
Thanks in advance
Rick
Most of you should already know Pragmatic book's "Agile web dev with rails" (third edition). On page 537 - 541 it has "Custom Form Builders" code as follows:
class TaggedBuilder < ActionView::Helpers::FormBuilder
# <p> # <label for="product_description">Description</label><br/> # <%= form.text_area 'description' %> #</p>
def self.create_tagged_field(method_name)
define_method(method_name) do |label, *args|
@template.content_tag("p" , @template.content_tag("label" , label.to_s.humanize,
:for => "#{@object_name}_#{label}") + "<br/>" + super)
end
end
field_helpers.each do |name|
create_tagged_field(name)
end
end
This code doesn't work with Ruby 1.9.1. It returns error as follows:
implicit argument passing of super from method defined by define_method() is not supported. Specify all arguments explicitly. (ActionView::TemplateError)
My question is: What should I change in the code to fix this?
Thank you
i have
class Profile
has_many :favorite_books, :dependent => :destroy
has_many :favorite_quotes, :dependent => :destroy
accepts_nested_attributes_for :favorite_books, :allow_destroy => true
accepts_nested_attributes_for :favorite_quotes, :allow_destroy => true
end
I have a dynamic form where you press '+' to add new textareas for creating new favorites.
What i want to do is ignore the blank ones, I find this harder to sort through in the update controller than a non nested attribute.
What i have temporarily is a hack in the after_save callback deleting the empty records. Whats the most rails way to ignore these blank objects?
I dont want validation and errors, just a silent deletion/ignore.
How can I go about deploying a Rails app on a cluster of Amazon EC2 servers? Any recommended guides?
I maintain a RoR app (currently hosted on Heroku) that uses a DB and DelayedJobs). The app has a large footprint, and needs to be distributed on a cluster most likely. Any tips would be appreciated. Are there Amazon AMIs that replicate some of Heroku's features (especially DJ)?
P.S. I'm quite a Ruby newbie.
Problem
The form_for helper incorrectly determines the path to my nested resource inside of a namespace. The models in question are: Forum::Thread and Forum::Reply respectively, located in a subfolder called "forum" under my models directory. This is in Rails 3 BETA 3.
routes.rb
namespace :forum do
root :to => 'threads#index'
resources :threads do
resources :replies
end
end
app/views/forum/replies/_form.html.haml
...
- form_for [@thread, @reply] do |f|
...
app/controllers/forum/replies_controller.rb
...
def new
@reply = Forum::Reply.new
end
...
Error
undefined method `forum_thread_forum_replies_path'
In reference to the line outlined above in _form.html.haml