开发者

Undefined method unexisting_url

I´m a newbie with Rails3 and I´ve got a strange problem. After searching in google and in StackOverflow for a while I decided to write down my question.

I have a Competencia and a Partida model. Competencia has_many :partidas and Partidas belongs_to :competencia.

I´m working with nested resources and my code looks like this:

routes.rb

resources :competencias do
  resources :partidas
end

partidas_controller.rb

class PartidasController < ApplicationController
  def new
    @competencia = Competencia.find(params[:competencia_id])
    @partida = @competencia.partidas.build
  end

  def create
    @competencia = Competencia.find(params[:competencia_id])
    @partida = @competencia.partidas.build(params[:partida])
    if @partida.save then #blabla end
  end
end

views/partidas/new.html.erb

<%= form_for [@competencia, @partida], :url => competencia_partidas_path(@competencia) do |f| %>
    <!--blabla-->
<% end %>

I know that it isn´t the right way to specify the url in the form_for helper (specially if I´m not using a custom action), but it was the only way I could work it out. When I wrote something like this: <%= form_for [@competencia, @partida] do |f| %> I´ve got this error:

Showing /Users/ks/rails/projects/chronos/app/views/partidas/new.html.erb where line #4 raised:

 undefined method `competencium_partidas_path' for #<#<Class:0x00000101718548>:0x00000101713728>

When I checked the routes (ra开发者_如何学编程ke routes) everything seems to be fine.

competencia_partidas GET    /competencias/:competencia_id/partidas(.:format)          {:action=>"index", :controller=>"partidas"}
                     POST   /competencias/:competencia_id/partidas(.:format)          {:action=>"create", :controller=>"partidas"}
new_competencia_partida GET    /competencias/:competencia_id/partidas/new(.:format)      {:action=>"new", :controller=>"partidas"}

Can someone explain me where the competencium name comes from?. What would be the correct approach to solve this?


The problem is that Rails assumes english grammar rules for pluralization. You can read more here: http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html. You can customize the inflector or use English model names. I'm Italian and even when a project is meant only for Italian customer I prefer to use English names.


Rails tries to singularize you model name. In your case, it thinks competencia is the plural of a latin word. To add an exception, put the following in config/initializers/inflections.rb:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.singular "competencia", "competencia"
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