jeudi 2 juillet 2015

Rails create callback for 2 dependent resources

I want to know how i can use create callback like before_create or after_create for the two dependent models.

class User < ActiveRecord::Base

end

class Member < ActiveRecord::Base

end

Lets suppose i have two models called User and Member and i want to create a member whenever any user will be created and want to create user whenever any member will be created .

If i will use the after_create or before_create callback in both the models it will run as never ending loop .so how this can be done.

Ruby on rails tutorial chapter 10...account activation not matching route

Trying to go through the ruby on rails tutorial on chapter 10. Have a pretty decent understanding of routing so far. Here is the issue: basically with account activations you send a token to an email which it then performs a hash on and tries to verify everything (after the link is clicked). I have a route set up for /account_activations/:id/edit

and this is the url I am sending: account_activations/$2a$10$GTxhcOsTRWNvmDMwaWLJk.WM1xgnICwXUpkymIN5oRqvWRIb%2FtnXy/edit?email=me%40gmail.com

I have verified that the hash is correct in several different testing scenarios. It then tells me it cannot find a route for it. Quoting the closest thing (in terms of priority) as:

edit_account_activation_path    GET /account_activations/:id/edit(.:format) account_activations#edit

The path in routes.rb looks like: resources :account_activations, only: [:edit]

Looks to me like those should match though as :id should just act as a wildcard for the string. If anyone needs more info let me know. Would really like to understand why this isn't working. Thanks.

Rails Viewing Multiple Stats within Admin Namespace

I have an Admin Namespace where all my controllers live. What I am want to be able to view is create stats on the view layer on all of these view. IE have the latest sales value on the top righthand side no matter which page you are on.

I have a few point that i have achieve

1) the data need to be in a the period 26th to the 25 of the next month. this must be on the current month. So today it would pull 26/6/2015 - 25/07/2015 this would only change again on the 26/7 to the new month. Any ideas code is below.

2) The code i have below is pulling the data it is very rough at this point for testing, also i need it to pull rep information so require rep_id which is giving a Null error as the users is not logged in yet. Once logged in it works apart from part 1.

Application Controller

  protect_from_forgery
  helper_method :current_user
  before_filter :loadlatestquotes
  before_filter :loadstats

  rescue_from CanCan::AccessDenied do |exception|
    redirect_to admin_url
  end

  private

  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end

  def loadlatestquotes
    @latestquotes = Quote.where(:rep_id => current_user.rep_id, :created_at => (8640.hours.ago..Time.now))
  end

  def loadstats
    @latestaccepedquotes = Quote.where(:rep_id => current_user.rep_id, :quote_accepted => true, :created_at => (8640.hours.ago..Time.now)).count
    @latestrejectedquotes = Quote.where(:rep_id => current_user.rep_id, :rejected => true, :created_at => (8640.hours.ago..Time.now)).count
    @latestpendingquotes = Quote.where(:rep_id => current_user.rep_id, :quote_accepted => false, :rejected => false, :created_at => (8640.hours.ago..Time.now)).count
    @latesttotalquote = Quote.where(:rep_id => current_user.rep_id, :created_at => (8640.hours.ago..Time.now)).sum(:quote_amount)
  end

  def logged_in?
    unless session[:user_id]
      flash[:notice] = "You need to log in first."
      redirect_to login_path
      return false
    else
      return true
    end
  end 

In my view i have the following

Total Quote Value: <%= number_to_currency(@latesttotalquote, :unit => "R") %><br /> 
Total Order Value: <br />
Total Pending Quotes:  <%= @latestpendingquotes %><br />
Total Accepted Quotes: <%= @latestaccepedquotes %> <br />
Total Rejected Quotes: <%= @latestrejectedquotes %> <br />
Total Quotes:  <%= @latestquotes.count %><br /> 
Lead Closure:  <br />
Rejection Rate:  <br />
Rand Closure:  <br />

Thanks for the assistance in advanced. Any assistance would be great in order for me to fix this thanks

mercredi 1 juillet 2015

Deploying Ruby on Rails 2.3 on Production

Am sorry for the noobness for this,

I am developing a ruby on rails application on my ubuntu 14.04. As for development, i'm starting my application using ruby script/server. Ruby -v 1.8.7 Rails -v 2.3.14 MySql

