Visualizzazione post con etichetta ruby on rails. Mostra tutti i post
Visualizzazione post con etichetta ruby on rails. Mostra tutti i post

mercoledì 4 dicembre 2013

Rails 4 rescue_from Routing Error path sconosciuti

Per prima cosa in fondo a route.rb

# routes.rb 
# ultima istruzione del file altrimenti naconde quelle precendenti
match '*path', via: :all, to: "application#routing_error"

in questo modo redirigiamo tutti i routing sconosciuti

Quindi  andiamo a generare l'errore che gestiremo come meglio ci aggrada

# application_controller.rb 

def routing_error
  raise ActionController::RoutingError.new(params[:path])

end

rescue_from ActionController::RoutingError, with: :routing_error_rescue

private

def routing_error_rescue
  flash[:error] = "Impossible #{params[:path]}."
  redirect_to request.headers["Referer"] || root_path

end

Fatto

venerdì 23 luglio 2010

Rails + Gmail con ruby 1.8.7

Un semplice How-to per fornire un applicazione Rails (2.3.x) del servizio di invio di mail
In particolare si suppone che esistano degli utenti User con attibuto User.username per testare l'interazione con l'applicazione

1) il mailer model "notifier"
Il primo passo è la creazione di un controller per l'invio delle mail

$ script/generate mailer Notifier
exists app/models/
create app/views/notifier
exists test/unit/
create test/fixtures/notifier
create app/models/notifier.rb
create test/unit/notifier_test.rb


crea il model notifier.rb e la cartella nelle viste notifier che conterrà le viste ovvero i modelli dei testi delle mail che manderemo

2)Il metodo"notification"
Bisogna quindi creare il metodo in /models/notifier.rb


class Notifier < user =""> user)
def notification(user)
recipients "myname@gmail.com"#user.email
from "myname@gmail.com"
subject "New account information"
body (:user => user)# "account" => recipient
content_type "text/html"
end
end


:user passato nel body rende disponibile la variabile @user nella vista che andremo a creare per il testo della mail

3) Il testo della mail
creiamo il file /views/notifier/notification.html.erb

Hi <%= @user.username %>,
Thanks for joining our service! Please check back often.


in cui @user è l'utente che abbiamo passato per argomento precedentemente

4) Modifichiamo le impostazioni di invio

modifichiamo il file /config/environments/developments.rb

ActionMailer::Base.smtp_settings = {
:enable_starttls_auto => true,
:address => "smtp.gmail.com",
:port => 587,
:authentication => :plain,
:domain => "myname@gmail.com",
:user_name => "myname@gmail.com",
:password => "mynamepassword",
}
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp


abbiamo completato le modifiche per testarle lanciamo la console con

script/console
Loading development environment (Rails 2.3.5)
>>

Quindi utilizziamo il methodo deliver per inviare la mail

>> Notifier.deliver_notification User.first
>> #TMail::Mail port=#TMail::StringPort:id=0x..fdb3d0cc4>
bodyport= #TMail::StringPort:id=0x..fdb3cf9d2
>>


Mail inviata e ricevuta

venerdì 20 febbraio 2009

Getting started with Activewarehouse: documentation and examples

I began to discover these gems
ActiveWarehouse
ActiveWarehouse-etl

and the first problem was to put together some documentation and examples

an important introduction
a presentation and the code
another presentation

activewarehouse:
rdoc (documentation dated, regenerate it locally rake doc:plugin)
readme

a demo application (github rails 1.1.6) (svn link)
tutorial (via web archive)
a important change

activewarehouse-etl:
the first
ctl examples: 1, 2, 3

the 2 blog of developpers:
http://blog.anthonyeden.com/
http://martyhaught.com/

This is only a first collection and I hope to update it thanks to feedback
Will soon try to write a brief account of my step-by-step experiments

mercoledì 17 dicembre 2008

Line graph with error band

Example of line graph with error band.
The function draws a series of quadrilaterals whose vertices matched pairs of the two series data_up and data_down then inserting a line is the average values.
To actually use series with variable error just simply change the code by passing the three sets as parameters instead generate randomly.


test_it_controller.rb

class TestItController < ApplicationController
def index
@graph1 = open_flash_chart_object(600,300,"/test_it/graph1_code")
end

def graph1_code

title = Title.new("a Graph")

chart = OpenFlashChart.new
chart.set_title(title)

@data_up = [1]
@data_down = [-1]

0.step(30, 1) {|i|
@data_up << @data_up[i] +rand - 0.5
@data_down << @data_down[i] + rand - 0.5
}

def draw_quadri(i)
q = Shape.new( '#80B11A' )
q.append_value(ShapePoint.new(i,@data_up[i]))
q.append_value(ShapePoint.new(i,@data_down[i]))
q.append_value(ShapePoint.new(i+1,@data_down[i+1]))
q.append_value(ShapePoint.new(i+1,@data_up[i+1]))
return q
end

flux = []
(@data_up.length-1).times do |h|
flux << draw_quadri(h)
end

flux.each do |shape|
chart.add_element(shape)
end

scatter_line = ScatterLine.new( '#FF0000', 5 )
scatter_line_u = ScatterLine.new( '#FF0000', 3 )
scatter_line_d = ScatterLine.new( '#FF0000', 3 )
data = []
data_u = []
data_d = []

x=0
(@data_up.length).times do |h|
data << (ScatterValue.new(x,(@data_up[h] + @data_down[h])/ 2))
data_u << (ScatterValue.new(x,@data_up[h]))
data_d << (ScatterValue.new(x,@data_down[h]))
x+=1
end

scatter_line.set_values(data); chart.add_element(scatter_line)
scatter_line_u.set_values(data_u); chart.add_element(scatter_line_u)
scatter_line_d.set_values(data_d); chart.add_element(scatter_line_d)

x = XAxis.new
x.set_range(0,32,5)
x.set_offset(false)
chart.set_x_axis(x)

y = YAxis.new
y.set_range(-5,5,1)
y.set_offset(true)
chart.set_y_axis(y)

render :text => chart.to_s
end
end

giovedì 11 dicembre 2008

Radar chart lines

First attempt to offer something to my referring to the excellent tutorial by pullmonkey.com and harryseldon
here is the ruby in transposition of the third example of radar chart
radar chart lines

test_it_controller.rb

class TestItController < ApplicationController
def index
@graph1 = open_flash_chart_object(600,300,"/test_it/graph1_code")
end

def graph_code
chart = OpenFlashChart.new
chart.set_title(Title.new('Radar Chart'))

values = [30,50,60,70,80,90,100,115,130,115,100,90,80,70,60,50]
spokes = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p']
vals = []

values.each_with_index do |value, i|
tmp = DotValue.new( value, '#D41E47' )
tmp.set_tooltip("#val#&ltbr%rtSpoke: #{spokes[i]}")
vals.push(tmp)
end

line = LineHollow.new()
line.set_values( vals )
line.set_halo_size( 0 )
line.set_width( 2 )
line.set_dot_size( 6 )
line.set_colour( '#FBB829' )
line.set_key( 'Hearts', 10 )
line.loop()


# add the area object to the chart:
chart.add_element(line)

r = RadarAxis.new( 150 )
r.set_steps(10)
r.set_colour( '#DAD5E0' )
r.set_grid_colour( '#EFEFEF' )
chart.set_radar_axis( r )

tooltip = Tooltip.new()
tooltip.set_proximity()
chart.set_tooltip( tooltip )

chart.set_bg_colour( '#ffffff' )

render :text => chart.to_s
end
end