Rails link_to 或 button_to 发布带有参数的请求 [英] Rails link_to or button_to post request with parameters

查看:24
本文介绍了Rails link_to 或 button_to 发布带有参数的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个安静的(脚手架生成的)控制器和投票模型,我只是想在视图中添加一个字段,当点击时,将为给定的汽车创建一个新的投票.它需要是一个 POST,我应该在 Rails 中执行此操作的方式是什么?

I have a restful (scaffold generated) controller and model for Votes, I am simply trying to add a field into the view that when clicked, will create a new vote for a given car. It needs to be a POST, What's the way I should be doing this in rails?

<% @cars.each do |car| %>
  <tr>
    <td><%= button_to '+', {:controller => "votes", :action => "create"}, :car_id => car.id, :user_id=> session[:user_id] , :method=>:post  %></td>
    <td><%= car.votes.count %></td>
    <td><%= car.name %></td>
    <td><%= car.code %></td>
    <td><%= car.album %></td>
<% end %>

推荐答案

<td><%= button_to '+', {:controller => "votes", :action => "create", :car_id => car.id, :user_id=> session[:user_id]} , :method=>:post  %></td>

这将使 params[:car_id] 和 params[:user_id] 在 VotesController 创建操作中可用.

This will make params[:car_id] and params[:user_id] available in VotesController create action.

这篇关于Rails link_to 或 button_to 发布带有参数的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