我使用它来允许用户对条目进行投票:
<% form_tag url_for(entry_votes_path(@entry)), :id => 'voting_form', :remote => true do %>
<%= hidden_field_tag 'vote[user_id]', current_user.id %>
<%= submit_tag 'Vote for this entry', :id => 'voting_button' %>
<% end %>
这是我的控制器代码:
def create
@entry = Entry.find(params[:entry_id])
@vote = @entry.votes.build(params[:vote])
respond_to do |format|
if @vote.save
format.html { redirect_to @entry }
format.js
end
end
end
我有两个问题
如何在
current_user.id
不使用隐藏字段的情况下进行分配?另外,我现在没有使用
attr_accessible
或attr_protected
使用 Vote 模型。我应该如何保护模型以确保有人不能创造很多选票?现在,Vote 模型中的所有字段都由params
哈希设置——我应该使用attr_protected
外entry_id
键,然后在控制器中单独设置它吗?