Rails 7 respond_to ActionController::UnknownFormat 错误
我一直在使用 Ruby on Rails 7,我想更新一个 div 元素,用 format.js
渲染部分内容,但当执行到达此行时会出现错误。
这是我的控制器:
class StocksController < ApplicationController
def search
if params[:stock].present?
@stock = Stock.new_lookup(params[:stock])
if @stock
respond_to do |format| # <-------------------- Here is the error
format.js { render partial: 'users/result' }
end
else
flash[:alert] = 'Please enter a valid symbol to search'
redirect_to my_portfolio_path
end
else
flash[:alert] = 'Please enter a symbol to search'
redirect_to my_portfolio_path
end
end
end
我创建了一个视图,在其中创建了一个 div 来仅修改 id 为“结果”的 div。
<%= form_tag search_stock_path, remote: true, method: :get do %>
<div class='form-group row'>
<div class='col-sm-9 pe-0'>
<%= text_field_tag :stock,
params[:stock],
placeholder: 'Stock ticker',
autofocus: true,
class: 'form-control form-control-lg' %>
</div>
<div class='col-sm-3 ps-0'>
<%= button_tag type: :submit, class: 'btn btn-success' do %>
<i class='fa fa-search fa-2x'></i>
<% end %>
</div>
</div>
<% end %>
</div>
<div id='results'> <!-- This is the div -->
</div>
我有一个部分名称 users/result.js.erb
,当我提交搜索时,我收到此错误:
ActionController::UnknownFormat - ActionController::UnknownFormat:
app/controllers/stocks_controller.rb:7:in `search'
这是我的目录以获取更多详细信息:
这是我的routes.rb
Rails.application.routes.draw do
devise_for :users
root "welcome#index"
get 'my_portfolio', to: 'users#my_portfolio'
get 'search_stock', to: 'stocks#search'
end
任何人都知道执行 respond_to
时发生了什么?
为什么会出现此 ActionController::UnknownFormat
错误?
I've been playing around with Ruby on Rails 7 and I want to update a div element rendering a partial with format.js
but an error arise when the execution reaches this line.
This is my controller:
class StocksController < ApplicationController
def search
if params[:stock].present?
@stock = Stock.new_lookup(params[:stock])
if @stock
respond_to do |format| # <-------------------- Here is the error
format.js { render partial: 'users/result' }
end
else
flash[:alert] = 'Please enter a valid symbol to search'
redirect_to my_portfolio_path
end
else
flash[:alert] = 'Please enter a symbol to search'
redirect_to my_portfolio_path
end
end
end
I created a view where I created a div to modify only that div which id is 'results'.
<%= form_tag search_stock_path, remote: true, method: :get do %>
<div class='form-group row'>
<div class='col-sm-9 pe-0'>
<%= text_field_tag :stock,
params[:stock],
placeholder: 'Stock ticker',
autofocus: true,
class: 'form-control form-control-lg' %>
</div>
<div class='col-sm-3 ps-0'>
<%= button_tag type: :submit, class: 'btn btn-success' do %>
<i class='fa fa-search fa-2x'></i>
<% end %>
</div>
</div>
<% end %>
</div>
<div id='results'> <!-- This is the div -->
</div>
I have a partial name users/result.js.erb
and when I submit the search I receive this error:
ActionController::UnknownFormat - ActionController::UnknownFormat:
app/controllers/stocks_controller.rb:7:in `search'
This is my directory for more details:
And this is my routes.rb
Rails.application.routes.draw do
devise_for :users
root "welcome#index"
get 'my_portfolio', to: 'users#my_portfolio'
get 'search_stock', to: 'stocks#search'
end
Anyone knows what's going on here when respond_to
is executed?
Why this ActionController::UnknownFormat
error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论