Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to direct to submit button to another action

Tags:

grails

how can I jump to another action in controller?

I have form and several submit buttons. Every submmit button has name.

<g:form action="save" method="post">
   <g:input name="title" value="${letter.title}" />
   <g:input name="comments[0].text" value="${letter.comments[0].text}" />
   <g:submitButton name="save" value="save" />
   <g:submitButton name="addComment" value="add" />   
</g:form>

def save = {

     if (params.addComment){
       letter.addToComents(  new Comment() ) 
       render(view:'form', model:["letter": letter])
       return
     }

    ...
    if ( letter.save() ) 
    ...
}

def addComment = {
      ...
    }

It works, but it is not good. I want move code from block "addComment" to action addComment:

def save = {

     if (params.addComment){
       // it don´t work
       redirect ( action:"addComment" )
     }

    ...
    if ( letter.save() ) 
    ...
}

def addComment = {
      letter.addToComents(  new Comment() ) 
      render(view:'form', model:["letter": letter])
      return
    }

Or it exists better solution? It would be nice:

<g:submitButton name="save" value="save" action="save" />
<g:submitButton name="addComment" value="add" action="addComment"  /> 

Thanks a lot Tom

like image 596
Tomáš Avatar asked Jun 10 '10 13:06

Tomáš


1 Answers

Use the g:actionSubmit tag instead.

        <g:form  method="post">
           <g:input name="title" value="${letter.title}" />
           <g:input name="comments[0].text" value="${letter.comments[0].text}" />
           <g:actionSubmit action="save" value="Save" />
           <g:actionSubmit action="addComment" value="Add Comment" />   
        </g:form>   
like image 76
Mike Sickler Avatar answered Nov 03 '22 23:11

Mike Sickler