(Beginner) ActionController::UrlGenerationError - No route matches

(Using Rails 4.1.1 with Ruby 2.1.1 on Mac OSX 10.6 Snow Leopard)

I'm doing a Rails tutorial (in case you are interested in: It's Getting Started with Rails — Ruby on Rails Guides), and I'm stuck on the following:

I create in some erb file a link using

  <%= link_to 'Add new weird stuff', controller: new_article_path %>

and this raises the exception

ActionController::UrlGenerationError in Articles#index No route matches {:action=>"index", :controller=>"articles/new"}

The helper new_article_path returns 'articles/new'. My routes are these:

welcome_index GET /welcome/index(.:format) welcome#index      articles GET /articles(.:format) articles#index               POST /articles(.:format) articles#create   new_article GET /articles/new(.:format) articles#new edit_article GET /articles/:id/edit(.:format) articles#edit       article GET /articles/:id(.:format) articles#show               PATCH /articles/:id(.:format) articles#update               PUT /articles/:id(.:format) articles#update               DELETE /articles/:id(.:format) articles#destroy          root GET / welcome#index

My ArticlesController class has a method 'new' with empty body.

I have views/articles/new.html.erb.

When I manually enter the URL http://localhost:3001/articles/new in my browser, the correct page is shown.

I had expected that my link_to call() would generate a link to that very page, but instead it throws an exception. Something seems to be missing here.

Where am I wrong here, and how can I fix it? (I hope the information provided is complete to answer this question).

try something like this:

<%= link_to t('.new', :default => t("helpers.links.new")),                       new_article_path(article), :class => 'btn btn-mini' %>

Where class =>'btn btn-mini' is un type of button when you use bootstrap theme if you don't use it try whitout this option.

The answer is in the error if you look carefully. You want it to link to controller articles, action new, but the error says that it is going to controller articles/new. It is because you have told it that the controller is articles/new, whereas in fact this is the complete path. You just need

<%= link_to 'Add new weird stuff', new_article_path %>

Colin

Colin Law wrote in post #1146273:

It is because you have told it that the controller is articles/new, whereas in fact this is the complete path. You just need

<%= link_to 'Add new weird stuff', new_article_path %>

:open_mouth:

Now after you explained it, it became obvious!!

Thanks a lot.

Ronald

Hi there!

I’d like to renew this old thread with another question. I’m also a newbie to RoR following the getting_started tutorial now and stuck upon creating new article. Unlike that stated and expected the form_with model renders markup to the same articles/new, not the home. If I specify the url: articles_path in the form helper it posts to #create action then but raises param is missing or the value is empty: article. What am I doing wrong? Is that tutorial page still relevant at all?

(excuse me if I missed some existing solution)