Pass infomation between different views and controllers


#1

Hi, I am a rails and ruby nuby :slight_smile:

How do you pass data between different controllers and views? I have a
page ( a quote) that I am editing url …quote/edit/6 6 id the
current id. I then go to a catalog (url…catalog) to add an item to
my quote (I do this via a line_item file that the quote reads) I now
want to go back to the quote. I have tried this

<%= link_to ‘Back to Quote’, :controller => “quote”, :action =>
“edit”, :id => @quote %>

but this message

Couldn’t find Client without an ID

I thought that @quote would give the ID but obviously it is not doing
so.

Could somebody please help me sort this out?

Regards,

Paul


#2

Paul Jonathan T. wrote:

Hi, I am a rails and ruby nuby :slight_smile:

How do you pass data between different controllers and views? I have a
page ( a quote) that I am editing url …quote/edit/6 6 id the
current id. I then go to a catalog (url…catalog) to add an item to
my quote (I do this via a line_item file that the quote reads) I now
want to go back to the quote. I have tried this

<%= link_to ‘Back to Quote’, :controller => “quote”, :action =>
“edit”, :id => @quote %>

but this message

Couldn’t find Client without an ID

I thought that @quote would give the ID but obviously it is not doing
so.

Could somebody please help me sort this out?

Regards,

Paul

What is key (id field) for the quote in the “line_item” table? eg. lets
assume it is quote_id.
:id = @line_item.quote_id

or
Keep the id of the quote in a session parameter? and the quote
controller edit references the session parameter value?


#3

quote = Quote.find(params[:id])

session[:quote_id] = quote.id

P.S. Don’t forget:

    session[:quote_id] = nil

  where ever it makes sense to make it go away.


– Tom M.


#4

Hi Glen, thanks for the quick response. Your suggestion of storing the
quote id in a session parameter makes sense to me. However I am not
quite sure how to do that. Could you give an example please.

Thank you,

Paul


#5

Sorry still have a problem. here is the code

Here is my controller code in quote_controller.rb

def edit
@client = Client.find(params[:id])
session[:client_id] = Client.id
end

Here is the link code in the view catalog.rhtml

<%= link_to 'Back to Quote', :controller => "quote", :action => "edit", :id => "client_id" %>


This is the dump

ActiveRecord::RecordNotFound in QuoteController#edit

Couldn’t find Client with ID=client_id

RAILS_ROOT: /home/pjt/quote/config/…
Application Trace | Framework Trace | Full Trace

/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:955:in
find_one' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:941:infind_from_ids’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:382:in
find' #{RAILS_ROOT}/app/controllers/quote_controller.rb:12:inedit’
/usr/bin/mongrel_rails:18

