1

I am trying to send an ID of an iterated item but I think the whole list is being sent. How can I send just one ID? I have STUDENT and COURSE domain class.

Domain Model

class Student {
    String fullName
    String toString() {
        "$fullName"
    }

    static belongsTo = [school: School]
    static hasMany = [courses:Course, studentCourses:StudentCourse]
}

class Course {
    String course
    String toString() {
        "$course"
    }

    static hasMany = [studentCourses:StudentCourse]
    static belongsTo = Student

}

class StudentCourse {
    Student student
    Course course

    //Some methods...
}

And this is my edit view.

<g:if test="${studentInstance.studentCourses?.course}">
        <g:each class="courseList" in="${studentInstance.studentCourses?.course}" var="courses">
            <li class="courseList">
                <span class="courseList" aria-labelledby="courses-label">${courses.id} ${courses}
                <g:actionSubmitImage class="deleteIcon" action="deleteCourse" value="delete"
                 src="${resource(dir: 'images', file: 'delete.png')}" height="17"
                 onclick="return confirm('${message(code: 'default.button.delete.confirm.message', default: 'Are you sure?')}');"
                 params="${courses.id}"/></span>
            </li>
        </g:each>
    </g:if>

I'd like to be able to delete one course from the list when user clicks delete.png image. But when I println params.course the parameter is being sent as a whole list, not as an individual item of the list even though it is within g:each tag. How can I just send one corresponding item to the controller?

My edit page has a list of courses.

Course 23  English (delete icon here)
       42  Science (delete icon here)
       67  Math (delete icon here)

In my println params.course this is what I see.

[ English, Science, Math ]

How can I just have [English] when user clicks the delete button next to English line?

Thank you in advance.

monty_bean
  • 494
  • 5
  • 25

1 Answers1

3

As far as I have understand your problem, The problem should be in your taglib

I am giving you the example of similar code and this is working.

<g:each in="${student?.studentCourses?.course}" var="course">
<div>
    <span>${course.id} ${course.course}</span>
    <g:link controller="login" action="delete" params="[courseId:course.id]">
        Delete
    </g:link>
</div>

In this block of code, by clicking delete link with params having params="${[id:course.id]}, it will redirect to delete action inside login controller. Print params in delete action and you will get output as params: [courseId:2, action:delete, format:null, controller:login] Here you can see your params.courseId is only a Long value not a list.

Hope it will help you understand your problem.

Gregg
  • 34,973
  • 19
  • 109
  • 214
Hari Om
  • 56
  • 1
  • 6
  • I tried but it blows up and says cannot print id on null object. However, if I use `params="${[courseId:courses.id]}"` it prints the entire entry like this. – monty_bean Dec 28 '15 at 20:17
  • [startDate:date.struct, school.id:1, school:[id:1], _action_deleteCourse:delete, endDate:date.struct, endDate_month:12, _method:PUT, endDate_day:31, endDate_picker:12/31/2015, endDate_year:2015, version:7, startDate_month:12, _action_deleteCourse.y:14, action_deleteCourse:[_x:15, _y:14], _action_deleteCourse.x:15, startDate_day:7, startDate_year:2011, course:[English, Science, Math], fullName:John Doe, startDate_picker:12/07/2011, id:121, action:update, format:null, controller:student] – monty_bean Dec 28 '15 at 20:17
  • 1
    your output shows that you are doing some processing in your taglib. Your taglib is responsible for this output. – Hari Om Dec 28 '15 at 20:33
  • 1
    `params="${[courseId:course.id]}"` should be `params="[courseId:course.id]"`. No need for the `${ }` – Gregg Dec 28 '15 at 20:35
  • change your actionSubmitImage taglib to a simple button and use tag instead of actionSubmitImage. You will get your desired output. – Hari Om Dec 28 '15 at 20:39
  • My goodness! It worked. Thank you @Hari Om I used `g:link` tag per your advice but forgot to close it. Instead of `g:actionSumbitImage`, I used `g:link` and `asset image` tag. Thank you. @Gregg, Thanks for your comment. I got rid of `${ }` but the result was the same. Why is it? – monty_bean Dec 28 '15 at 21:03