邮件/文章的路由错误

时间:2015-06-09 04:17:45

标签: ruby-on-rails ruby routing

我一直在制作一个网站,并按照指南使用rails制作消息。但是,当我点击“保存”时,会出现路由错误:

   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

我的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

我的show.html.erb:

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

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

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

我的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

我的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 %>

我的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>

此路由错误的任何结果?感谢

2 个答案:

答案 0 :(得分:1)

您有一个名为application_controller.rb的文件,其中包含一系列文章方法。相反,您应该有一个名为articles_controller.rb的文件,其中包含所有与文章相关的方法。 Rails找不到save方法,因为它无法找到它预期保存的文件。

您可以通过更改以下行来解决问题:

class ApplicationController < ActionController::Base

为:

class ArticlesController < ActionController::Base

此文件应保存到your_app/app/controllers/articles_controller.rb

但是,您仍然需要另一个应用程序控制器文件。您知道,应用程序控制器与应用程序中的特定模型无关,但是关注的是为应用程序的其余部分提供通用方法(特别是辅助方法)。

答案 1 :(得分:0)

您在ApplicationController中编写的代码应该在'ArticlesController'中。

应该是这样的:

class ApplicationController < ActionController::Base
end

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

articles_controller的路径应为path_to_your_app/app/controllers/articles_controller.rb,而views位于“path_to_your_app / app / views / articles /”