简体   繁体   中英

Using javascript in variable in rails

in a model popup receiving a variable as question type id as '$('#subquestion').val()' based on this value the model popup content have to create,

model popup receiving the value but based on this

<%@lg = Language.all %>
<% @qs_type = Question.find( **javascript value** ).question_type %>
<% @lg.each do |n| %>
   if ( @qs_type == 11)  
      matchingtype question two textbox
     <%= text_area %>   
     <%= text_area %>   
   elsif @qs_type == 3 
     mutiple choice one textbox. 
    <%= text_area %>
<% end %>

is there any way to do this ? whether the RJS will help ?

Sorry, but the answer is probably no.

When evaluating a template, Rails will first parse and run any embedded ruby.... the resulting html (and possibly javascript) is then passed back to the client's machine which then interprets the html and then runs the javascript.

So... by the time you get to running the javascript, the ruby has already been run and any results replaced. You can't call a javascript value from the ruby because the javascript doesn't get run until after it's too late for the ruby to use it.

you need to rethink what is being called and when. Another option available to you is to make a new controller action that returns json... and in your template, call that action via AJAX, using the result in your javascript in this template.

But what I think you have above is quite a tangled intertwining of javascript and ruby... which should be simplified a bit more. Separate out what you need from what you are displaying... and give us more information on what you need and we can help you find a solution.

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