Mau Ruiz
Mau Ruiz

Reputation: 777

Ajax and Ruby on Rails

Ok so I have this query in my controlller

    @number1 = 3
    @number2 = 6
    @itemsok = Contribution.where("first_item_id = ?",@number1).where("first_item_grade = ?",@number2)

I have also 2 variables wich should make the query "custom"(not right now)... The question is, how can I save a user input into that variables so it can be "dynamic"...

I think that one approach is to create fom with a button like this:

<%= button_to 'All perfect', contribution_path(:id=> contribution.number1),
                    :remote => true %>

But how do I listen for this event in Rails?

I am completely lost. Thank you!

Upvotes: 0

Views: 103

Answers (1)

ootoovak
ootoovak

Reputation: 1063

So if I understand your question you would like the user to be able to enter in values to a form that are then used in the controller to perform a search on the data in your database?

If so you should start with a form in your view, something like:

<%= form_tag(contribution_path, :method => "get") do %>
  <%= label_tag(:number1, "Number 1:") %>
  <%= text_field_tag(:number1) %>
  <%= label_tag(:number1, "Number 2:") %>
  <%= text_field_tag(:number2) %>
  <%= submit_tag("Search") %>
<% end %>

If your routes are set up correctly this will then send the parameters to your controller so you can use them like so:

@number1 = params[:number1]
@number2 = params[:number2]
@itemsok = Contribution.where(:first_item_id => @number1).where(:first_item_grade => @number2)

Upvotes: 1

Related Questions