Mau Ruiz
Mau Ruiz

Reputation: 777

Ruby on rails variable AJAX

What I want to do is pass a the value of the fields in the form to the controller so I can make a custom query in the db. I think something is missing but I just can't see what.

This is my controller

 def index

    @contributions = Contribution.all
    @number1 = params[:number1]
    @number2 = params[:number2]
    @itemsok = Contribution.where("first_item_id = ?",@numer1).where("first_item_grade = ?",@numer2) 


    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @contributions }
    end

This is the view

<%= 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("Searcs") %>
<% end %>

And this is the line in the routes.rb

  get 'contribution' => 'contributions#index', :as => 'contribution'   

Thank you very much.

Upvotes: 0

Views: 263

Answers (1)

clyfe
clyfe

Reputation: 23770

Add :remote => true to the form tag to make it submit via ajax, and make sure to respond_to format.js in the controller action.

<%= form_tag(contribution_path, :remote => true) %>

Upvotes: 1

Related Questions