2015-06-09 72 views
2

路由錯誤我做了一個網站,並遵循的指導,讓使用Rails的消息。但是,當我點擊 '保存',它自帶了一個路由錯誤:一個消息/條

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

此路由錯誤的結果?由於

+0

顯示從開發完整的錯誤消息。日誌文件.. –

+0

顯示的'articles_controller'代碼 – Prashant4224

+0

我相信[這個答案](http://stackoverflow.com/a/23865236/1270789)介紹如何正確地實現你的'new'和'create'。 –

回答

1

您有一個名爲application_controller.rb有一堆的文章在它的方法文件。相反,您應該有一個名爲articles_controller.rb的文件,其中包含所有與這些文章相關的方法。 Rails可以找不到保存方法,因爲它無法找到,它預計保存在文件

您可以通過改變線路解決您的問題。

class ApplicationController < ActionController::Base 

到:

class ArticlesController < ActionController::Base 

該文件應但是保存到your_app/app/controllers/articles_controller.rb

你會還需要另一個應用控制器文件。只要你知道,應用控制器是不相關的應用程序中的具體型號,但涉及提供通用的方法(尤其是輔助方法)到應用程序的其餘部分。

+0

哦!上帝...... OP做了一個混亂。 :/ –

+0

要保存的文件的位置是'your_app /應用/控制器/ articles_controller.rb'不'your_app /應用/資產/控制器/ articles_controller.rb' – Pavan

+0

@Pavan固定複製/粘貼錯誤。 – MarsAtomic

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 /應用/視圖/用品/」

相關問題