Forum: Ruby on Rails Pass infomation between different views and controllers

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Paul Jonathan Thompson (Guest)
on 2006-06-13 04:58
(Received via mailing list)
Hi, I am a rails and ruby nuby :-)

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
880f96b24ef64881d9ee2e9f062d59a8?d=identicon&s=25 Glenn C. (glenn)
on 2006-06-13 05:14
Paul Jonathan Thompson wrote:
> Hi, I am a rails and ruby nuby :-)
>
> 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?
Paul Jonathan Thompson (Guest)
on 2006-06-13 05:27
(Received via mailing list)
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
Tom Mornini (Guest)
on 2006-06-13 05:36
(Received via mailing list)
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 Mornini
Paul Jonathan Thompson (Guest)
on 2006-06-13 06:07
(Received via mailing list)
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

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

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:in
`find_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:in `edit'
/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:in
`find_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:in
`perform_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:in
`perform_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:in
`perform_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:in
`process_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:in
`process'
/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:in
`process'
/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:in
`process'
/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:in
`process_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:in `run'
/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:in `run'
/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:in
`run'
/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:in
`find_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:in `edit'
/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:in
`perform_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:in `measure'
/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:in
`perform_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:in
`process_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:in
`dispatch'
/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:in `synchronize'
/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:in
`process_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:in `run'
/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:in `run'
/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:in `run'
/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:in
`run'
/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
Tom Mornini (Guest)
on 2006-06-13 09:30
(Received via mailing list)
On Jun 12, 2006, at 9:04 PM, Paul Jonathan Thompson 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
>
> <p><%= link_to 'Back to Quote', :controller => "quote", :action =>
> "edit", :id => "client_id" %></p><br />

That should be :id => @client

--
-- Tom Mornini
Paul Jonathan Thompson (Guest)
on 2006-06-14 01:10
(Received via mailing list)
Thanks for the help so far but sorry, it is still not working :-(
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"}
Ezra Zygmuntowicz (Guest)
on 2006-06-14 02:39
(Received via mailing list)
On Jun 13, 2006, at 4:09 PM, Paul Jonathan Thompson wrote:

> Thanks for the help so far but sorry, it is still not working :-(
> 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
Seamus Gilchrist (Guest)
on 2006-06-19 20:47
Ezra Zygmuntowicz wrote:
> On Jun 13, 2006, at 4:09 PM, Paul Jonathan Thompson wrote:
>
>> Thanks for the help so far but sorry, it is still not working :-(
>> 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
<h1> Your Stats</h1>
<%= if @user.correctCount == nil
		tempUserCorrect = 0
	 else
		tempUserCorrect = @user.correctCount
	 end
	 %>
<p> Correct = <%= @tempUserCorrect %></p>
<p> Incorrect = </p>

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
Tom Mornini (Guest)
on 2006-06-19 21:23
(Received via mailing list)
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 Mornini
Seamus Gilchrist (Guest)
on 2006-06-19 21:59
Tom Mornini 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 Mornini


Thanks - that works.

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

Seamus
Tom Mornini (Guest)
on 2006-06-19 22:22
(Received via mailing list)
On Jun 19, 2006, at 12:59 PM, Seamus Gilchrist wrote:

>> --
>> -- Tom Mornini
>
>
> 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 Mornini
Julian 'Julik' Tarkhanov (Guest)
on 2006-06-20 20:21
(Received via mailing list)
On 13-jun-2006, at 5:26, Paul Jonathan Thompson wrote:

>> > Hi, I am a rails and ruby nuby :-)
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
Paul Jonathan Thompson (Guest)
on 2006-08-03 14:23
(Received via mailing list)
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)

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

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 :-(

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:in
`run'
/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"}
This topic is locked and can not be replied to.