获取 ArgumentError 参数数量错误(0 代表 1)

发布于 2024-12-19 18:44:16 字数 1024 浏览 0 评论 0原文

当我运行我的应用程序时,出现以下错误:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星星的軌跡 2024-12-26 18:44:16

StatesController 中的过滤器方法是一个操作。此类操作不接受参数。实现您想要做的事情的更好方法是创建一个名为 State 的模型,如下所示。并向其中添加过滤器方法:

# app/models/state.rb
class State
  def filter(params[:my_string])
    @new_array = []
    @new_array = state.each {|x| if /#my_string/i =~ x then puts x end}
    return @new_array
  end
end

在您的控制器中,您现在应该具有如下所示的内容:

class StatesController < ApplicationController
  def filter
    @states = State.filter(params[:sub_string])
  end
end

然后在相应的视图中:

<% if @states.empty? %>
  <p>No matches!</p>
<% else %>
  <p>
    <% @states.each do |state| %>
      <%= state %>
    <% end %>
  </p>
<% end %>

它的作用是使用模型将数组加载到实例变量中。该实例变量(以 @ 开头)可在控制器操作中呈现的视图中访问。我们正在使用的操作被指定为过滤器。

在视图中,我使用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:

# app/models/state.rb
class State
  def filter(params[:my_string])
    @new_array = []
    @new_array = state.each {|x| if /#my_string/i =~ x then puts x end}
    return @new_array
  end
end

In your controller, you should now have something like the following:

class StatesController < ApplicationController
  def filter
    @states = State.filter(params[:sub_string])
  end
end

Then in the corresponding view:

<% if @states.empty? %>
  <p>No matches!</p>
<% else %>
  <p>
    <% @states.each do |state| %>
      <%= state %>
    <% end %>
  </p>
<% end %>

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文