Forum: Ruby on Rails where is error in this code

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.
Sijo k. (Guest)
on 2008-11-04 09:57
I have an action like

def create
    if params[:ticket][:title].empty?
      flash[:notice] = 'Please fill fields marked with *'
      redirect_to :action => 'create_ticket_ui'
    else
  @contact_id =
ContactEmailAddress.get_contact_id(params[:contact_email][:email])
      begin
        ActiveRecord::Base.transaction do
        #Now create a new sd ticket
         ServiceDesk.create_ticket(@contact_id,params[:ticket])
        end #transaction end
      end #begin end
       rescue   #here is the problem
    end #outer if else end
  end

     My problem is when I start typing rescue as above IDE shows Syntax
error.Could you please help me to solve this? What I tried is to get the
error in transaction rescued Already inside the model also I have placed
the code in transaction begin end block and re raised any exception from
there

Thanks in advance
Sijo
Morgan G. (Guest)
on 2008-11-04 10:24
(Received via mailing list)
Kind of fixed version:

def create
  if params[:ticket][:title].empty?
    flash[:notice] = 'Please fill fields marked with *'
    redirect_to :action => 'create_ticket_ui'
  else
  @contact_id =
ContactEmailAddress.get_contact_id(params[:contact_email][:email])
  begin
    ActiveRecord::Base.transaction do
      ServiceDesk.create_ticket(@contact_id,params[:ticket]) #Now create
a
new sd ticket
    end
  rescue
  end
end

The reason why your editor says it's wrong is a superfluous end before
the
rescue clause.

There are a couple of other things you want to change here as well, but
that
depends on your implementation.

   - Use ActiveRecord validations to tell the user which field the error
was
   on. This also means you can be very specific about what can go into
each
   field.
   - Redirect on successful creation of ticket, not on failure (that way
the
   fields can remain filled out)

Here is a version somewhat easier to work with. I don't know how
ServiceDesk.create_ticket works, so I modified it to new_ticket (which
doesn't actually save the ticket that has been created, just returns
it).

def create
  if request.post?
    @contact_id =
ContactEmailAddress.get_contact_id(params[:contact_email][:email])
    @ticket     = ServiceDesk.new_ticket(@contact_id, params[:ticket])

    if @ticket.save
      redirect_to :action => 'create_ticket_ui'
      return
    end
  end

  @ticket ||= ServiceDesk.new_ticket
end

Hope this helps.


Cheers,
Morgan G..



2008/11/4 Sijo Kg <removed_email_address@domain.invalid>

>      begin
> error.Could you please help me to solve this? What I tried is to get the
>
--
--------------------------------------------------------------------------
Morgan G. - Just Landed
General Tel: +34 91 590 2611
removed_email_address@domain.invalid
--------------------------------------------------------------------------
http://www.justlanded.com - Helping people abroad!
30 countries, in up to 8 languages, more to come...
--------------------------------------------------------------------------
Sijo k. (Guest)
on 2008-11-04 10:45
Hi
    Thanks for your reply. But I did not understand what you mean by
superfluous end...But the problem solved when I use begin end for the
whole action instead of else part only..So still dont know why Syntax
error at first

Sijo
Frederick C. (Guest)
on 2008-11-04 11:34
(Received via mailing list)
On Nov 4, 8:45 am, Sijo Kg <removed_email_address@domain.invalid> wrote:
> Hi
>     Thanks for your reply. But I did not understand what you mean by
> superfluous end...But the problem solved when I use begin end for the
> whole action instead of else part only..So still dont know why Syntax
> error at first
>
because the correct syntax is

begin
  ...
rescue
  ...
end
(you could have multiple rescue clauses etc)
not
begin
  ...
end
rescue
  ...
end

Fred
Sijo k. (Guest)
on 2008-11-04 11:38
Hi
   Thanks for the reply
Sijo
Sijo k. (Guest)
on 2008-11-04 12:00
Hi
   One more question related to this What I understood from your reply
was rescue placed inside the begin end block(Am I right?)..Could you
please tell how following working ?

    begin
SDMailer.delive_to_send_mail_to_user(name,email_id,ticket_no) unless
email_id.blank?
    end
    rescue Exception => e:
    puts e.to_s

And the o/p is (here i gave delive deliberately instead of deliver)
undefined method `delive_to_send_mail_to_online_user_on_ticket_creation'
for SDMailer:Class


Sijo
Frederick C. (Guest)
on 2008-11-04 15:08
(Received via mailing list)
On 4 Nov 2008, at 10:00, Sijo Kg wrote:

>    rescue Exception => e:
>    puts e.to_s
>
> And the o/p is (here i gave delive deliberately instead of deliver)
> undefined method
> `delive_to_send_mail_to_online_user_on_ticket_creation'
> for SDMailer:Class
>
rescues can go in other places, eg

def some_method
   ...
rescue ...
   ...
end

so for example you can do

def some_method
   begin
     ...
   end
rescue ...
   end
end

Although the use of a begin/end block doesn't really add anything.

Fred
This topic is locked and can not be replied to.