How to direct the submit button to another action

How can I go to another action in the controller?

I have a form and several submit buttons. Each submit button has a 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 to move the code from the "addComment" block to the addComment action:

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 is this the best 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

+5
source share
2 answers

Use g: actionSubmit 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>   
+13
source

For those using the Twitter Bootstrap plugin (or something other than the text in your button) and want to add a glyphicon to the button, you will need to use the button tag. So you need to do something like

SNIPPET 1.

   <g:form  role="form" method="post">
      ...your inputs

    <button type="submit" name="_action_save">
     <span class="glyphicon glyphicon-ok"></span>
     Save
    </button>

    <button type="submit"  name="_action_saveAndNew">
       <span class="glyphicon glyphicon-ok"></span>
        Save and New
     </button>
  </g:form> 

  _action_ 

-

   name="_action_yourActionName"

, twitter Bottrrap plugin 3.0,

   <span class="glyphicon glyphicon-ok"></span>

SNIPPET 1. :

   <g:form  role="form" method="post">
      ...your inputs        

    <g:actionSubmit action="save" value="Save" />

    <g:actionSubmit action="saveAndNew"  value="Save and New" />

  </g:form> 

actionSubmit , . , actionSubmit, .

0

All Articles