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
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=mystringin 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