简体   繁体   English

如何在 Rails 上的 Ruby 中将两个参数从 controller 传递到 model?

[英]How can I pass two params from controller to model in Ruby on Rails?

I want to pass two params that I am introducing in an input en the index view.我想在索引视图中传递我在输入中引入的两个参数。 This is what i have in the index (the style doesn´t matter):这就是我在索引中的内容(风格无关紧要):

<form action="/cars" method="GET">
      <input type="text" name="from" placeholder="From" value="<%= @from %>"/>
      <input type="text" name="to" placeholder="To" value="<%= @to %>"/>
      <input style="color:white; border-radius: 2px; padding: 10px; margin-right: 20px; font-family: 'Montserrat', sans-serif; background-color: black; " type="submit", value="SUBMIT" />
</form>

After this, in the cars_controller I have the following:在此之后,在 cars_controller 我有以下内容:

if params[:from] && params[:to]
      @cars = @cars.filter(params[:from, :to])
end

Finally in my model, i have my function that works with The:from and:to params in a specific way to filter the cars, it doesn´t matter what´s inside.最后在我的 model 中,我有我的 function,它与 The:from 和:to 参数一起以特定的方式过滤汽车,不管里面是什么。 My method is like the following:我的方法如下:

def filter(from, to)
end

So the problem comes when, in the index view, I introduce the:from ant:to params by hand, press teh submit button and it returns me "wrong number of arguments (given 2, expected 1)"所以问题来了,在索引视图中,我介绍了:从 ant:手动到参数,按提交按钮,它返回我“arguments 的编号错误(给定 2,预期 1)”

 @cars = @cars.filter(params[:from] , params[:to])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将参数传递给Rails中的ruby中的控制器动作 - how to pass a params into a controller action in ruby on rails 在Ruby on Rails中,我如何传递params [:id]? - In Ruby on Rails, how can I pass params[:id]? 如何使用Ruby on Rails将数据从控制器传递到模型? - How do you pass data from a controller to a model with Ruby on Rails? Ruby on Rails:如何在模型中创建初始化方法,还是应该在控制器中设置所有默认参数? - Ruby on Rails: how do I create an initializer method in the model or should I set all the default params in a controller? 如何在ajax中传递可以通过Rails中的ruby控制器中的参数访问的属性? - How to pass the attributes in ajax which can be accessed through params in the controller in ruby on rails? 如何将param从控制器传递给序列化器? - How can I pass params from a controller to my serializers? 如何限制我从Ruby on Rails中的控制器传递的数据 - How to limit what data I pass in from the controller in Ruby on Rails Rails如何将params从控制器传递到模型内的after_save - Rails How to pass params from controller to after_save inside model Ruby on Rails:如何遍历参数并将任何空字符串转换为控制器中的nil值? - Ruby on Rails: How can i iterate over params and convert any empty strings into nil values in a controller? 如何在Rails中将其他参数从控制器传递到模型 - How Do I Pass Additional Parameters in Rails from Controller to Model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM