Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6840311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:49:40+00:00 2026-05-26T23:49:40+00:00

I continue with my newbie questions coming from PHP, there might be more than

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:49:41+00:00Added an answer on May 26, 2026 at 11:49 pm

    Hard to fully answer this question as a little info is missing but hopefully this steers you in the right direction.
    Your search?type=commas url 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:

    def search  
        if params[:type] 
            if params[:type] == "commas"
                ... search logic
            elsif
                ... other search logic
            end 
        end
    end
    

    In Rails the controller method using you naming convention would look more like this;

    def search 
        @actors = Actor.search(params[:type])
    end
    

    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;

    def self.search(type)
        case type
        when "commas"
          Actor.where(:type => "commas")
        when "whatever"
          Actor.where(:type => "whatever")
        else
          Actor.all
        end
    end
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To continue to question further I'm more interested in blogs, websites who once in
So continue from this: Linking how PHP/HTML Please check the answer i accepted, and
A newbie style question, I'm afraid. I was wondering if someone might tell me
I want to continue one task because my head has some problem with a
As I continue to build more and more websites and web applications I am
How to continue from where I have been searching to find the index? I
I would like to continue the discussion from this post: GPX Schema validation problems
Possible Duplicate: Continue Execution Only After .each() Completes This question is actually a continuation
I'm really new at network-programming, so I hope this isn't a complete Newbie-question. I
i continue with this question.Its help and work. regex for special character .My regex

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.