获取 ArgumentError 参数数量错误(0 代表 1)
当我运行我的应用程序时,出现以下错误:
ArgumentError in StatesController#filter
wrong number of arguments (0 for 1)
我的控制器看起来像这样:
class StatesController < ApplicationController
def filter(my_string)
@new_array = []
@new_array = state.each {|x| if /#my_string/i =~ x then puts x end}
return @new_array
end
end
我的视图看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>States</title>
</head>
<body>
<% States.filter(params[:substring]) %>
<p><%= @new_array.each %></p>
<% if @new_array.size = 0 %>
<p>No matches!</p>
</body>
</html>
我的代码有什么问题?
非常感谢
When I run my application I am getting the following error:
ArgumentError in StatesController#filter
wrong number of arguments (0 for 1)
My controller looks like this:
class StatesController < ApplicationController
def filter(my_string)
@new_array = []
@new_array = state.each {|x| if /#my_string/i =~ x then puts x end}
return @new_array
end
end
And my view looks like this:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>States</title>
</head>
<body>
<% States.filter(params[:substring]) %>
<p><%= @new_array.each %></p>
<% if @new_array.size = 0 %>
<p>No matches!</p>
</body>
</html>
What wrong with my code?
thanks so much
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
StatesController 中的过滤器方法是一个操作。此类操作不接受参数。实现您想要做的事情的更好方法是创建一个名为 State 的模型,如下所示。并向其中添加过滤器方法:
在您的控制器中,您现在应该具有如下所示的内容:
然后在相应的视图中:
它的作用是使用模型将数组加载到实例变量中。该实例变量(以 @ 开头)可在控制器操作中呈现的视图中访问。我们正在使用的操作被指定为过滤器。
在视图中,我使用empty? 检查数组中是否有任何结果。方法。否则我只是循环遍历数组。
现在,只有在标题中有 params[:sub_string] 时,这才有效。因此,您需要确保网址中包含类似
?sub_string=mystring
的内容。鉴于您的问题和代码布局,我假设您对 Rails 相当陌生。我真的建议仔细阅读这里的入门指南:http://guides.rubyonrails.org/getting_started .html
The filter method you have in the StatesController is an action. Such actions do not accept parameters. A better way to achieve what you are trying to do would be to create a model called State like so. And add the filter method to it:
In your controller, you should now have something like the following:
Then in the corresponding view:
What this does is uses the model to load an array into an instance variable. This instance variable (started with the @) is accessible in the view that is rendered in the controller action. The action we are using is specified as filter.
In the view, I check if there are any results in the array by using the empty? method. Otherwise i just loop through the array.
Now, this will only work provided you have the params[:sub_string] in the headers. So you will need to make sure you have something like
?sub_string=mystring
in the url.Given your question, and your code layout, I would assume you are fairly new to rails. I really recommend having a good read through the getting started guide here: http://guides.rubyonrails.org/getting_started.html