一种模型和控制器,但 Rails3 中的视图和路线略有不同

发布于 2024-09-26 13:26:27 字数 1050 浏览 4 评论 0原文

我有一个通知模型,它基本上处理相同的一些事情。一个简单的联系表、邀请表等。它们都有相同的通用项目……即姓名、电子邮件、评论等等。

可能有一个稍微不同的字段,但它们是可选的,所以我想将它们视为一个模型,并具有一个名为: notification_type 的区分字段,因此邀请或反馈等。并且仅渲染不同的视图我在通知下的子文件夹中创建(通知/邀请/)。

我让它在我的路线中与类似这样的东西正常工作:

路线

resources :notifications
match 'invites' => 'notifications#new', :defaults => { :notification_type => 'invitation' } 

我传递 notification_type 的

... new.html.erb

<% if params[:notification_type] or params[:notification][:notification_type] == "invitation" %>
 <%= render "notifications/invitations/form" %>
<% end %>

form.html.erb

我传递 notification_type 的隐藏字段

<%= f.input :notification_type, :as => 'hidden', :input_html => { :value => @notification.notification_type ||= params[:notification_type] } %>

这一切似乎都有效.. 唯一的警告是如果他们创建了一个错误,它会将它们发送到 /notification 路由而不是在邀请中..但它仍然可以正常工作,否则但我想知道有一种更简单的方法可以做同样的事情?从控制器层内部?我觉得稍后会有一些事情让我感到惊讶。

I have a model Notifications, and it basically handles the same few things. A simple contact form, an invitations form, etc. They all have the same generic items... ie Name, email, comment, blah.

There might be one slightly different field but they are optional, so I'd like to treat them as one model with a differentiating field called: notification_type so Invitation or Feedback, etc. And only render different views which i created in subfolders under notifications (notifications/invitations/).

I have it working fine with something like this in my routes:

routes

resources :notifications
match 'invites' => 'notifications#new', :defaults => { :notification_type => 'invitation' } 

I pass the notification_type...

new.html.erb

<% if params[:notification_type] or params[:notification][:notification_type] == "invitation" %>
 <%= render "notifications/invitations/form" %>
<% end %>

form.html.erb

I pass a hidden field for the notification_type

<%= f.input :notification_type, :as => 'hidden', :input_html => { :value => @notification.notification_type ||= params[:notification_type] } %>

It all seems to work.. the only caveat being that if they create an error, it sends them to the /notification route instead of being in invites.. but it still works correctly otherwise but I'm wondering it there's a simpler way to do the same thing? From within the controller layer? I feel like something's going to surprise me later as it stands.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

生生漫 2024-10-03 13:26:27

我会在模型中进行继承并有两个控制器。我的控制器尽可能小,所以通常我可以添加许多控制器。但这是表单错误之外的一个非常巧妙的解决方案。我认为你的预感可能是正确的,因为事情变得更加复杂。

因此,为了清楚起见,我必须对邀请和反馈进行建模。每个模型都会将 set_table_name 设置为“notifications”,继承自 Notification,然后在初始化方法中设置默认通知,或者使用路由中已有的 :defaults 块。在我看来,这更加灵活和明确。

I'd do the inheritance in the model and have two controllers. My controllers are as small as possible so normally I'm ok with adding many of them. But this is a pretty neat solution outside of the form errors. I think your hunch might be right as it gets more complex.

So to be clear, I'd have to models Invitation and Feedback. Each model would set_table_name to "notifications", inherit from Notification and then set the default notification in the initialize method or use the :defaults block you have in your routes already. This is a bit more flexible and explicit imo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文