pass values from the view to the controller ruby o

2019-04-15 18:28发布

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条回答
够拽才男人
2楼-- · 2019-04-15 18:43

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 %>
查看更多
登录 后发表回答