pass values from the view to the controller ruby on rails

余生长醉 提交于 2020-01-13 06:44:18

问题


This is my controller:

def addcar
    @car = Car.new(params[:car])    
    render :action => "list"  
end

this is my view:

<%(@allcars).each do |cell|%>
    <p><%= link_to cell.to_s, :controller => "car", :action => "addcar", :car => cell.to_s %></p>
<%end %>

In the link_to statement I want to pass cell.to_s to the controller. How can I do that please? The cell.to_s is just a string but I want it to be the name of the car object (car.Name)


回答1:


Car.new(params[:car]) expects params[:car] to be a Hash ({:foo => "bar"}). So change your code:

<% @allcars.each do |cell| %>
  <p>
    <%= link_to cell.to_s,
          :controller => "car",
          :action => "addcar",
          :car => { :name => cell.to_s } %>
  </p>
<% end %>


来源:https://stackoverflow.com/questions/1816801/pass-values-from-the-view-to-the-controller-ruby-on-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!