Get a submitted value with AJAX (Ruby on Rails)

Ananas

I have this code in my view

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

  <%= submit_tag("OK") %>
<% end %>
</form>

And this in my controller

  def index
    @portefeuillemodeles = Portefeuillemodele.all
    @date = params[:date]

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

When I press the submit button, it doesn't do anything. And I have ajax gem installed, what do I need to just save the submitted value , so I can use it in my view ?

Mandeep

Looking at your code

<form> // you don't need this tag form_tag will create those tags for you
  <%= form_tag(portefeuillemodele_path, :remote => true) do %>
    <%= label_tag(:date, "The selected date is:") %>
    <%= text_field_tag(:date) %>

    <%= submit_tag %>
  <% end %>
</form>

Secondly when you are using ajax you need js format in your controller

def index
  @portefeuillemodeles = Portefeuillemodele.all
  @date = params[:date] # this will give you access to your date but you need to check your params to be sure

  respond_to do |format|
    format.html # index.html.erb
    format.js{} # ajax will call this format not html or json
    format.json { render json: @portefeuillemodeles }
  end
end

This will allow rails to look for a file named index.js.erb where you can manipulate your DOM elements by js. For details refer to Working with Javascript in Rails

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get a submitted value with AJAX (Ruby on Rails)

From Dev

Ruby on Rails: Getting the value from a select form field before it's submitted

From Dev

get the object value using ajax in rails

From Dev

Rails: In a controller, add a value to the params submitted by a form

From Dev

AJAX get request does not recognize submitted data?

From Dev

Ruby on Rails cannot Send Get Request with CoffeeScript and AJAX

From Dev

Get data back after validating a Coupon with Ruby on Rails using AJAX

From Dev

Get JSON contents from Ruby on Rails server using AJAX

From Dev

Can't get JSON from Ruby on Rails Ajax call

From Dev

Ruby on Rails and Ajax

From Dev

Ruby on Rails and Ajax

From Dev

Get value of a submitted form without submit button

From Dev

How to get a specific value from url in ruby on rails

From Dev

Rails 4.2, AJAX form is submitted exponentially to the server on each subsequent triggering

From Dev

Ruby on Rails get route

From Dev

Ruby on Rails searching through ajax

From Dev

Like button Ajax in Ruby on Rails

From Dev

ajax closest is not affected in ruby on rails

From Dev

Like button Ajax in Ruby on Rails

From Dev

Get request object, when JSP page is submitted using AJAX

From Dev

How to get the submitted value of a form in a Django class-based view?

From Dev

Field value of integer doesn't get submitted (Simple form)

From Dev

How get Node.JS fields value (submitted form data)

From Dev

Ruby on Rails: Passing a value to the model

From Dev

value not stored in array ruby on rails

From Dev

Ruby on Rails: Passing a value to the model

From Dev

Ruby on rails get mac address

From Dev

Get hostname of URL in ruby on rails

From Dev

Ruby on rails get mac address

Related Related

HotTag

Archive