# A newbie question, if possible to help

**URL:** <https://www.ruby-forum.com/t/a-newbie-question-if-possible-to-help/61824>\
**Category:** Rails\
**Created:** [May 28, 2006, 9:16am UTC](https://www.ruby-forum.com/t/a-newbie-question-if-possible-to-help/61824 "2006-05-28T09:16:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![shai\_rosenfeld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@shai\_rosenfeld](https://www.ruby-forum.com/u/shai_rosenfeld)\
**Post date:** [May 28, 2006, 9:16am UTC](https://www.ruby-forum.com/t/a-newbie-question-if-possible-to-help/61824/1 "2006-05-28T09:16:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shai\_rosenfeld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@shai\_rosenfeld](https://www.ruby-forum.com/u/shai_rosenfeld)\
**Post date:** [May 28, 2006, 9:38am UTC](https://www.ruby-forum.com/t/a-newbie-question-if-possible-to-help/61824/2 "2006-05-28T09:38:41Z")

</div>

On 5/28/06, shai rosenfeld [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![shai\_rosenfeld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@shai\_rosenfeld](https://www.ruby-forum.com/u/shai_rosenfeld)\
**Post date:** [May 28, 2006, 9:58am UTC](https://www.ruby-forum.com/t/a-newbie-question-if-possible-to-help/61824/3 "2006-05-28T09:58:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shai\_rosenfeld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@shai\_rosenfeld](https://www.ruby-forum.com/u/shai_rosenfeld)\
**Post date:** [May 28, 2006, 10:15am UTC](https://www.ruby-forum.com/t/a-newbie-question-if-possible-to-help/61824/4 "2006-05-28T10:15:25Z")

</div>

On 5/28/06, shai rosenfeld [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> it doesn’t recognize the statement – could this be because my statement  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](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.

---

<div class="post-metadata">

**Author:** ![shai\_rosenfeld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@shai\_rosenfeld](https://www.ruby-forum.com/u/shai_rosenfeld)\
**Post date:** [May 28, 2006, 2:04pm UTC](https://www.ruby-forum.com/t/a-newbie-question-if-possible-to-help/61824/5 "2006-05-28T14:04:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shai\_rosenfeld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@shai\_rosenfeld](https://www.ruby-forum.com/u/shai_rosenfeld)\
**Post date:** [May 28, 2006, 4:05pm UTC](https://www.ruby-forum.com/t/a-newbie-question-if-possible-to-help/61824/6 "2006-05-28T16:05:50Z")

</div>

On 5/28/06, shai rosenfeld [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> your help.  
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Bevakasha 🙂

---

<div class="post-metadata">

**Author:** ![shai\_rosenfeld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@shai\_rosenfeld](https://www.ruby-forum.com/u/shai_rosenfeld)\
**Post date:** [May 28, 2006, 10:28am UTC](https://www.ruby-forum.com/t/a-newbie-question-if-possible-to-help/61824/7 "2006-05-28T10:28:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shai\_rosenfeld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@shai\_rosenfeld](https://www.ruby-forum.com/u/shai_rosenfeld)\
**Post date:** [May 28, 2006, 5:03pm UTC](https://www.ruby-forum.com/t/a-newbie-question-if-possible-to-help/61824/8 "2006-05-28T17:03:12Z")

</div>

> Bevakasha 🙂

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

---

<div class="post-metadata">

**Author:** ![shai\_rosenfeld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@shai\_rosenfeld](https://www.ruby-forum.com/u/shai_rosenfeld)\
**Post date:** [May 28, 2006, 5:34pm UTC](https://www.ruby-forum.com/t/a-newbie-question-if-possible-to-help/61824/9 "2006-05-28T17:34:44Z")

</div>

On 5/28/06, anonymous coward [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Tel Aviv, Israel (camuvan 😉

You can send it here - removed\_email\_address@domain.invalid - I have a special filter  
for non-list incomings.
