Forum: Ruby on Rails a newbie question, if possible to help

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.
shai rosenfeld (Guest)
on 2006-05-28 11:16
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
Alder G. (Guest)
on 2006-05-28 11:38
(Received via mailing list)
On 5/28/06, shai rosenfeld <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
shai rosenfeld (Guest)
on 2006-05-28 11:58
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
Alder G. (Guest)
on 2006-05-28 12:15
(Received via mailing list)
On 5/28/06, shai rosenfeld <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/.
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> 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.
shai rosenfeld (Guest)
on 2006-05-28 12:28
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
Michael T. (Guest)
on 2006-05-28 16:04
(Received via mailing list)
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
Alder G. (Guest)
on 2006-05-28 18:05
(Received via mailing list)
On 5/28/06, shai rosenfeld <removed_email_address@domain.invalid> wrote:
> your help.
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>

Bevakasha :)
anonymous coward (Guest)
on 2006-05-28 19:03
> Bevakasha :)

toda...where from? any email/other means of contact? yom tov,
s
Alder G. (Guest)
on 2006-05-28 19:34
(Received via mailing list)
On 5/28/06, anonymous coward <removed_email_address@domain.invalid> wrote:
> removed_email_address@domain.invalid
> 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.
This topic is locked and can not be replied to.