/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:955:in
find_one' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:941:infind_from_ids’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:382:in
find' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:910:inperform_action_without_filters’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:368:in
perform_action_without_benchmark' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:inperform_action_without_rescue’
/usr/lib/ruby/1.8/benchmark.rb:293:in measure' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:inperform_action_without_rescue’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/rescue.rb:82:in
perform_action' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:381:inprocess_without_filters’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:377:in
process_without_session_management_support' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/session_management.rb:117:inprocess’
/usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/dispatcher.rb:38:in
dispatch' /usr/lib/ruby/gems/1.8/gems/mongrel-0.3.12.4/lib/mongrel/rails.rb:66:inprocess’
/usr/lib/ruby/1.8/thread.rb:135:in synchronize' /usr/lib/ruby/gems/1.8/gems/mongrel-0.3.12.4/lib/mongrel/rails.rb:64:inprocess’
/usr/lib/ruby/gems/1.8/gems/mongrel-0.3.12.4/lib/mongrel.rb:425:in
process_client' /usr/lib/ruby/gems/1.8/gems/mongrel-0.3.12.4/lib/mongrel.rb:424:inprocess_client’
/usr/lib/ruby/gems/1.8/gems/mongrel-0.3.12.4/lib/mongrel.rb:495:in run' /usr/lib/ruby/gems/1.8/gems/mongrel-0.3.12.4/lib/mongrel.rb:494:inrun’
/usr/lib/ruby/gems/1.8/gems/mongrel-0.3.12.4/lib/mongrel.rb:483:in run' /usr/lib/ruby/gems/1.8/gems/mongrel-0.3.12.4/lib/mongrel.rb:774:inrun’
/usr/lib/ruby/gems/1.8/gems/mongrel-0.3.12.4/lib/mongrel.rb:772:in run' /usr/lib/ruby/gems/1.8/gems/mongrel-0.3.12.4/bin/mongrel_rails:97:inrun’
/usr/lib/ruby/gems/1.8/gems/mongrel-0.3.12.4/lib/mongrel/command.rb:163:in
`run’
/usr/lib/ruby/gems/1.8/gems/mongrel-0.3.12.4/bin/mongrel_rails:194

/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:955:in
find_one' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:941:infind_from_ids’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:382:in
find' #{RAILS_ROOT}/app/controllers/quote_controller.rb:12:inedit’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:910:in
perform_action_without_filters' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:368:inperform_action_without_benchmark’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in
perform_action_without_rescue' /usr/lib/ruby/1.8/benchmark.rb:293:inmeasure’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in
perform_action_without_rescue' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/rescue.rb:82:inperform_action’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:381:in
process_without_filters' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:377:inprocess_without_session_management_support’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/session_management.rb:117:in
process' /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/dispatcher.rb:38:indispatch’
/usr/lib/ruby/gems/1.8/gems/mongrel-0.3.12.4/lib/mongrel/rails.rb:66:in
process' /usr/lib/ruby/1.8/thread.rb:135:insynchronize’
/usr/lib/ruby/gems/1.8/gems/mongrel-0.3.12.4/lib/mongrel/rails.rb:64:in
process' /usr/lib/ruby/gems/1.8/gems/mongrel-0.3.12.4/lib/mongrel.rb:425:inprocess_client’
/usr/lib/ruby/gems/1.8/gems/mongrel-0.3.12.4/lib/mongrel.rb:424:in
process_client' /usr/lib/ruby/gems/1.8/gems/mongrel-0.3.12.4/lib/mongrel.rb:495:inrun’
/usr/lib/ruby/gems/1.8/gems/mongrel-0.3.12.4/lib/mongrel.rb:494:in run' /usr/lib/ruby/gems/1.8/gems/mongrel-0.3.12.4/lib/mongrel.rb:483:inrun’
/usr/lib/ruby/gems/1.8/gems/mongrel-0.3.12.4/lib/mongrel.rb:774:in run' /usr/lib/ruby/gems/1.8/gems/mongrel-0.3.12.4/lib/mongrel.rb:772:inrun’
/usr/lib/ruby/gems/1.8/gems/mongrel-0.3.12.4/bin/mongrel_rails:97:in
run' /usr/lib/ruby/gems/1.8/gems/mongrel-0.3.12.4/lib/mongrel/command.rb:163:inrun’
/usr/lib/ruby/gems/1.8/gems/mongrel-0.3.12.4/bin/mongrel_rails:194
/usr/bin/mongrel_rails:18

Request

Parameters: {“id”=>“client_id”}

Show session dump


:quote_id: -609488102
:client_id: -609130666
flash: !map:ActionController::Flash::FlashHash {}

Response
Headers: {“cookie”=>[], “Cache-Control”=>“no-cache”}

Thanking you in advance,

Paul


#6

Thanks for the help so far but sorry, it is still not working :frowning:
Below is what I am getting:

My code:

def edit
@client = Client.find(params[:id])
session[:client_id] = @Client.id
end

The dump:

RuntimeError in QuoteController#edit

Called id for nil, which would mistakenly be 4 – if you really wanted
the id of nil, use object_id

RAILS_ROOT: /home/pjt/quote/config/…
Application Trace | Framework Trace | Full Trace

#{RAILS_ROOT}/app/controllers/quote_controller.rb:13:in `edit’
/usr/bin/mongrel_rails:18

/usr/lib/ruby/gems/1.8/gems/mongrel-0.3.12.4/bin/mongrel_rails:194
/usr/bin/mongrel_rails:18

Request

Parameters: {“id”=>“7”}

Show session dump


:client_id: -610612090
flash: !map:ActionController::Flash::FlashHash {}

Response
Headers: {“cookie”=>[], “Cache-Control”=>“no-cache”}


#7

On Jun 12, 2006, at 9:04 PM, Paul Jonathan T. wrote:

Sorry still have a problem. here is the code

Here is my controller code in quote_controller.rb

def edit
@client = Client.find(params[:id])
session[:client_id] = Client.id

That should be @client.id

end

Here is the link code in the view catalog.rhtml

<%= link_to 'Back to Quote', :controller => "quote", :action => "edit", :id => "client_id" %>


That should be :id => @client


– Tom M.


#8

On Jun 13, 2006, at 4:09 PM, Paul Jonathan T. wrote:

Thanks for the help so far but sorry, it is still not working :frowning:
Below is what I am getting:

My code:

def edit
@client = Client.find(params[:id])
session[:client_id] = @Client.id
end

def edit
@client = Client.find(params[:id])
session[:client_id] = @client.id
end

You need to use the same @client variable. You where using @Client

instead of @client

-Ezra


#9

Ezra Z. wrote:

On Jun 13, 2006, at 4:09 PM, Paul Jonathan T. wrote:

Thanks for the help so far but sorry, it is still not working :frowning:
Below is what I am getting:

My code:

def edit
@client = Client.find(params[:id])
session[:client_id] = @Client.id
end

def edit
@client = Client.find(params[:id])
session[:client_id] = @client.id
end

You need to use the same @client variable. You where using @Client
instead of @client

-Ezra

Hi,

