简体   繁体   中英

How do I pass a json value into a rails render partial

Ok...I am banging my head into hamburger. Is this possible? Am I doing life right?

I have a value called "myValue.id" in a javascript function and I need to pass this value as a variable into a rails partial "new" but I do not know how. This is an old rails 2.3 app any help is appreciated... here is what this looks like....

      eventClick: function(myValue, jsEvent, view) {
                        console.log("eventClick function called");
                        console.log("myValue.id "+myValue.id);
                        jQuery('#remote_container').html('<%= escape_javascript(render :partial=>"new", :locals=>??????) %>');
                        jQuery('#new_event').modal('show');
                    }

Just try to use input text value property to pass your javascript variable to the controller. Firstly just make simple form with hidden input property that have specific id in your index.html . Next create button that will send the variable to

<form id "myForm" action="/welcome/index">
  <input type="hidden" id="myVar" name="varParams">
</form>
<button id="btnSend">SEND</button>

Then, create javascript function to set the value of the input text with our required variable when the button is clicked. Just put below code in your index.html .

<script>
document.getElementById('btnSend').addEventListener('click', function () {
  document.getElementById("myVar").value = "myValue";
  document.getElementById("myForm").submit();
});
</script>

Next, just retrieve the myVar params in your welcome_controller.rb .

  def index
    myVar = params[:varParams]
    # Do something with myVar variable  
  end

Just modify the code provided to fulfill your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM