where is error in this code

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

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 Grubb.

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

Hi    Thanks for the reply Sijo

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

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

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