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 8803011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:13:42+00:00 2026-06-14T01:13:42+00:00

This is my first attempt at trying to implement a simple search form in

  • 0

This is my first attempt at trying to implement a simple search form in my rails app, New to rails so forgive me if I have made some major mistakes

I have a search form that looks like this

  <%= form_tag({:controller => 'search', :action => 'search'}, {:method => 'get'})  do |s| %>
  <%= text_field_tag :search, params[:search] %>
   <%= submit_tag "Search" %>
  <% end %>

My controller

def search

@countrysearch = Recipe.find(params[:search])
end

For now i am outputting my results like so

  <h4>output</h4>
  <%= @countrysearch %>

I am getting an output but if i search for a word like Wales, i get an error saying could not find by id, but if i search for an id like 17 then i get

  #<Recipe:0x97b0fa8> 

I have looked into the find_by_sql method but my understanding of this is not there yet, im unsre of the syntax to search dependent on the string I enter, though I guess that doesn’t matter as the params are the content of the search field?

If any can help then it would be much appreciated

Thanks

  • 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-06-14T01:13:44+00:00Added an answer on June 14, 2026 at 1:13 am

    The model find method (Recipe.find()) expects the numeric id of the instance (a specific recipe). But it looks like you’re searching by country name. There are lots of ways to do this in Rails. First, however, I assume that your recipe model has an attribute like “country” whose values are like “Wales”.

    If so, then you could make your search method like this:

    def search
      @countrysearch = Recipe.find_by_country(params[:search])
    end
    

    Or alternatively you could use the new approach in Rails 3

    def search
      @countrysearch = Recipe.where(:country => params[:search])
    end
    

    Don’t use find_by_sql — it will work, and it’s not bad, but then you’re missing some of the main power of Rails.

    There are a couple of other things worth mentioning:

    • You’ll get an error in the view if the search returns no results. Before referencing the @countrysearch instance variable, test for nil using if @countrysearch.nil? or for the positive case if @countrysearch.present?
    • Chances are there will be more than one recipe from Wales, so expect not an instance of Recipe back, but a collection of them — in your view, iterate using
      <% @countrysearch.each do |recipe| %>
      <%= recipe.name %>
      …
      <% end %>
    • Similarly, if you use the finder method you should go with Recipe.find_all_by_country
    • Storing the country name in the Recipe model is not a great practice. Create a model containing countries.
    • You probably want a case-insensitive search (“Wales” != “wales”)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a simple threading procedure (granted this is my first attempt
This is my first attempt at responsive design, so I'm keeping it simple. I
Up front: This is my first attempt at an Android app. I'm in that
I have run into trouble trying to implement functionality for serializing some classes in
This is my first attempt at an Android app. I'm targeting Android 4/Ice Cream
I'm trying to implement a custom classloader for tomcat. My first attempt yielded a
This is my first attempt to write shorthand if statements however am befuddled by
this if my first attempt at using streaming for WCF, and I am struggling
This is my first attempt to create a GUI in MATLAB. I haven't been
this is my first attempt at a responsive design so excuse me if this

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.