【问题标题】:Rails handle nil/undefined methodRails 处理 nil/未定义的方法
【发布时间】:2019-01-21 19:09:58
【问题描述】:

我正在尝试将名为 /categories 的内容全部移至 /digital-catalog。

我正在重定向所有内容并且它可以工作,但是当输入不存在的 url_name 时它会失败。

我几乎是三元运算符的新手,但看起来它失败了。

代码在以下方面失败:

- custom_search_path = action_name == 'show' ? category_path(@category.url_name) : digital_catalog_path
.search-cont
 = form_tag custom_search_path, id: 'search-filter', method: :get do
   = search_field_tag :search_input, params[:search_input], placeholder: 'Search', class: 'form-control'
   - if params[:search_input]
     = link_to 'x', custom_search_path, class: 'add-on'
   = submit_tag 'go'

确切的错误信息是:

类别中的 NoMethodError#show

nil:NilClass 的未定义方法 `url_name'

我的 CategoriesController 看起来像:

class CategoriesController < ApplicationController
  respond_to :html, :json
  before_action :load_category, only: [:show]
  before_action :require_user

  def index
    @categories = Category.ordered.search(params[:search_input]).paginate(page: params[:page])
    respond_with @categories
  end

  def show
    if @category.nil?
      redirect_to '/'
    else
      @items = @items.search(params[:search_input]).paginate(page: params[:page])
      @response = { category: @category, items: @items }
      respond_with @response
    end
  end

  private

  def load_category
    @category = Category.find_by!(url_name: params[:id])
    @items = @category.items
    add_breadcrumb 'categories', digital_catalog_path
    add_breadcrumb @category.name
  end
end

当有人输入的网址与此处不匹配时,我该如何处理?

我已尝试使用以下内容代替助手:

def route_category
 if action_name == 'show'
   redirect_to digital_catalog_path(@category.url_name)
 else
   redirect_to root_path
 end
end

最终得到未定义的方法“redirect_to”,老实说,这并不能解决表单标签获取正确数据的问题。

编辑:

我也试过同样的错误信息:

def show
 if @category.url_name.nil?
   redirect_to root_path
 else
   @items = @items.search(params[:search_input]).paginate(page: params[:page])
   @response = { category: @category, items: @items }
   respond_with @response
 end
end

那么如何处理不存在的 url?

编辑:我已经添加了整个 CategoriesController。以及我认为可以处理错误的 show 方法的更新,但它没有。

【问题讨论】:

  • 错误信息很清楚:@categorynil。你在渲染之前有没有打电话给load_category? (也许在控制器中的before_action 中?)
  • 有一个before_action :load_category,只有:[:show]
  • 你能把你的 CategoriesController 全部贴出来吗?
  • @Deekor 我更新了整个控制器。
  • 那么问题到底是什么?如何渲染 404?

标签: ruby-on-rails


【解决方案1】:

我还没有找到将它发送到 404 页面的正确方法,但这是我的权宜之计:

def show
 if @category
   @items = @items.search(params[:search_input]).paginate(page: params[:page])
   @response = { category: @category, items: @items }
   respond_with @response
 else
   redirect_to digital_catalog_path
 end
end

private

def load_category
  begin
    @category = Category.find_by!(url_name: params[:id])
    @items = @category.items
    add_breadcrumb 'categories', digital_catalog_path
    add_breadcrumb @category.name
  rescue ActiveRecord::RecordNotFound => e
    @category = nil
  end
end

【讨论】:

    猜你喜欢
    • 2021-05-25
    • 2020-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-23
    • 2017-08-04
    • 2015-03-06
    • 1970-01-01
    相关资源
    最近更新 更多