I''ve been using rails for about a month and I''ve been noticing that most of my controller functions use values from my session variable. I find that I write something like: person = Person.find(session[:person] if( person ) # ... else flash[:notice] = "unable to retrieve person" redirect_to( :action => index ) end It seems redundant to write similar code for every action. I could use a hook to check the value before each action, but then I would be accessing my database twice for the same data. Is it overkill to be making sure something wasn''t deleted out of the database on each action? How bad is it if my page tries to use an objec that is nil? What is the standard practice? Thanks for your help, Chris -- Posted via http://www.ruby-forum.com/.
I''ve been using rails for about a month and I''ve been noticing that most of my controller functions use values from my session variable. I find that I write something like: person = Person.find(session[:person]) if( person ) # ... else flash[:notice] = "unable to retrieve person" redirect_to( :action => index ) end It seems redundant to write similar code for every action. I could use a hook to check the value before each action, but then I would be accessing my database twice for the same data. Is it overkill to be making sure something wasn''t deleted out of the database on each action? How bad is it if my page tries to use an objec that is nil? What is the standard practice? Thanks for your help, Chris -- Posted via http://www.ruby-forum.com/.
Maybe you can write a private method in your controller that do this work for you. Chris Rericha escribi?:> I''ve been using rails for about a month and I''ve been noticing that most > of my controller functions use values from my session variable. I find > that I write something like: > > person = Person.find(session[:person] > if( person ) > # ... > else > flash[:notice] = "unable to retrieve person" > redirect_to( :action => index ) > end > > It seems redundant to write similar code for every action. I could use > a hook to check the value before each action, but then I would be > accessing my database twice for the same data. Is it overkill to be > making sure something wasn''t deleted out of the database on each action? > How bad is it if my page tries to use an objec that is nil? What is the > standard practice? > > Thanks for your help, > Chris >
Here''s how I''ve done it before...
class PersonController < ApplicationController
before_filter :get_person, :only =>[:show, :edit, :update, :delete]
def list
@people = Person.find :all
end
def show
end
def edit
end
def update
if @person.update_attributes params[:person]
....
end
def delete
....
end
private
def get_person
begin
@person = Person.find session[:id]
rescue
flash[:notice] = "No person could be found with that ID."
redirect_to :action => "list"
end
end
end
get_person makes @person available. You use it as a before_filter on the
methods where you need it.
Just a thought. Remember, if you do @ person.find params[:id] and that
person isn''t there, it will throw an exception.... your if block
won''t
work.That''s why the above block catches the exception.
This particular example has not been tested for typos but it should work for
you.
-bph
On 7/10/06, Chris Rericha < tristipher@gmail.com>
wrote:>
> I''ve been using rails for about a month and I''ve been
noticing that most
> of my controller functions use values from my session variable. I find
> that I write something like:
>
> person = Person.find(session[:person])
> if( person )
> # ...
> else
> flash[:notice] = "unable to retrieve person"
> redirect_to( :action => index )
> end
>
> It seems redundant to write similar code for every action. I could use
> a hook to check the value before each action, but then I would be
> accessing my database twice for the same data. Is it overkill to be
> making sure something wasn''t deleted out of the database on each
action?
> How bad is it if my page tries to use an objec that is nil? What is the
> standard practice?
>
> Thanks for your help,
> Chris
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060711/339b5b6b/attachment.html
Reasonably Related Threads
- Trouble defining a stub method for a controller
- Samba Won't Start after Upgrade to 4.8.3-4 on Centos 7.5.1804
- Interest in commercial support for R, R-metrics and related packages
- Help with spec controller
- [Bug 1185] New: counter flag proposal for sets and maps