A newbie question, if possible to help

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

On 5/28/06, shai rosenfeld [email protected] wrote:

doing wrong? is there a different way to solve this?


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

On 5/28/06, shai rosenfeld [email protected] wrote:

it doesn’t recognize the statement – could this be because my statement
Posted via http://www.ruby-forum.com/.


Rails mailing list
[email protected]
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.

FYI, a better implementation would use the callback before_destroy.
See pages 134 and 274 in AWDWR. On page 134 Dave T. covers this
exact issue.

Michael

On 5/28/06, shai rosenfeld [email protected] wrote:

your help.
Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Bevakasha :slight_smile:

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

Bevakasha :slight_smile:

toda…where from? any email/other means of contact? yom tov,
s

On 5/28/06, anonymous coward [email protected] wrote:

[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Tel Aviv, Israel (camuvan :wink:

You can send it here - [email protected] - I have a special filter
for non-list incomings.