开发者

How to pass jquery or javascript data to Ruby on Rails model?

开发者 https://www.devze.com 2023-04-11 21:00 出处:网络
I\'m working on a prototype in Rails 3 and ended up using some jquery (my first time and it\'s pretty sweet). Although, it appears that I am stuck when submitting the data from my view, which is from

I'm working on a prototype in Rails 3 and ended up using some jquery (my first time and it's pretty sweet). Although, it appears that I am stuck when submitting the data from my view, which is from the Impromptu JQuery Survey, and into a my database (sqllite3). Let me explain how the page works:

  • When a drop down item is selected the survey pops up. Also, this is a modal form and there's about 7 questions.

That's pretty much it. My desired outcome is after the user submits the form by clicking finish, each of the answered questions needs to do into the database. Some of the input fields are checkboxes and dropdowns.

Here is a sample of the code, from the last question of the survey:

state7: {
                    html:'Personality Type <div class="field"><select name="personality" id="rate_find"><option value="enfp">ENFP</option><option value="INTJ">INTJ</option><option value="ESTJ">ESTJ</option><option value="INTP">INTP</option></select></div>',
                    buttons: { Back: -1, Cancel: 0, Finish: 1 },
                    focus: 2,
                    submit: function(v, m, f){
                        if (v == 0) 
                            $.prompt.close()
                        else 
                            if (v == 1) 
                                return true; //we're done
                            else 
                                if (v = -1) 
                                    $.prompt.goToState('state6');//go back
                    开发者_运维知识库    return false;
                    }
                }
            }

            $.prompt(temp,{
                callback: function(v,m,f){
                    var str = "You can now process with this given information:<br />";
                    $.each(f,function(i,obj){
                        str += i + " - <em>" + obj + "</em><br />";
                    }); 
                    $.prompt(str)
                }
            });
        }


The key/value pairs containing the information you got through the impromptu popup need to be piled into a POST request like you might expect in a regular form. If you have an ActiveRecord model for the data going into your database, then you could make a form in the view that has the dropdown that triggers the popup, but just make the whole form hidden. Then at the end of your survey you can go through and assign values to the correct input (e.g.

$.each(f,function(i,obj){
     $('input#' + i).val(obj);
});

), and the final 'Finish' button can trigger the submit event on the form. You can look at this question to find out how to check checkboxes with jquery.

Hope that helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消