before_validation :capitalize_first_words 

    def capitalize_first_words 
      [:question, :correct_ans_1, :correct_ans_2, :correct_ans_3].each do 
|key| 

          self.key = key.to_s.gsub(/^(\W*?[a-z])/) { |m| m.upcase } if 
!key.to_s.blank? 
      end 
    end 

Note the difference betweet self.key and key.
self.key calls method key on self.
key - is block local variable, which is instance of Symbol ( :question, 
:correct_ans_1 ...).
Symbol has not methods '.gsub' and '.blank?', so it must be converted to 
String to call '.gsub' and '.blank?'

среда, 4 сентября 2013 г., 1:03:10 UTC+3 пользователь Ruby-Forum.com User 
написал:
>
> Now giving error: 
> undefined method `key' for #<Question:0x007fdff26b7798> 
>
> In Question model: 
>  before_validation :capitalize_first_words 
>
>     def capitalize_first_words 
>       [:question, :correct_ans_1, :correct_ans_2, :correct_ans_3].each 
> do |key| 
>
>           self.key = self.key.gsub(/^(\W*?[a-z])/) { |m| m.upcase } if 
> !self.key.blank? 
>       end 
>     end 
>
> Am I missing something really quite simple? 
>
> Thanks 
>
> -- 
> Posted via http://www.ruby-forum.com/. 
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/8bd8a4a0-2626-4fe8-958c-64d8c9939a9e%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to