Ruby on rails 为什么Rails应用程序的ajax不起作用?
1.Ruby on rails 为什么Rails应用程序的ajax不起作用?,ruby-on-rails,Ruby On Rails,1.welcome/index.html.erb的代码如下: <%= form_tag('search', method:"get",remote:true) do %> <%= label_tag(:q, "Search for:") %> <%= text_field_tag(:q) %> <%= submit_tag("Search") %> <% end %> <div id="results">
welcome/index.html.erb的代码如下:
<%= form_tag('search', method:"get",remote:true) do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %>
<div id="results">
<%= render 'searchresults' %>
</div>
<table class="table">
<tr>
<th>Title</th>
<th>Description</th>
<th></th>
</tr>
<% @articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.description %></td>
<td><%= link_to 'Show', article_path(article) %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
三,。WelcomeController的此代码
:
class WelcomeController < ApplicationController
def index
@articles = Article.all
return @articles
end
def search
@query = params[:q]
@articles = Article.where('name LIKE ?', "%#{@query}%")
respond_to do |format|
format.html { redirect_to @articles }
format.js
end
end
end
search.js.erb的代码:
$(“#结果”).html(“”)
结果
为什么Rails应用程序的ajax不起作用?
谢谢大家。
我正在使用rails 5.2和ruby 2.5。请在控制台中包含错误堆栈。@jvilian:只有在查看调试时才会出现错误。它没有进入错误页面。您的控制台必须显示错误。您在页面上看不到它,因为您使用的是JS。@DT请发布您在rails服务器上显示的错误。(错误堆栈)。很抱歉,我有编辑问题,第一个错误,我有修复。
<div id="results">
<%= render 'searchresults', articles: @articles %>
</div>
<div id="results">
<%= render 'searchresults', articles: @articles %>
</div>
$('#results').html("<%= j render 'welcome/searchresults', articles: @articles%>");
<table class="table">
<tr>
<th>Title</th>
<th>Description</th>
<th></th>
</tr>
<% articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.description %></td>
<td><%= link_to 'Show', article_path(article) %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>