[英]Send search results to json with rails
I am currently working on a simple search feature on a Rails app. 我目前正在使用Rails应用程序上的简单搜索功能。 I'm using Vue.js, so I'm feeding everything to the frontend using Rails' JSON builder.
我正在使用Vue.js,因此我将使用Rails的JSON构建器将所有内容馈送到前端。 For the search I have the controller and model setup as so:
对于搜索,我具有如下控制器和模型设置:
The controller: 控制器:
def index
if params[:search]
@strains = Strain.search(params[:search]).order("created_at DESC")
else
@strains = Strain.all.order("created_at DESC")
end
respond_to do |format|
format.html
format.json { render :json => @strains }
end
end
The model: 该模型:
def self.search(search)
where('title LIKE ?', '%#{search}%')
end
The view: 风景:
<%= form_tag(strains_path, :method => "get") do %>
<%= text_field_tag :search, params[:search], placeholder: "Search Strains" %>
<%= submit_tag "Search", :name => nil %>
<% end %>
When I type in the search, I see it update the params in the URL, but nothing happens to the output on the page. 当我输入搜索内容时,我看到它更新了URL中的参数,但是页面上的输出没有任何反应。 I'm thinking it has something to do with the JSON.
我在想它与JSON有关。 This is my ready function inside the Vue component.
这是我在Vue组件中准备好的功能。
ready: function() {
var that;
that = this;
$.ajax({
url: '/strains.json',
success: function(res){
that.strains = res;
}
});
}
I'm watching the log and it seems like it will render the search for HTML, but not JSON. 我正在查看日志,似乎它将呈现HTML而不是JSON的搜索。
Here is the log: 这是日志:
Started GET "/strains.json" for ::1 at 2016-05-23 21:40:32 -0400
Processing by StrainsController#index as JSON
Strain Load (0.2ms) SELECT "strains".* FROM "strains" ORDER BY created_at DESC
Completed 200 OK in 2ms (Views: 1.8ms | ActiveRecord: 0.2ms)
Started GET "/strains?utf8=%E2%9C%93&search=Test" for ::1 at 2016-05-23 21:41:07 -0400
Processing by StrainsController#index as HTML
Parameters: {"utf8"=>"✓", "search"=>"Test"}
Strain Load (0.2ms) SELECT "strains".* FROM "strains" WHERE (title LIKE '%Test%') ORDER BY created_at DESC
Rendered strains/index.html.erb within layouts/application (1.5ms)
Completed 200 OK in 17ms (Views: 16.3ms | ActiveRecord: 0.2ms)
Started GET "/strains.json" for ::1 at 2016-05-23 21:41:07 -0400
Processing by StrainsController#index as JSON
Strain Load (0.1ms) SELECT "strains".* FROM "strains" ORDER BY created_at DESC
Completed 200 OK in 2ms (Views: 1.4ms | ActiveRecord: 0.1ms)
I'm not sure if it is your main issue but it looks your where clause is using single quotes, thus is not picking up that variable. 我不确定这是否是您的主要问题,但它看起来您的where子句使用的是单引号,因此没有选择该变量。 You need to use double quotes if you want to use ruby string interpolation.
如果要使用红宝石字符串插值,则需要使用双引号。
irb(main):006:0> foo = 5
=> 5
irb(main):007:0> 'this is my number #{foo}'
=> "this is my number \#{foo}"
irb(main):008:0> "this is my number #{foo}"
=> "this is my number 5"
So you want: 所以你要:
def self.search(search)
where('title LIKE ?', "%#{search}%")
end
You should set the dataType
option to expect JSON data for the return value. 您应该将
dataType
选项设置为期望JSON数据作为返回值。 You can do it like this: 您可以这样做:
$.ajax({
url: '/strains.json',
dataType: 'json',
success: function(res){
that.strains = res;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.