I am also new at Rails and have been struggling with this topic. I
apologize if this is not the correct way of doing things (posting a new
question on a post from someone else). I want to access a variable that
was set up in a controller from a different view. The code is as
follows:

def login
if request.get?
session[:user_id] = nil
@user = User.new
else
@user = User.new(params[:user])
logged_in_user = @user.try_to_login
if logged_in_user
session[:user_id] = logged_in_user.id
redirect_to(:action => “index”)
else
flash[:notice] = “Invalid user/password combination”
end
end
end

in the index view, I would like to access info about this specific user.
I am not sure how to recover it from the session id - or whether that
is an efficient way of doing it versus keeping the whole user record in
the session.

index.rhtml

Your Stats

<%= if @user.correctCount == nil tempUserCorrect = 0 else tempUserCorrect = @user.correctCount end %>

Correct = <%= @tempUserCorrect %>

Incorrect =

it does not find @user (it has a nil value). Not sure why the @user
variable from the login controller is not available in index which it
redirects to.

thanks for any thoughts


#10

On Jun 19, 2006, at 11:47 AM, Seamus Gilchrist wrote:

I am also new at Rails and have been struggling with this topic. I
apologize if this is not the correct way of doing things (posting a
new
question on a post from someone else). I want to access a variable
that
was set up in a controller from a different view. The code is as
follows:

in the index view, I would like to access info about this specific
user.
I am not sure how to recover it from the session id - or whether that
is an efficient way of doing it versus keeping the whole user
record in
the session.

@logged_in_user = User.find(session[:user_id])

Don’t worry about efficiency. And, don’t store the entire object.

Storing the user object will “work”, but then you’ll need to keep
the session user object in sync with the row in the table.


– Tom M.


#11

Tom M. wrote:

On Jun 19, 2006, at 11:47 AM, Seamus Gilchrist wrote:

I am also new at Rails and have been struggling with this topic. I
apologize if this is not the correct way of doing things (posting a
new
question on a post from someone else). I want to access a variable
that
was set up in a controller from a different view. The code is as
follows:

in the index view, I would like to access info about this specific
user.
I am not sure how to recover it from the session id - or whether that
is an efficient way of doing it versus keeping the whole user
record in
the session.

@logged_in_user = User.find(session[:user_id])

Don’t worry about efficiency. And, don’t store the entire object.

Storing the user object will “work”, but then you’ll need to keep
the session user object in sync with the row in the table.


– Tom M.

Thanks - that works.

Why do you suggest not to worry about efficiency? - curious?

Seamus


#12

On Jun 19, 2006, at 12:59 PM, Seamus Gilchrist wrote:


– Tom M.

Thanks - that works.

Why do you suggest not to worry about efficiency? - curious?

Seamus

You’re just learning Rails. You’re at the training wheels stage, but
you’re worrying about the Tour de France.

Learn the framework. Learn to write apps efficiently and quickly.
Apps that are easy to understand and follow, easy for yourself and
others to maintain.

From the guy who wrote the books:

"Premature optimization is the root of all evil (or at least most

of it) in programming."


Source: "Donald Knuth: Computer Programming as an Art" 1974 Turing
         Award lecture


– Tom M.


#13

On 13-jun-2006, at 5:26, Paul Jonathan T. wrote:

Hi, I am a rails and ruby nuby :slight_smile:
This is a bad approach because the “Back to whatever” links in ALL
windows will lead to the latest
search that was done out of any window.


Julian ‘Julik’ Tarkhanov
please send all personal mail to
me at julik.nl


#14

Thanks, I fixed that (and I should have noticed it. no excuse) However
the link_to still does not seem to find the ID.

The code in quote controler

def edit
@client = Client.find(params[:id])
session[:client_id] = @client.id
end

The code in the layout for link_to (catalog controller)

<%= link_to 'Back to Quote', :controller => "quote", :action => "edit", :id => @client %>


The URL that I am getting (no client id!)

http://127.0.0.1:3000/quote/edit

I am exspecting to see this - http://127.0.0.1:3000/quote/edit/7 but no
go :frowning:

The dump!

ActiveRecord::RecordNotFound in QuoteController#edit

Couldn’t find Client without an ID

RAILS_ROOT: /home/pjt/quote/config/…
Application Trace | Framework Trace | Full Trace

/usr/lib/ruby/gems/1.8/gems/mongrel-0.3.12.4/bin/mongrel_rails:97:in
run' /usr/lib/ruby/gems/1.8/gems/mongrel-0.3.12.4/lib/mongrel/command.rb:163:inrun’
/usr/lib/ruby/gems/1.8/gems/mongrel-0.3.12.4/bin/mongrel_rails:194
/usr/bin/mongrel_rails:18

Request

Parameters: None

Show session dump


:client_id: 7
flash: !map:ActionController::Flash::FlashHash {}

Response
Headers: {“cookie”=>[], “Cache-Control”=>“no-cache”}