Redirect_to with a hash instead of parameters

All,

I have numerous places in my codebase like this:

redirect_to :action => ‘schedule_by_uid_and_run_date’, :uid =>
@schedule.first.train_uid, :year => @date.year, :month => @date.month,
:day => @date.day

I’d like to replace the :year, :month and :day parameters with a hash
containing the three parameters. @date is an instance of a Date class,
and the trouble with that is Date.month is a single character. I’d like
it to be null-padded, but it seems messy to put .rjust(2, ‘0’) against
all three parameters everywhere in the code.

Is it possible to do something like this?

redirect_to :action => ‘foo’, :uid => @schedule.first.train_uid,
params_hash

…where params_hash contains { :year => ‘2011’, :month => ‘01’, :day =>
‘19’ }

Cheers,

Peter

http://apidock.com/rails/ActionController/Redirecting/redirect_to
assuming
you use rails 3.1

this might work :

redirect_to {:action => ‘foo’, :uid => @schedule.first.train_uid,
params_hash}.merge(param_hash)

Ahmy Y.

sorry redirect_to {:action => ‘foo’, :uid =>
@schedule.first.train_uid}.merge(param_hash)

Ahmy Y.

On Fri, Jan 20, 2012 at 04:22:14PM +0700, Ahmy Y. wrote:

sorry redirect_to {:action => ‘foo’, :uid =>
@schedule.first.train_uid}.merge(param_hash)

I think I tried that already, but I’ll give it a go.

I settled on…

redirect_to :action => ‘foo’, :uid => …, nil => param_hash

…but I’m not sure if it feels horribly dirty or elegantly smart.

Peter

Hai:

create virtual models

year_entry.rb

attr_accessor :year,
:month,
:day

controller:

@year_entry_access = []
year_var = YearEntry.new
year_var.year = 2012
year_var.month = 1
year_var.day = 20
@year_entry_access << year_var

redirect_to :action=>show_rrr,:year_entry_access =>@year_entry_access

After that you can pass other method

def show_rrr
if params[:year_entry_access] # here you can get params_hash format
params[:year_entry_access].each_value do |get_date|
get_date[:date]
get_month[:month]
get_year[:year]
end
end
end

you can access like this way “get_date[:date]”

Bye:)
bdeveloper

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs