根据用户来自哪个页面来改变 Rails 表单
我有一个会议模型,可以通过多种方式启动。用户可以转到页面并以导师身份初始化会议。他还可以作为学生从另一个页面发起会议。我想要一个通用的事件形式。如果以导师身份启动,则某些字段将自动填写,因此无需在表单中显示这些字段。学生发起的会议也有类似情况。
如何根据页面用户的来源更改表单?如果可能的话我宁愿不使用Javascript。
谢谢。
I have a Meeting model, which can be initiated in a couple ways. An user can go to a page and initialize a meeting as Tutor. He can also initiate a meeting from another page as a Student. I would like to have a common form for event. If it is initiated as Tutor, then certain fields will be automatically filled out, thus there's no need to show these fields in the form. Similar situation for meeting initiated by Student.
How can I change the form depending on page user comes from? I would prefer not to use Javascript if possible.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的建议是嵌套路由。您并没有真正描述模型之间的关系,但想法是有 2 个不同的 URL,如下所示:
您的 Meetings_controller 可以检查
params[:student_id]
或是否存在params[:tutor_id]
来确定将显示哪种形式。如果用户实际上以学生或导师的身份登录,那么您可能应该使用该信息(可能来自 cookie)。
My recommendation would be nested routes. You don't really describe the relationship between your models, but the idea would be to have 2 different URLs, like this:
Your meetings_controller can check for the presence of
params[:student_id]
orparams[:tutor_id]
to determine which kind of form it will be displaying.If the user is actually logged in as either a Student or a Tutor, then you should probably be using that information (perhaps from a cookie) instead.
您可以传递要在请求行上预填充的数据。或者在 events_controller 中,您可以检查 HTTP_REFERRER 并根据需要设置属性。
在请求行上传递数据可能是更好的选择,但这一切都取决于应用程序的详细信息。
一个例子:
You could pass data to be pre-filled on the request line. Or in the events_controller, you can check the HTTP_REFERRER and set attributes as needed.
Passing data on the request line is probably the better choice, but it all depends on the details of your app.
An example: