Inserting values into grails createlink params on page creation - grails

I've got a list view and for each line I want a link to another list view showing the related results. The createlink line I've finally managed to construct looks like this
my link
This generates the link
http://localhost:3278/FARTFramework/runResults/listSpecific/testExecQueueInstance.id
I know that testExecQueueInstance.id is 22 and I want the link to actually look like this
http://localhost:3278/FARTFramework/runResults/listSpecific/22
What am I missing?
The idea is that I then have a controller like so which should then list those items matching that id...
def ListSpecific(int id){
render(view: "list", model: [runResultsInstanceList: RunResults.findAllByTestExeQueueID(id, [sort:"testExecTime", order: "desc"]), runResultsInstanceTotal: RunResults.count()])
}

You are using an apostrophe on map of params.
You should try this.
my link
enjoy.

Related

Route(URL) error for each controller

I am new to ASP.Net MVC 4. I have 2 controllers
Customer
Supplier
each controller have a menu CustomerSetup and SupplierSetup.
The first visited menu is ok, e.g when i click on CustomerSetup Menu, url looks like localhost:1496/Customer/Index its ok, now when i click on Supplier Menu, url looks like localhost:1496/Customer/Supplier/Index instead of localhost:1496/Supplier/Index and below error is shown..
Server Error in '/' Application.
The resource cannot be found.
The problem is probably because you're using hand-coded URL to point to your Supplier.Index action. You better use the Url.Action() method instead:
Supplier Menu
See Documentation
I think using html helper can help:
#Html.ActionLink("Menu name(display to user)","Action_name","Controller_name")
This will take you to your desired controller action.
eg:
#Html.ActionLink("Home","Index","Home")

How to get a passed "id" param through a link from a gsp file to another gsp file in grails

Suppose I have a gsp file with a link with is working with the tag "Read more. This links opens another gsp file name "blog" and if you look at the url bar, you can see my current link is "MyWebApp/post/blog/(current i value). So how do I get the (current i value) wich is actually an integer, from the new blog.gsp file?
class PostController {
def list() {
// this action calls the page that has the "Read more" link
def posts = Post.list()
[posts: posts]
}
def blog() {
// this action is triggered by the "Read more" link and
// renders your blog post where you want the current ID
def post = Post.get(params.id)
[post: post]
}
}
blog.gsp
<html>
....
${post.id}
....
</html>
UPDATE
You can probably do the following
<g:link action="blog" controller="post" params="['id': '${i}']">
Read more
</g:link>
It depends on what i is via url mappings, but I guess it would be ${params.i}
When you create links or submit forms, your information is stored in the params Map. This map is accessible by your controller and with that you can do whatever you need like passing the data to your view, or perform query's and then pass the result to the view.
The simplest way of understand the flow controller > view > controller > another view is to use grails command generate-all and check the basic crud for your domain class. See how the show and edit works.
Gregg's answer is probably what you're looking for. If it not works, maybe you're passing an invalid id. You can check if the post exists before showing the content using <g:if>, for example:
<g:if test="${post}">
id: ${post.id}
</g:if>
<g:else>
<p>This blog post don't exists.</p>
</g:else>

Rails: Compiling URL with variable

I'm looking to redirect users to another website, and include their user id in the link.
The problem is that the url is different for each record.
The desired URL: ..../index.php?user_id=123
In another url, it might be: ..../index.php?user=123&foo=true
The desired form entry: http://www.google.com/index.php?user_id=#{#user.id}
How would I do this?
Thanks in advance!
Mitch
Insert the ID in a normal link tag:
Link text
Or create the link in a helper, model method, etc:
def google_url(id)
"http://www.google.com/index.php?user_id=#{id}"
end
Link text
There are a few other options, I suppose.

The tag g:include is not passing the model to the view correctly

