简体   繁体   English

ajax 从 Rails 应用程序发布到外部数据库

[英]ajax post to external database from Rails app

I am trying to insert data from a form built with Ruby on Rails to my SQL Server database.我正在尝试将数据从使用 Ruby on Rails 构建的表单插入到我的 SQL Server 数据库中。 I have connected my database correctly and can pull data from it.我已经正确连接了我的数据库并且可以从中提取数据。 I am now trying to post back to it.我现在正在尝试回帖。 I am needing help with figuring out how to get the data from the form into the correct columns in my table in the database.我需要帮助弄清楚如何将表单中的数据放入数据库表中的正确列中。

My ajax:我的阿贾克斯:

  <script>
$('#Favorites').on('submit',function(event){
    var $form = $(this),
    data = $form.serialize();
    var url = 'http://localhost:3000/welcome/insert';
    $.ajax({
        type: 'POST',
        url: url,
        data: data,
        success: function(){
            
            alert('Your form has been successfully submitted!');
            document.getElementById('Favorites').reset();
            window.location.reload();
        },
        fail:function(){
            alert('something went wrong...try again');
        }
    });
 return false;
 });

</script>

My controller function:我的控制器功能:

def insert
    @ipinsert=Ipdw.connection.execute("insert into [DB_Test02].[dbo].[My_Table] (Report_Name,Report_Link)
  values ('I am cool','www.google.com')")
  end

Currently I just have a dummy insert statement here to make sure that I can insert into the tables and I can.目前,我在这里只有一个虚拟的插入语句,以确保我可以插入到表中,我可以。 I just need to know how to break out the form values sent to the controller and how to tell Rails what table and columns to put those values into.我只需要知道如何分解发送到控制器的表单值,以及如何告诉 Rails 将这些值放入哪些表和列。

Rails will format the data for you . Rails 将为您格式化数据 In controller like this:在这样的控制器中:

{'Report_Name': 'some name', 'Report_link': 'www.example.com'}

and will be accessible via the params .并且可以通过params访问。

Your job is now to format the data correctly for the manual execution of the SQL query.您现在的工作是为手动执行 SQL 查询正确格式化数据。

insert_values = "('%s', '%s')" % params['Report_Name'], params['Report_link']
@ipinsert=Ipdw.connection.execute("insert into [DB_Test02].[dbo].[My_Table] (Report_Name,Report_Link) values #{insert_values}")

For the problem of which table to add to your DB server you could specify this in hidden fields in your form and every fieled should have a name, When you say $form.serialize();对于将哪个表添加到数据库服务器的问题,您可以在表单的隐藏字段中指定它,并且每个字段都应该有一个名称,当您说$form.serialize(); it turns it to something like FirstName=Amr&SecondName=Adel and so on where FirstName is the name of the field and Amr is the value of the field, Then you put this serialization into a form of JSON format like {"data": $form.serialize()} and add dataType: "JSON" to your post request, In your Insert function you can get it through params[:data] and split it with & to be something like ['FirstName=Amr','SecondName=Adel'] and every element split it with = so you can get something like this [['FirstName','Amr'], ['SecondName','Adel']] .它将它变成类似FirstName=Amr&SecondName=Adel等等,其中FirstName是字段的名称, Amr是字段的值,然后您将此序列化为 JSON 格式,如{"data": $form.serialize()}并将dataType: "JSON"添加到您的帖子请求中,在您的Insert函数中,您可以通过params[:data]获取它并用&将其拆分为['FirstName=Amr','SecondName=Adel']并且每个元素都用=分割它,这样你就可以得到这样的[['FirstName','Amr'], ['SecondName','Adel']]

Hope this helps.希望这可以帮助。

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

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