开发者

Populate Rails form fields

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 http://img695.imageshack.us/img695/7540/screenshot20100512at180开发者_C百科.png

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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