成员创建属于他们和另一个模型问题的投票。目前我正在使用隐藏表单并传递适当的参数来执行此操作。这是问题索引视图的代码:
<%= form_for(@vote) do |f| %>
<%= f.hidden_field "issue_id", :value => issue.id %>
<%= f.hidden_field "member_id", :value => session[:member_id] %>
<%= f.hidden_field "type", :value => :Upvote %>
<%= f.label issue.upvotes_count(issue.id) %>
<%= submit_tag "Up", :class => 'up-vote' %>
<% end %>
这似乎并不理想,因为它使 issue_id 和 member_id 对批量分配开放。有没有更好的方法来使用 button_to 标签或其他东西?
这是控制器代码:
class VotesController < ApplicationController
#GET
def new
@vote = Vote.new
end
# POST
def create
@vote = Vote.new(params[:vote])
@vote.member_id = current_member
if @vote.save
redirect_to issues_path
else
redirect_to issues_path, notice: "you must be logged in to vote"
end
end
end
和
class IssuesController < ApplicationController
# GET
def index
@issues = Issue.find(:all)
@vote = Vote.new
end
# GET
def show
@issue = Issue.find(params[:id])
respond_to do |format|
format.html
format.js
end
end
end