【发布时间】:2012-06-07 17:41:30
【问题描述】:
我有一个名为 teacher 的模型,我想为它添加评分(5 星)。目前,我通过在我的教师资源中添加一个 ratings 嵌套路由(资源评级)来实现这一点。然后我创建了一个模型:使用 (id, user_id, teacher_id, rating, ...) 进行评分。然后我创建了一个带有隐藏字段的表单,其中一个名为stars。当用户点击星星时,我使用 jQuery 发送 AJAX 请求来创建/更新该用户和老师的评分。
我的困惑是:我在页面上有两个单独的表单。我有一个用于撰写审稿人 cmets 的表格。此表单有两个字段:标题、cmets(和提交)。然后我有带有隐藏字段的评级表。这是做这样的事情的正确方法吗?在我看来,我真的应该将评级模型字段以某种方式嵌入到主评论表单中。
高度赞赏任何帮助。谢谢。
[编辑]
我更新了我的应用程序,现在用户不再给 teacher 对象评分,而是对 teacher 的 comment 进行评分
我的设置是这样的:
路线
resources :comments as :teacher_comments do
resource :rating
end
模型
评论
has_one :rating
attr_accessible :body, :rating_attributes
accepts_nested_attributes_for :rating
评分
belongs_to :comment
attr_accessible :stars, :user_id, :teacher_id, :comment_id
查看
<%= form_for( @comment, :remote => true, :url => teacher_comments_path ) do |tc| %>
<%= tc.text_area :body, :maxlength => 450 %>
<%= tc.fields_for :rating do |builder| %>
<%= builder.text_field :stars %>
<% end %>
<% end %>
我没有看到星星的文本字段。它只是没有出现。有什么我错过的吗?
【问题讨论】:
标签: ruby-on-rails