Reputation: 12528
I am quite new to grails. I just noticed that variables in the controller are not visible in the view. I only can get the variable values when I assign it to a scope. Is this the standard Grails way or am I doing this wrong. Also, is the params scope the correct one to use or should I use sessions, servletContext?
In the Controller
String uploadLocation = grailsApplication.config.uploads.location.toString()
params.put "upLoc", uploadLocation
In the View
<td> <input type="text" value="${params.get('uploc')}/${fileResourceInstance.decodeURL()}"></input></td>
I'm very familiar with Ruby on Rails and this is handled very differently in RoR. Thanks.
Upvotes: 7
Views: 6357
Reputation:
One interesting side note. If you don't return anything from your action then all variables in the action's scope will be available in your view as described here: http://www.grails.org/Controllers+-+Models+and+Views
Upvotes: 1
Reputation: 10848
You can do it like Maricel say, but there's another way (I think it's a default way to do): return the values you want to pass to the view in action function. For example
def test = "abc"
def num = 3
return [testInView: test, numInView: num]
Then in view you can access ${testInView}, ${numInView}
Another slightly different way: you can neglect the "return" keyword, it's "groovy way" to return the last value of the function.
Upvotes: 11
Reputation: 2089
You need to pass your variable as part of the model through the render method in your controller action, like this:
String uploadLocation = grailsApplication.config.uploads.location
render(model: [uploadLocation: uploadLocation])
Then in the view you can just do:
<td>
<input type="text" value="${uploadLocation}/${fileResourceInstance.decodeURL()}"/>
</td>
On the other hand, if this is a value that is defined in the Config.groovy, you can do this as well 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 info check the Grails docs.
Upvotes: 6