从显示视图:我想将显示的消息ID传递给丢弃操作并废弃消息。
从索引视图:我想将已检查邮件的ID传递给丢弃操作并立即将其全部删除。
但即使我检查多个并从索引视图提交,我也只能一次删除一条记录 如何使用相同的动作????
归档1和2路线
match 'messages/discard(/:id)' => 'messages#discard', :via => :post , :as => :discard_messages
索引视图
<%= form_tag(:action => discard, :via => 'post') do %>
<% @messages.each do |m| %>
<tr>
<td><%= check_box_tag "id",m.id %></td>
<td><%= m.last_message.id %></td>
<td><%= 'unread' if m.is_unread?(current_user) %></td>
<td><%= m.last_message.created_at.to_s(:jp) %></td>
<td><%= m.last_sender.username %></td>
<td><%= link_to m.subject, show_messages_path(:id => m, :breadcrumb => @box) %></td>
</tr>
<% end %>
<%= submit_tag "discard", :class => 'btn' %>
<% end %>
显示视图
<%= link_to 'Discard', discard_messages_path(@messages), :class => 'btn', :method => 'post' %>
控制器
def discard
conversation = Conversation.find_all_by_id(params[:id])
if conversation
current_user.trash(conversation)
flash[:notice] = "Message sent to trash."
else
conversations = Conversation.find(params[:conversations])
conversations.each { |c| current_user.trash(c) }
flash[:notice] = "Messages sent to trash."
end
redirect_to :back
end
答案 0 :(得分:0)
在你的html中使用[]命名,这些rails将在params中作为数组提供
index.html.erb
<td><%= check_box_tag "message_id[]", m.id %></td>
控制器
# ...
else
conversations = Conversation.where("id IN (?)", params[:message_id][])
# ...
为了进一步简化操作,我会在您的操作中删除条件并创建两个单独的操作
路由
resource :messages do
member do
post 'discard' # /messages/:id/discard
end
collection do
post 'discard_all' # /messages/discard_all?message_id[]=1&message_id[]=22
end
end