1

I really don't get how to use Ajax with Ruby on Rails. I must be missing something simple.

What I want to do is to ask the user to select a date, and then make a table of documents appear, but only with the selected date (Date is an attribute of Document).

My idea is to create a local variable witch is not in my database, store the selected date in it, and then create a loop in my view saying for example @document.where(:date = date).each...

In app/controllers/documents_controller.rb, I have :

class DocumentsController < ApplicationController

 def information
    @documents = Document.all
    @date = params[:date]

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

end

And in app/views/documents/_information.js.erb, I have:

<%= form_tag(document, :remote => true) do %>
  <%= label_tag(:date, "The selected date is:") %>
  <%= text_field_tag(:date) %>

  <%= submit_tag %>

<% end %>

In the end, I have a field where the user puts his date, but the submit button doesn't do anything. What do I miss ?

10
  • 1
    add a onchange event using jQuery on the text_field which send a ajax request with the selected date. In the method where the ajax is received, collect all the documents related to the date. In the js.erb file, replace a html element on the current page with a partial which iterates over the documents. Ajax isnt hard. Commented Jul 8, 2014 at 10:20
  • would you mind showing how i should implement the onchange event and the js.erb file ? Commented Jul 8, 2014 at 12:45
  • @Ananas field where you are selecting your date is in a form? and your partial should be _information.html.erb not js.erb Commented Jul 8, 2014 at 14:55
  • Yes my partial is _information.html.erb, and the field where I select my data is in this very partial. Commented Jul 8, 2014 at 15:01
  • @Ananas what is the problem you are facing? In your previous question also i explained details of using ajax Commented Jul 8, 2014 at 15:16

1 Answer 1

1

As discussed you need to change the flow of your app.Lets go through steps one by one

a. create your input field where you are selecting your date field. You already have your form for that

<%= form_tag(you_path_for_information_method, :remote => true) do %>
  <%= label_tag(:date, "The selected date is:") %>
  <%= text_field_tag(:date) %>
  <%= label_tag(:portfolio, "Add portfolio") %>
  <%= text_field_tag(:portfolio) %>
  <%= submit_tag %>

<% end %>

In controller

def information
  @documents = Document.all
  @date = params[:date]
  @portfolio = Portfolio.find(params[:portfolio])
  respond_to do |format|
    format.js{}
  end
end

In your information.js.erb you can have:

$("#some_id_of_parent").html("<%=j render partial: "your_partial", locals: {portfolio: @portfolio} %>")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.