简体   繁体   English

填充Rails表单字段

[英]Populate Rails form fields

This is quite difficult to explain, but here goes. 这很难解释,但这里有。 I have a Rails app with students, courses, assignments and grades 我有一个Rails应用程序,包括学生,课程,作业和成绩

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'. 所有在学生和作业之间有记录成绩的作业也会说明成绩,所有其他作业都显示“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'. 彼得帕克有一个“数学考试”的成绩,但不是'数学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.png 替代文字http://img695.imageshack.us/img695/7540/screenshot20100512at180.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 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? 如何将student_id和assignment_id传递给表单? 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. 杰克,你需要在当前视图中可以传递student_id和assignment_id的东西。 like a link_to. 像一个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. 这个链接将要做的是调用赋值控制器的show动作。 you can get the id's in the controller by calling: 您可以通过调用以下方式获取控制器中的id:

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: 然后在/ app / views / assignments /目录下的show.html.erb中,您需要呈现表单代码,例如:

<%= 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 最后在表单中禁用text_field

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. 我只是链接到表单并传递student_id和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. 您无法真正使表单字段“不可编辑”,但您可以在显示和保存信息之前验证该用户是否有权查看/编辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM