Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 运行带有变量的sql查询的Rails_Javascript_Mysql_Ruby On Rails_Ruby - Fatal编程技术网

Javascript 运行带有变量的sql查询的Rails

Javascript 运行带有变量的sql查询的Rails,javascript,mysql,ruby-on-rails,ruby,Javascript,Mysql,Ruby On Rails,Ruby,可以在rails中使用用户输入的变量运行sql查询吗 我正试图通过数据库搜索他们正在寻找的具有某些特征的餐厅(即菜肴、分数、zipcode位置)。这里是我的html.erb作为参考 <form id="frm1" action="form_action.asp"> Name: <input type="text" name="name" value="Antico Pizza"><br> Lower Score: <input type=inte

可以在rails中使用用户输入的变量运行sql查询吗

我正试图通过数据库搜索他们正在寻找的具有某些特征的餐厅(即菜肴、分数、zipcode位置)。这里是我的html.erb作为参考

<form id="frm1" action="form_action.asp">
  Name: <input type="text" name="name" value="Antico Pizza"><br>
  Lower Score: <input type=integer name="LowerScore" value=0>
  Higher Score: <input type="integer" name="HigherScore" value=100><br>
  Zipcode: <input type=integer name="Zipcode" value=30332><br>
            <label for="Cuisine">Cuisine: </label>
            <select name="Cuisine" id="Cuisine">
            <%= @my_cuisines.each do|cuisine|%>
                    <option value=<%= cuisine.cuisine %> onclick="getCuisine()"><%= cuisine.cuisine %></option>
              <% end %>
            </select>
</form> 

<button onclick="myFunction()">Search!</button>

<p id="demo"></p>
<script>
    function myFunction() {
        var x = document.getElementById("frm1");
}

#{x}
是用户变量(即c=courine,l=lowerScore…)。在rails中有这样做的方法吗?

当然有!您可以使用ActiveRecord的查询接口(Arel)

请注意,您需要在餐厅和检查模型之间建立关联

class Restaurant < ActiveRecord::Base
  has_many :inspections, foreign_key: 'rid'
end

class Inspection < ActiveRecord::Base
  belongs_to :restaurant, foreign_key: 'rid'
end
class餐厅
我建议您阅读有关以下主题的Rails指南:


另外,无论您做什么,在没有使用ActiveRecord接口的情况下,不要在SQL查询中使用来自用户的输入。您将面临SQL注入攻击。看

谢天谢地!我担心这件事做得不干净。我现在正在阅读指南,但我想知道用户的输入将如何存储?如何将html.erb页面的用户输入推入变量?这里也回答了这个问题:如果答案对您有效,您介意接受它吗?
Restaurant.joins(:inspection).where(cuisine: c, zipcode: z, totalscore: l..h)
class Restaurant < ActiveRecord::Base
  has_many :inspections, foreign_key: 'rid'
end

class Inspection < ActiveRecord::Base
  belongs_to :restaurant, foreign_key: 'rid'
end