ajax从Rails app发布到外部数据库

时间:2017-11-01 20:51:44

标签: ruby-on-rails ruby ajax

我正在尝试将使用Ruby on Rails构建的表单中的数据插入到我的SQL Server数据库中。我已正确连接数据库,可以从中提取数据。我现在正试图回复它。我需要帮助找出如何从表单中的数据到数据库中我的表中的正确列。

我的阿贾克斯:

  <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>

我的控制器功能:

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

目前我只是在这里有一个虚拟插入语句,以确保我可以插入表中,我可以。我只需要知道如何分解发送给控制器的表单值以及如何告诉Rails将这些值放入哪些表和列。

2 个答案:

答案 0 :(得分:1)

Rails将format the data for you。在这样的控制器中:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<modelVersion>4.0.0</modelVersion>
<groupId>com.DH</groupId>
<artifactId>FYP4</artifactId>
<packaging>war</packaging>
<version>1</version>

<properties>
    <springframework.version>4.0.6.RELEASE</springframework.version>
    <hibernate.version>4.3.6.Final</hibernate.version>
    <mysql.version>5.1.31</mysql.version>
    <joda-time.version>2.3</joda-time.version>
    <testng.version>6.9.4</testng.version>
    <mockito.version>1.10.19</mockito.version>
    <h2.version>1.4.187</h2.version>
    <dbunit.version>2.2</dbunit.version>
</properties>

<dependencies>
    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${springframework.version}</version>
    </dependency>

    <!-- Hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

    <!-- jsr303 validation -->
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.1.3.Final</version>
    </dependency>

    <!-- MySQL -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
    </dependency>

    <!-- Joda-Time -->      
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>${joda-time.version}</version>
    </dependency>

    <!-- To map JodaTime with database type -->         
    <dependency>
        <groupId>org.jadira.usertype</groupId>
        <artifactId>usertype.core</artifactId>
        <version>3.0.0.CR1</version>
    </dependency>

    <!-- Servlet+JSP+JSTL -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.1</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>


    <!-- Testing dependencies -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${springframework.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>${testng.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>${mockito.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>${h2.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>dbunit</groupId>
        <artifactId>dbunit</artifactId>
        <version>${dbunit.version}</version>
        <scope>test</scope>
    </dependency>

</dependencies>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <warSourceDirectory>src/main/webapp</warSourceDirectory>
                    <warName>FYP4</warName>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
    <finalName>FYP4</finalName>
</build>

可以通过{'Report_Name': 'some name', 'Report_link': 'www.example.com'} 访问。

您现在的工作是正确格式化数据以手动执行SQL查询。

params

答案 1 :(得分:0)

对于要添加到数据库服务器的表的问题,您可以在表单的隐藏字段中指定它,并且每个表都应该有一个名称,当您说$form.serialize();时,它会将其转换为{{1}等等FirstName=Amr&SecondName=Adel是字段的名称,FirstName是字段的值,然后将此序列化放入JSON格式的形式,如Amr并添加{ {1}}在您的帖子请求中,在{"data": $form.serialize()}函数中,您可以通过dataType: "JSON"将其拆分并将其与Insert拆分为params[:data],并将每个元素拆分为&使用['FirstName=Amr','SecondName=Adel'],您可以获得类似=的内容。

希望这有帮助。