Now my question is how to deploy this on production? Am planning to use Ubuntu 14.04 Server as well.

I read about the Phusion Passenger but I did not understand what is that for.

I was hoping to make the ubuntu server as a localhost. So that if I point to the browser like myrailsapp, my application will be available (Normally I will use script/server). Do I have to use the script/server command everytime the server will restart?

Please tell me if im on the right track.

Pluralize method for different language in Ruby on Rails

I am using pluralize method in HTML files to dynamic value which user can set as per their locale.

i.e. so If user set label deaprtment with the german word "Abteilung", it will append 's' as per current Rails Inflection rules for pluralize string. So word will be "Abteilungs" , but their is no suchword in German.

I researched , but there is no such inflection rules specific to language. There may b many such word in many languages so i can't make custom inflection rules. I am not using I18n or tr8n for internationalizing such words. can someone suggest , what can i do to resolve this.

Email Field returns blank string

I am using devise in my Rails3 application.The problem I am facing is the email field is being returned as a blank string after save.

    resource.save
    Rails.logger.info p resource

This returns me

#<User id: 1850, email: "", encrypted_password: "$2a$10$EZnvzTKpgYyN2K8I20vXBuk6OZ.XQ6gfOCbOUyfkDKx/...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, confirmation_token: "6e6c41315002a24cf63e4512ab94f693ef9d419218e1cbb9a2d...", confirmed_at: nil, confirmation_sent_at: "2015-07-02 04:49:23", unconfirmed_email: "anildiasdavis@gmail.com", company_code: "WNZQIFHN", partner_id: nil, partner_type: nil, super_admin_id: nil, super_admin_type: nil, device_token: nil, is_active: nil, is_disabled: false, total_session_time: "0:0:0", status: "pending", created_at: "2015-07-02 04:49:20", updated_at: "2015-07-02 04:49:20", doctor_id: nil, last_notified_offer_id: nil, is_deleted: false, remove_reason: nil, active_at: nil, deactivated_at: nil, was_disabled: false, is_first_menu_selection: true, is_default_filter: true>

But when I query the console it gives me the correct response.

#<User id: 1850, email: "anildiasdavis@gmail.com", encrypted_password: "$2a$10$EZnvzTKpgYyN2K8I20vXBuk6OZ.XQ6gfOCbOUyfkDKx/...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, confirmation_token: "6e6c41315002a24cf63e4512ab94f693ef9d419218e1cbb9a2d...", confirmed_at: nil, confirmation_sent_at: "2015-07-02 04:49:23", unconfirmed_email: "anildiasdavis@gmail.com", company_code: "WNZQIFHN", partner_id: nil, partner_type: nil, super_admin_id: nil, super_admin_type: nil, device_token: nil, is_active: nil, is_disabled: false, total_session_time: "0:0:0", status: "pending", created_at: "2015-07-02 04:49:20", updated_at: "2015-07-02 04:49:20", doctor_id: nil, last_notified_offer_id: nil, is_deleted: false, remove_reason: nil, active_at: nil, deactivated_at: nil, was_disabled: false, is_first_menu_selection: true, is_default_filter: true>

Why does this happen?Am I doing anything wrong here?

Getting "Null" as return value in ruby method

Following code is only for testing purposes

controller class

def show
  @game = Game.find(params[:id])
  matrix = [
     [':yes', ':No', ':none'],
     [':yes', ':No',' :Yes'],
     [':No', ':Yes', ':none'],
 ]
 @game.matrix = ::Gamer.new(matrix).next
 @game.save!()
 ...
end

Another class in lib folder

class Gamer
 @@current_value

 def initialize(args_value)
  @@current_value = Marshal.load(Marshal.dump(args_value))
 end

 def next
  prev_state = Marshal.load(Marshal.dump(@@current_value))
  for row in prev_state
   for item in row
    ...
    #making changes into `@@current_value` 
    ...
   end
  end
  return @@current_value
end 

Whenever I check the value of gamer.matrix it returns null everytime. If i replace return from return @@current_value with return @@prev_state I am getting the exact value which I passed to this function. That means my controller is able to call this class method.

What I am doing wrong here?