views:

24

answers:

1

I'm very new to rails so hopefully this should be a quick fix. I'm writing an application that searches a database and reloads the page displaying the desired results. In rails how does one save a input into a text_field and post it so that it can be retrieved and used in the query for retrieving data.

My view:

    <title></title>
</head>

<body>

    Search Collection <br>
     <%= text_field "person", "name" %>
     <%= select_tag(:search_id, '<option value="0">Search by</option><option        value="1">Make</option><option value="2">Condition</option>
                                 <option value="3">Sport</option>') %> 
      <%= link_to 'New catalog', new_catalog_path %>
    <br>
    <br>
     <%= link_to "Search", :search_text => , :action => :index %> <br>
    <br>


    <h1>Results</h1>
    <%= will_paginate @catalogs %>

    <table border="1">
        <tr>
            <th>Catalog id</th>

            <th>Year</th>

            <th>Make</th>

            <th>Card number</th>

            <th>Number</th>

            <th>Condition</th>

            <th>Sport</th>

            <th>Tracking list</th>
        </tr>
        <% for catalog in @catalogs %>

        <tr>
            <td><%= catalog.Catalog_ID %></td>

            <td><%= catalog.Year %></td>

            <td><%= catalog.Make %></td>

            <td><%= catalog.Card_Number %></td>

            <td><%= catalog.Number %></td>

            <td><%= catalog.Condition %></td>

            <td><%= catalog.Sport %></td>

            <td><%= catalog.Tracking_List %></td>

            <td><%= link_to 'Show', catalog %></td>

            <td>
            <%= link_to 'Edit', edit_catalog_path(catalog) %></td>

            <td>
            <%= link_to 'Destroy', catalog, :confirm => 'Are you sure?', :method => :delete %></td>
        </tr>
        <% end %>
    </table>
    <br>

</body>

My Controller method

def index
@search_text = 'T206 White Border'

@catalogs = Catalog.paginate_by_sql ['select * from catalogs where Make =\''+   @search_text+'\'' , 80000], :page => params[:page]


end

Be gentle if its an easy fix, I'm still getting used to the whole MVC thing

+1  A: 

Your question has a lot going on, so let's try to sort through it one piece at a time. First, I'll assume your database has a table called catalogs with a column called make, and that you're using the will_paginate plugin. It looks like you got started by copying and modifying some examples straight from the docs. First, your controller - you don't need the more complex paginate_by_sql, you can use the simpler paginate.

controller:

def index
  @catalogs = Catalog.paginate(:all, :conditions => {:make => params[:search]}, :page => params[:page])
end

Now your view, just the stuff relevant to the search:

<% form_tag catalogs_path, :method => :get do %>
  <%= text_field_tag 'search' %>
  <%= submit_tag 'submit search' %>
<% end %>

And that's it. Good luck!

Jaime Bellmyer
This has solved so many problems haha Thanks a ton. You are wise
Program This
Thanks, glad it helped :) Can you accept my answer, so I get credit for it?
Jaime Bellmyer
Hi, it looks like you're new to stackoverflow. By accepting my answer, I get credit for helping you. If you don't accept good answers from people who help you, you'll eventually stop getting help. Every time you ask a question, it displays your acceptance percentage so people know if you accept answers or not.
Jaime Bellmyer