根据用户来自哪个页面来改变 Rails 表单

发布于 2024-12-11 11:06:48 字数 189 浏览 0 评论 0原文

我有一个会议模型,可以通过多种方式启动。用户可以转到页面并以导师身份初始化会议。他还可以作为学生从另一个页面发起会议。我想要一个通用的事件形式。如果以导师身份启动,则某些字段将自动填写,因此无需在表单中显示这些字段。学生发起的会议也有类似情况。

如何根据页面用户的来源更改表单?如果可能的话我宁愿不使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北城半夏 2024-12-18 11:06:48

我的建议是嵌套路由。您并没有真正描述模型之间的关系,但想法是有 2 个不同的 URL,如下所示:

example.com/students/3/meetings/new
example.com/tutors/7/meetings/new

您的 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:

example.com/students/3/meetings/new
example.com/tutors/7/meetings/new

Your meetings_controller can check for the presence of params[:student_id] or params[: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.

淡笑忘祈一世凡恋 2024-12-18 11:06:48

您可以传递要在请求行上预填充的数据。或者在 events_controller 中,您可以检查 HTTP_REFERRER 并根据需要设置属性。

在请求行上传递数据可能是更好的选择,但这一切都取决于应用程序的详细信息。

一个例子:

<%= link_to edit_event_path(@event, :name => 'Override Name', :day => 'Monday') %>
# will generate /events/123/edit?name=Override%20Name&day=Monday

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:

<%= link_to edit_event_path(@event, :name => 'Override Name', :day => 'Monday') %>
# will generate /events/123/edit?name=Override%20Name&day=Monday
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文