views:

71

answers:

2

Hi,

This is quite difficult to explain, but here goes. I have a Rails app with students, courses, assignments and grades

In the student show view, there is a list of all of the courses that a student takes, along with the assignments associated with those courses. All assignments which there is a recorded grade for between the student and the assignment also state the grade, all others display 'N/A'. What I would like to do is have a link to a form with the student and the assignment field pre-populated, depending on the link that was clicked.

Here is an example. Peter Parker has a grade for the 'mathematics exam', but not for the 'mathematics CW'. If the user clicks the 'Add' link, I would like a view to be displayed with a form pre-populated with the student name and assignment name, ready for the grade to be added.

alt text

I essentially don't know how best to pass this data to the form, or how to have the form display the data in an uneditable way.

Thanks.

+1  A: 

I'd just link to the form and pass the student_id and assignment_id. You can't truly make a form field "uneditable", but you can validate that that user has permission to view/edit before displaying and saving information.

Anna
Technical correction. You can disable a form field, but you absolutely shouldn't trust that the data from it is unchanged.
Anna
How do I pass the student_id and assignment_id to the form? the system is only going to be used locally, but I will validate everything none the less.
Jack
+2  A: 

Jack you have a few questions here, one of them is relatively easy to display the data in an uneditable way you can make it disabled

http://www.w3.org/TR/html401/interact/forms.html#h-17.12

*How do I pass the student_id and assignment_id to the form? the system is only going to be used locally, but I will validate everything none the less. – Jack*

Jack, you will need something in your current view that can pass the student_id and assignment_id. like a link_to.

<%= link_to "show me my assignments", {:action => 'show', :controller => "assignments"}, :student_id => student.id, :assignment_id => student.assignment[0].id %>

(note it is probably better to use restful routes, but this shows better what is going on)

what this link to will do is call the show action of the assignments controller. you can get the id's in the controller by calling:

assignment_id = params[:assignment_id]
student_id = params[:student_id]
@student = Student.find(:first, :conditions {:id => student_id})
...

Then in your show.html.erb under /app/views/assignments/ directory you would need to render your form code, something like:

<%= form_for @student, :student ...

There are many many possibilities that I glossed over, the question you're asking is a little bit broad. It may be more helpful to break it into smaller chunks (focus less on the application and more on one single action) and re-ask to the forum members.

You could ask more about:

  • Sending data to a controller from a link
  • Finding items in your database from the controller
  • Generating a form from the instance variables in the controller
  • finally disabling text_field in a form

I would say give what i've suggested a shot (send data from the view to a controller, and then use that data from the controller to render another view), and if you cannot figure out something, try to ask a very focused question about the problem you're having, and you will likely end up with a much better over all product.

ThinkBohemian
snowmaninthesun is on the right track here, *but* don't make the form field `disabled` - make it `readonly`. Disabled inputs differ in that they do **not** have their value submitted.
nfm
Thank you so much for this answer snowmaninthesun - really appreciate it and I managed to get it working within about 10 minutes using the pointers in your answer. Top man
Jack