Yahs Hef
Yahs Hef

Reputation: 797

routing error for an message/article

I been making a website and a followed a guide to make a message using rails. But, when I click 'Save', it comes up with an routing error:

   Routing Error
   No route matches [POST] "/articles/new"

   Rails.root: C:/Users/yahshef/sites/game

   Application Trace | Framework Trace | Full Trace
   Routes

   Routes match in priority from top to bottom

   Helper   HTTP Verb   Path    Controller#Action
   Path / Url           
   welcome_index_path   GET /welcome/index(.:format)    welcome#index
   articles_path    GET /articles(.:format) articles#index
   POST /articles(.:format) articles#create
   new_article_path GET /articles/new(.:format) articles#new
   edit_article_path    GET /articles/:id/edit(.:format)    articles#edit
   article_path 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_path    GET /   welcome#index

My application_controller.rb:

    class ApplicationController < ActionController::Base
     def index
       @articles = Article.all
     end

     def show
       @article = Article.find(params[:id])
     end

     def new
     end

     def create
        @article = Article.new(article_params)

        @article.save
        redirect_to @article
    end

    private
        def article_params
             params.require(:article).permit(:title, :receiver, :text)
        end
      protect_from_forgery with: :exception
    end

My show.html.erb:

    <p>
      <strong>Title:</strong>
      <%= @article.title %>
    </p>

    <p>
      <strong>Receiver:</strong>
      <%= @article.receiver %>
    </p>

    <p>
      <strong>Text:</strong>
      <%= @article.text %>
    </p>

My 20150609013912_create_articles.rb:

    class CreateArticles < ActiveRecord::Migration
      def change
        create_table :articles do |t|
          t.string :title
          t.string :receiver
          t.text :text

          t.timestamps
        end
      end
    end

My new.html.erb

    <h1>New Message</h1>
    <%= form_for :article do |f| %>
      <p>
        <%= f.label :title %><br>
        <%= f.text_field :title %>
      </p>

      <p>
        <%= f.label :receiver %><br>
        <%= f.text_field :receiver %>
      </p>

      <p>
        <%= f.label :text %><br>
        <%= f.text_area :text %>
      </p>

      <p>
        <%= f.submit %>
      </p>
    <% end %>

And my index.html.erb:

    <h1>Listing messages</h1>

     <table>
       <tr>
        <th>Title</th>
        <th>Receiver</th>
        <th>Text</th>
       </tr>

     <% @articles.each do |article| %>
        <tr>
          <td><%= article.title %></td>
          <td><%= article.receiver %></td>
          <td><%= article.text %></td>
        </tr>
      <% end %>
    </table>

Any results for this routing error? Thanks

Upvotes: 2

Views: 172

Answers (2)

MarsAtomic
MarsAtomic

Reputation: 10696

You have a file called application_controller.rb with a bunch of methods for Article in it. You should, instead, have a filed called articles_controller.rb with all of those article related methods in it. Rails can't find the save method because it can't find the file that it expects save to be in.

You could fix your problem by changing the line:

class ApplicationController < ActionController::Base

to:

class ArticlesController < ActionController::Base

This file should be saved to your_app/app/controllers/articles_controller.rb

You'd still need another Application Controller file, however. Just so you know, the Application Controller is not related to a specific model in your application, but is concerned with providing general methods (particularly helper methods) to the rest of your application.

Upvotes: 1

usmanali
usmanali

Reputation: 2037

The code you have written in ApplicationController should be in 'ArticlesController'.

It should be like:

class ApplicationController < ActionController::Base
end

class ArticlesController < ApplicationController
  # Your methods for the articles controller
end 

And the path to the articles_controller should be path_to_your_app/app/controllers/articles_controller.rb and your views under 'path_to_your_app/app/views/articles/'

Upvotes: 0

Related Questions