Rails 7 respond_to ActionController::UnknownFormat 错误

发布于 2025-01-10 11:54:54 字数 2150 浏览 2 评论 0原文

我一直在使用 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'

这是我的目录以获取更多详细信息:

rails_directory

这是我的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:

rails_directory

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文