Grails controller variables not visible in views

I'm brand new to the grail. I just noticed that the variables in the controller are not visible in the view. I can get the values โ€‹โ€‹of variables when I assign it to a scope. Is this the standard Grails way or am I doing it wrong. Also, is the parameter area correct to use, or should I use the sessions, servletContext?

In the controller

String uploadLocation = grailsApplication.config.uploads.location.toString() params.put "upLoc", uploadLocation 

In view

 <td> <input type="text" value="${params.get('uploc')}/${fileResourceInstance.decodeURL()}"></input></td> 

I am very familiar with Ruby on Rails, and in RoR this happens differently. Thanks.

+7
source share
3 answers

You can do this as Maricel says, but there is another way (I think this is the default way): return the values โ€‹โ€‹you want to pass to the view in the action function. for example

 def test = "abc" def num = 3 return [testInView: test, numInView: num] 

Then you can access $ {testInView}, $ {numInView}

Another slightly different way: you can neglect the keyword "return", "groovy way" to return the last value of the function.

+11
source

You need to pass your variable as part of the model using the render method in your controller action, for example:

 String uploadLocation = grailsApplication.config.uploads.location render(model: [uploadLocation: uploadLocation]) 

Then in a view that you can simply do:

 <td> <input type="text" value="${uploadLocation}/${fileResourceInstance.decodeURL()}"/> </td> 

On the other hand, if this is the value defined in Config.groovy, you can also do this in your gsp:

 <%@ page import="org.codehaus.groovy.grails.commons.ConfigurationHolder as CH" %> <td> <input type="text" value="${CH.config.uploads.location}/${fileResourceInstance.decodeURL()}"/> </td> 

For more information check out the Grails docs .

+6
source

One interesting note. If you do not return anything from your action, all variables in the scope will be available in your view, as described here: http://www.grails.org/Controllers+-+Models+and+Views

+1
source

All Articles