I have been struggling with these for a few hours now, and have come to the conclusion that it may be a bug with the 2.0.0.RC1.
At first I thought it was the project I was working on, but then I created a complete new project and was able to recreate the bug. Am using grails 2.0.0.RC1.
The bug presents itself when I try to include a model object in a GSP, for example:
/hello/index.gsp
<p>This data is coming from the model:</p>
<p>content: ${content}</p>
<g:include model="${otherModel}" view="/hello/include.gsp" />
Now in my action I have something like:
HelloController.groovy
package helloworld
class HelloController {
def index() {
def model = [:]
model.content = 'content...'
def includeModel = [:]
includeModel.content = 'includeModel...'
model.otherModel = includeModel
render( view:'index', model:model )
}
}
The /hello/include.gsp file contains the following:
/hello/include.gsp
<p>This data is coming from the included model:</p>
<p>content: ${content}</p>
But, what shows up on the page is not what I am expecting, this is what shows on the page:
http://localhost:8080/helloworld/hello/index
This data is coming from the model:
content: content...
This data is coming from the included model:
content: content...
Any ideas? Any help is greatly appreciated.
Thanks,
-Cesar
It might be a bug, but according to the docs, the include tag is specifically designed to include the response of another controller/action or view in the current response -- not just additional GSPs. If you want to "include" another GSP into your page, you really should use the render tag. I have verified that your code works correctly with the render tag and renaming include.gsp to _include.gsp and making the tag be <g:render model="${otherModel}" template="include" />. I got the following output:
This data is coming from the model:
content: content...
Using g:render:
This data is coming from the included model:
content: includeModel...
I also tried adding another action to the controller to return the included content and render include.gsp, and then using the g:include tag to output that in the page and it worked:
def include() {
def includeModel = [:]
includeModel.content = 'includeModel...'
includeModel
}
And then in the index.gsp I added:
<g:include action="include"/>
And I got:
This data is coming from the model:
content: content...
Using g:render:
This data is coming from the included model:
content: includeModel...
Using g:include with action
This data is coming from the included model:
content: includeModel...
Also, you don't have to specify render(view:'viewname, ...) in your controller if the view is the same name as the method in the controller. You can just return the model and it Grails will automatically pick the GSP file with the same name as the controller action.
All that being said, it still seems like what you're trying to do with the include tag should work, and I can't explain why it's not (and the source code for the tag isn't showing up at the bottom of the docs like it's supposed to either). I'd recommend filing a JIRA though if the render tag isn't an option for you.

Ajax on Rails - I'm having an issue

I'm working on a Ruby on Rails web app, in which I've got several lists of posts. Each post will use Ajax to load their comments. In order of populating them in the right place, I'm doing the following:
Each post has a div with an id formatted as follows: <div id="my_div_<%= post.id %>"></div>. So, for example if the post.id is 12, the id will be "my_div_12".
My controller's action looks like this:
render :update do |page|
page.replace_html 'my_div_' + params[:post_id].to_s, :partial => 'my_comments_section_partial_path'
end
That works fine only if I have a post only once at the page. But in this site, each post might be listed more than once, because there are several lists (latest posts, popular, tops, etc). And all of them will have their Comments section to show.
The issue now is that, as the comments section functionality is inside a partial view, it will work the same for every type of list (as it's expected), and therefore, it doesn't make the difference between the divs (because they will have the same post.id, and thus, the same div's id).
My question now is: how could I solv this problem? Is there a better and different way for doing this?
Thanks in advance!
EDIT:
Just to clarify what I want, in few words:
I would like the code page.replace_html 'my_div_' + params[:post_id].to_s, :partial => 'my_comments_section_partial_path' to replace that partial in EVERY div called 'my_div' + params[:post_id].to_s (because there may be more than one div in the same page with the same id).
Note that having several elements with the same id on one page produces technically invalid html. Many browsers won't even render all those id attributes (leaving one and erasing the rest).
One simple solution is to switch to using classes instead of ids. There can be multiple elements with the same class and each element can have more than one class.
I don't work with built-in ajax helpers in Rails, but google suggests it's possible.
It seems like you have two sets of posts on the same page and are worried about putting the comments for the correct post in the correct spot. I think making two Ajax requests would be fine. The URLs would probably be something like /posts/1/comments and /posts/2/comments. Essentially what you want is "all the comments for a given post". You could have a comments action on your posts controller to use your has_many :comments association from Post. Using the URL ID parameter you can supply the post ID. You could get the Post ID out of the markup through a custom ID attribute, a custom class, or a custom data-* attribute.
It looks like you're using RJS but I'd recommend using Prototype or jQuery to make the Ajax request and specify JSON data or HTML as the response, then append the JSON or HTMl in the correct spot.
In jQuery I'd grab the post ID, make the ajax request, then append the comment HTML. This is not tested, but roughly the idea in code.
var comments_div = $('.post .comments'); /* need a diff. comments div for each */
var post_id = $('.post').attr('data-post_id');
$.get({'/posts/'+post_id+'/comments', function(data){ comments_div.append(data); } });

Resources