i was wondering if anyone can help me form a very simple "if" statment
in the controller...
i did a usual scaffold for table ''pages'', and everything is
working out
well, i did the main site ''backbone'' and all, and everythings
good....the thing is, now i need to make sure that the site-admin does
not have the possiblity to destroy page 1 or page 2.
so, i tried a couple things myself(added onto the ''destroy''
action in
the scaffold defualt) but it didn''t really work out for me...what am i
doing wrong? is there a different way to solve this?
--------regular --------
def destroy
Page.find(params[:id]).destroy
redirect_to :action => ''list''
end
-----------------
and this is my try
-------------mytry---------
def destroy
if params[:id] == 1
flash[:notice] = ''you cannot destroy this page''
redirect_to :action => ''list''
else
Page.find(params[:id]).destroy
redirect_to :action => ''list''
end
------------------
why isn''t this working?
any help will be greatly appreciated.
thanks ahead of time,
shai rosenfeld
--
Posted via http://www.ruby-forum.com/.
On 5/28/06, shai rosenfeld <shai@octava.co.il> wrote:> i was wondering if anyone can help me form a very simple "if" statment > in the controller... > > i did a usual scaffold for table ''pages'', and everything is working out > well, i did the main site ''backbone'' and all, and everythings > good....the thing is, now i need to make sure that the site-admin does > not have the possiblity to destroy page 1 or page 2. > > so, i tried a couple things myself(added onto the ''destroy'' action in > the scaffold defualt) but it didn''t really work out for me...what am i > doing wrong? is there a different way to solve this? > > --------regular -------- > def destroy > Page.find(params[:id]).destroy > redirect_to :action => ''list'' > end > ----------------- > > and this is my try > > > -------------mytry--------- > def destroy > > if params[:id] == 1 > flash[:notice] = ''you cannot destroy this page'' > redirect_to :action => ''list'' > else > Page.find(params[:id]).destroy > redirect_to :action => ''list'' > end > > ------------------ > > why isn''t this working? > any help will be greatly appreciated. > thanks ahead of time, > > shai rosenfeld >Looks like you forgot to put an "end" after the else clause. It should be: def destroy if params[:id] == 1 flash[:notice] = ''you cannot destroy this page'' redirect_to :action => ''list'' else Page.find(params[:id]).destroy redirect_to :action => ''list'' end end
alder, thanks for the advice, but apperently (is that spelled
correctly?) not enough . . . it seems that rails can identify
params[:id] when it''s used within
"Page.find(params[:id]).destroy"
but when i do
"if params[:id]"
it doesn''t recognize the statement -- could this be because my
statement
is incorrect (i.e, syntax) or what? how should i be writing this
correctly? (the if statemnt for preventing someone to delete(=destroy)
page 1/2)...
anyone who can help, or has a clue, thanks again.
shai
--
Posted via http://www.ruby-forum.com/.
On 5/28/06, shai rosenfeld <shai@octava.co.il> wrote:> alder, thanks for the advice, but apperently (is that spelled > correctly?) not enough . . . it seems that rails can identify > params[:id] when it''s used within > > "Page.find(params[:id]).destroy" > > but when i do > > "if params[:id]" > > it doesn''t recognize the statement -- could this be because my statement > is incorrect (i.e, syntax) or what? how should i be writing this > correctly? (the if statemnt for preventing someone to delete(=destroy) > page 1/2)... > > anyone who can help, or has a clue, thanks again. > shai > > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >Syntactically, what you wrote is fine. However, note that parameters coming from params are always *Strings*, not Integers. So trying to compare params[:id] in its raw string state to the integer 1 would always return false. Try if params[:id].to_i == 1 intead. -- -Alder
shai rosenfeld
2006-May-28 08:28 UTC
[Rails] Re: Re: a newbie question, if possible to help
nothing else i can say but ... thanks! not only am i a newbie in ruby, but i am kind of a newbie in forums too (as up until lately i thought it was better to break my head hours upon something, than give up and ''ask for directions'') - until i found out it''s possible to get to the same location three hours earlier, with a lousy question mark and a handful of good people . . . so, thanks for your help. (what i ended up doing was params[:id] = ''1'' (as apposed to 1) thanks again. shai -- Posted via http://www.ruby-forum.com/.
Michael Trier
2006-May-28 12:02 UTC
[Rails] Re: Re: a newbie question, if possible to help
FYI, a better implementation would use the callback before_destroy. See pages 134 and 274 in AWDWR. On page 134 Dave Thomas covers this exact issue. Michael
On 5/28/06, shai rosenfeld <shai@octava.co.il> wrote:> nothing else i can say but ... > > > thanks! > > not only am i a newbie in ruby, but i am kind of a newbie in forums too > (as up until lately i thought it was better to break my head hours upon > something, than give up and ''ask for directions'') - until i found out > it''s possible to get to the same location three hours earlier, with a > lousy question mark and a handful of good people . . . so, thanks for > your help. > > (what i ended up doing was params[:id] = ''1'' (as apposed to 1) > thanks again. > > > shai > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >Bevakasha :) -- -Alder
anonymous coward
2006-May-28 15:03 UTC
[Rails] Re: Re: Re: a newbie question, if possible to help
> Bevakasha :)toda...where from? any email/other means of contact? yom tov, s -- Posted via http://www.ruby-forum.com/.
Alder Green
2006-May-28 15:33 UTC
[Rails] Re: Re: Re: a newbie question, if possible to help
On 5/28/06, anonymous coward <shai@octava.co.il> wrote:> > > Bevakasha :) > > toda...where from? any email/other means of contact? yom tov, > s > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >Tel Aviv, Israel (camuvan ;) You can send it here - alder.green@gmail.com - I have a special filter for non-list incomings. -- -Alder