How to pass javascript variables to ruby on rails?

3

What I want to achieve is that when a user clicks on my menu that is on a part of my page this same one appears but on another part of my website.

Function of my menu title

<div id="TituloMenu" >
    <%= image_tag "menu2.png" , :class=>"imagenM2" , :id=>"img" , :onclick=>"menu(1)"%>
    Titulo del Menu
</div>

Here is my menu

<%= yield %>

All this is inside my application.html.erb file, what I would like is to pass a value of application.html.erb to yield, which in this case would be who has the content of the web page

explanation of how it should work.

  

Update

Everything is in the same file as the initial, which would be the application.html.erb, the title, the footnote and the content that is the yield, use the design template, so what I want is to get from the div of the title to the div of the yield that by default comes this way when creating by console, I would like to pass the parameters between both divs through javascript and the function of menu (1) sends a value by clicking on that function to that depending if it is one or zero show or hide the div of the menu that is within yield

    
asked by David 19.12.2016 в 22:26
source

4 answers

1

you can send the data through a hidden to manipulate it in the controller

<%=f.hidden_field :datos, :id => "datos", :value => "" %>

change the value of the hidden at the time you want with jquery

$("datos").attr("value", "nuevo valor");

and you get the value in the controller after the submit

@datos = params[:form]["datos"]
    
answered by 24.04.2017 в 05:45
0

app / assets / javascripts / application.js

function readData() {
    var js_to_rails = 81; //Asignación del valor a la variable
    if (js_to_rails) {
        window.open("localhost:3000//tasks/new?js_to_rails="+js_to_rails,"_self")
    }
}

app / controllers / tasks_controller.rb

  # GET /tasks/new
  def new
    #byebug #para visualizar el valor que tiene params
    @in_rails = params[:js_to_rails]   #Recepción de la variable, y se asigna a una variable de instancia 
    @task = Task.new
  end

app / views / tasks / new.html.erb

<h1>New Task</h1>

<%# Visualizamos el valor enviado desde javascript %>
<%="valor en la vista: " + @in_rails %>

<%= render 'form' %>

<%= link_to 'Back', tasks_path %>

Where you can see the value in the view /new

    
answered by 22.12.2016 в 20:53
0

I would tell you that it is not necessary to pass the parameter. Keep in mind that when the page is completely loaded, a single page is formed with all the content. Rails is responsible for building the full page and what will be built will work with javascript.

The function that reacts to the menu click must exist in the context of the page, preferably in a separate file. In yield, it uses predefined names so that you can manipulate them with certainty and not generate an error.

    
answered by 17.04.2017 в 16:55
0

I think it would be best to use AJAX, for example ... if I want to send data to my controller to the "create" method:

$("#guardar").click(function(){
                // alert("The button was clicked.");
                var campo = document.getElementById('id_campo').value;
                $.ajax({
                    url: "/modelo/create",
                    type: "POST",
                    data: {
                            "campo":campo
                    },
                    dataType: "json",
                    success: function(data) {
                        console.log(data);
                    }
                });
            }
    
answered by 30.08.2017 в 15:34