Is there a way to check if it was a POST request in Grails, I mean like we do in PHP (if (isset($_POST))). I need it for form submission code in the same controller action which renders a form.
def myform {
if (POST) {
myModel.save
}
render view: myView, model: [user: myModel]
}
I cannot use params, because there are always some parameters and it's not empty.
You can do if(request.method == 'POST')
In a Grails controller you have access to request which is an HttpServletRequest. Using the getMethod() method you should be able to do something like this:
if (request.getMethod().equals('POST'))
Related
In my jobsController, I have a method named getEmployee(). This method renders view named employee.gsp.
render(view : "employee")
When my view is displayed, the url is generated as given below.
http://localhost:8080/test/jobs/getEmployee
Now in this URL I want to append a parameter pagination=false. So my new url should look like:
http://localhost:8080/test/jobs/getEmployee?pagination=false.
How can I do this? Is there any way to append parameters in the generated URL from the controller method getEmployee?
there is a trick to do so, by redirecting to getEmployee action with params.pagination = false
redirect action:'getEmployee', params:[pagination:false]
for example
assume I want 'x' parameter in my /cont/checkGet?x=false like this, so I will redirect from 'check' action to 'checkGet' action with x params
def check(){
redirect action:"checkGet",params: [x:false]
}
def checkGet(){
render "Anshul"
}
hope this helps, Thanks
Hi I'm using Grails controller to delete data from Domain Class and on successful delete I would like to redirect to a very specific page. Can someone tell me how to do that?
I Googled around and I found this link:
http://grails.org/doc/latest/ref/Controllers/redirect.html
I'm just not sure how do I embed this in my controller's definition:
def delete(DomainClass domainClass){
respond domainClass view: 'confirmDelete'
}
If you are deleting an instance, let's say
Book.groovy // domain object
Bookcontroller.groovy // controller
then
// your delete logic
def delete() {
...
book.delete()// this performs the delete and upon successful deletion
redirect(controller: "book", action: "show")//you will be redirected to new page called show.gsp
...
}
hope this helps you.
If redirecting to the same controller:
def delete() {
// your delete code
redirect action: "someAction"
}
If redirecting to another controller
def delete() {
// your delete code
redirect action: "someAction", controller: "someController"
}
My javascript send Ajax request which will invoke a controller function, then the controller function response to the Ajax request. My problem is in the controller response part.
my javascript which send Ajax request to controller:
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
//RENDER RESPONSE #cars here
}
}
xmlhttp.open("GET","/cars/reserved_cars/"+customer_id,true);
xmlhttp.send();
So, the above ajax request will invoke my CarsController's reserved_cars function with customer_id as parameter.
My CarsController:
class CarsController < BaseController
def reserved_cars
customer_id=params[:customer_id]
#cars = Car.getCars(customer_id)
end
...
end
My controller get all cars by query Car model with customer_id.
Everything works fine, I just don't know how to return the #cars in controller as response of my ajax request to my javascript (the place in my javascript where I commented "//RENDER RESPONSE #cars here")
So, How to get #cars response in my javascript?
Thanks guys, I figured out the solution.
I first convert #cars to string cars_str,
then use render :json => cars_str in my controller,
in my javascript I can get this cars_str string with xmlhttp.responseText
HTTP protocol always transfers the string, if you are trying to return array or other non scalar variable you will fail.
The solution would be to format your variable as a string and then parse it in JavaScript.
You will need to write an ajax response
class CarsController < BaseController
def reserved_cars
customer_id=params[:customer_id]
#cars = Car.getCars(customer_id)
respond_to do |format|
format.js
end
end
end
In your cars view you add reserved_cars.js.rjs that contains the javascript. This file contains your javascript code like:
page.replace_html :cars, :partial => '/cars/car', :collection => #cars
replace_html will replace the inner html of the DOM element with id cars (eg. <div id="cars"></div>) . the partial displays 1 car
I hope this helps
This page
"/cars/reserved_cars/"+customer_id"
Needs to return a page with text on it. This text can then be accessed by accessing the responseText of your xmlhttp object.
if (xmlhttp.readyState==4 && xmlhttp.status==200){
//RENDER RESPONSE #cars here
responseFromCar = xmlhttp.responseText;
}
Where responseFromCar will be the text returned from your request. Then just parse the text as necessary to find the associated info you need.
Note: You could also use responseXML instead of responseText if you are expecting a properly formatted XML page back from your request. This approach would make parsing it a bit easier.
In order to send appropriate response, I need to detect whether the controller action has been requested by a classic HTTP GET request, an AJAX request or a g:include tag lib.
For instance, considering the following snippet code:
class CommunityController {
def show = {
def users = getUsers()
if (/* WHAT IS THE CODE HERE??? */) //g:include request => render 'show' template only
render template:'show', model=[users]
else if (request.xhr) //Ajax => we send JSON content
render users as JSON
else //Classic request => we render 'show' GSP page
[users]
}
}
...how can I detect that the action has been called via a g:include tag lib?
Thank you.
You can test it like this:
import org.springframework.web.util.WebUtils
if (request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)) {
// request was included
}
how to send file through xmlhttprequest.send(file) in rails , post method not calling respective action(method) in the controller , get method calling the same action which specified in the post method and how to get the request(body) content in the controller ?
Any idea?
You should be able to get it from your request object. Maybe something like this:
path = "public/"
File.open(path, "wb") { |f|
f.write(request.body.read)
}