简体   繁体   English

如何将jquery或javascript数据传递给Ruby on Rails模型?

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

I'm working on a prototype in Rails 3 and ended up using some jquery (my first time and it's pretty sweet). 我正在用Rails 3开发一个原型,最终使用了一些jquery(这是我的第一次,这很不错)。 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). 虽然,从我的视图(即Impromptu JQuery Survey)向我的数据库(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. 另外,这是一个模态形式,大约有7个问题。

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 . 包含您通过即兴弹出窗口获取的信息的键/值对需要像常规形式那样被堆积到POST请求中。 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. 如果您有一个ActiveRecord模型来存储进入数据库的数据,则可以在视图中创建一个窗体,该窗体具有触发弹出式窗口的下拉菜单,而只是隐藏整个窗体。 Then at the end of your survey you can go through and assign values to the correct input (eg 然后,在调查结束时,您可以遍历并将值分配给正确的输入(例如,

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

), and the final 'Finish' button can trigger the submit event on the form. ),最后的“完成”按钮可以触发表单上的Submit事件 You can look at this question to find out how to check checkboxes with jquery. 您可以查看此问题,以了解如何使用jquery选中复选框。

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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