【问题标题】:Weird route behaivor for show view显示视图的奇怪路由行为
【发布时间】:2013-12-09 16:23:04
【问题描述】:

我正在使用西班牙语的所有型号名称制作应用程序。我遇到了一些与奇异化相关的奇怪问题。 我的模型:

class Artista < ActiveRecord::Base
  attr_accessible :fecha, :foto, :instrumento, :nombre
end

我的模特名字是单数的“artista”(艺术家)。

控制器:

class ArtistasController < ApplicationController
  # GET /bandas
  # GET /bandas.json
  def index
    @artistas = Artista.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @artistas }
    end 
  end 

  def show
    @artista = Artista.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @artista }
    end 
  end 
  def new
    @artista = Artista.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @artista }
    end
  end

  def edit
    @artista = Artista.find(params[:id])
  end

  def create
    @artista = Artista.new(params[:artista])

    respond_to do |format|
      if @artista.save
format.html { redirect_to @artista, notice: 'Artista was successfully created.' }
        format.json { render json: @artista, status: :created, location: @artista }
      else
        format.html { render action: "new" }
        format.json { render json: @artista.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    @artista = Artista.find(params[:id])
    respond_to do |format|
      if @artista.update_attributes(params[:banda])
        format.html { redirect_to @artista, notice: 'Artista was successfully updated.' }
        format.json { head :no_content }
 else
        format.html { render action: "edit" }
        format.json { render json: @artista.errors, status: :unprocessable_entity }
      end
    end
 end
  def destroy
    @artista = Artista.find(params[:id])
    @artista.destroy
    respond_to do |format|
      format.html { redirect_to artistas_url }
      format.json { head :no_content }
    end
   end
   end

(所有这些都是使用 rails generate 命令自动创建的)

现在,我的路线包括以下内容:

resources :artistas

当我访问localhost:3000/artistas 时,一切正常。我可以看到已经创建的艺术家列表。现在,当我出于某种奇怪的原因单击现有艺术家(或者在我尝试创建新艺术家后,被重定向到表演艺术家页面后)时,它会转到 http://localhost:3000/artistum.3(3 是我点击的艺术家的 ID) .该 url 的输出是一个完全空白的页面。

我什至从来没有打过artistum这个词。我不知道它是从哪里弄来的。此外,它有一个点而不是斜线来分隔名称和id,所以我不知道如何重定向它。

我对包含所有内容的文件夹进行了 grep 搜索,但 artistum 这个词只存在于日志文件中。

我的猜测是,我的应用程序的某些部分认为“artista”是复数形式,而“artistum”是其单数形式。

我将match '/artistum' =&gt; 'artistas#index'添加到我的路线中,这适用于索引页面,但该点让我对如何为显示页面执行此操作感到困惑。

有人可以帮我 A) 找出它为什么要到达那里或 b) 如何从那些显示页面路由? 谢谢!

【问题讨论】:

  • 点击每个人Artistalink_to 是什么样子的?

标签: ruby-on-rails ruby ruby-on-rails-3 routes pluralize


【解决方案1】:

你可以试试这个:

将此添加到config/initializers 文件夹中的inflections.rb

ActiveSupport::Inflector.inflections do |inflect|
  inflect.plural 'artista', 'artistas'
  inflect.irregular 'artista', 'artistas'
end

【讨论】:

    猜你喜欢
    • 2014-10-23
    • 2017-09-01
    • 2016-09-25
    • 2017-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    相关资源
    最近更新 更多