I continue with my newbie questions coming from PHP, there might be more than one question below, thanks for your patience.
What I’m trying to accomplish:
I have a template (view) called search.html.erb where I want different methods to be called depending on user input, specifically get variables passed by a link in a previous page. So for exmample if users click on the “commas” link it would pass something like
search?type=commas
(I’ve substituted action for type because I understand ‘action’ is reserved in Rails for methods declared inside the Controller). The route file has this:
match ':controller/:action/:type/'
Which is supposed to allow me to do “http://localhost:3000/actors/search/commas as a way to pass the ‘commas’ type?
Then the search page would call the showCommas method from the Actors Model:
def self.showCommas
@commas = self.where("actor LIKE ?", "%,%,%")
end
If they clicked on “acute” it would pass
search?type=acute
And the page would call the acute method in the Actors Model.
Finally I also want them to be able to pass a search term and the search page would do a SQL query with that term using .find_by_sql.
This is trivial to implement in PHP, I just use a
if ($_GET[action] == 'commas') { ...
But how to do it in Rails? Problems I have:
If I create a method in the Actor controller for a search term:
def search(term)
Actor.find_by_sql([sql query here with '%#{term}%'])
end
Rails will complain that I have too many arguments for a method in the controller. So I’ve moved all my methods to the Model (guessing this is the right way to do it then?)
Putting this into my search.html.erb page:
if type.nil?
criteria = "No type specified"
elsif type == 'commas'
criteria = type
@actors = Actor.showCommas
end
Gives me an error immediately about the type variable not being defined: undefined local variable or method `type’
So I’ve made a grand mess of things, but as great as Rails is, coming from PHP it’s quite hard to wrap your head around the Rails way of doing things. I understand the primary philosophy of having a view with the same name as your action and Rails will call it, but I don’t want to build one page for every query (or other little thing) I need to do. Furthermore, the books I’ve read don’t seem to go into detail about this, so, again, thanks for your help and indulgence in asking multiple questions in one post.
Hard to fully answer this question as a little info is missing but hopefully this steers you in the right direction.
Your
search?type=commasurl will route to the actors controller where by the looks of it you have a search method. The type=commas will be accessible in the params hash (params[:type]).In rails its best practice to move as much logic into the model as you can, so whereas coming from php it would feel natural to do something like this in you controller:
In Rails the controller method using you naming convention would look more like this;
Note the
params[:type]to access your type url parameter. The way you had written it you had type as a controller method argument which is incorrect as controller methods do not have arguments.And the model could look something like this;
Your search method will obviously be much different but that is just and example of how to pass params from url right through to the model.