为什么我为PostsController获取未定义的方法`action'(表不存在)?

时间:2022-08-22 22:44:23

I am pretty new to coding, so I decided to start the Ruby on Rails guide for version 4.0.0 and have had problem after problem. I am currently running version 4.0.0 and I have followed the guide step by step.

我对编码很陌生,所以我决定启动版本4.0.0的Ruby on Rails指南并且遇到问题后出现问题。我目前正在运行4.0.0版,我已逐步按照指南操作。

Once I got to 5.2 First form I began to get errors and was using other people's posts to solve my questions, but this error doesn't seem to happen for anyone else, so here it is:

一旦我到达第一个表单,我开始出错并使用其他人的帖子来解决我的问题,但这个错误似乎不会发生在其他任何人身上,所以这里是:

NoMethodError in PostsController#index
undefined method `action' for PostsController(Table doesn't exist):Class

Here is my code:

这是我的代码:

class PostsController < ActiveRecord::Base                             

attr_accessible :title, :text                            

  def new                                                              
  end                                                                  

  def create                                                           
    @post = Post.new(params[:post].permit(:title, :text))              

    @post.save                                                         
    redirect_to @post                                                  
  end                                                                  

  def show                                                             
    @post = Post.find(params[:id])                                     
  end                                                                  

  def index                                                            
    @post = Post.all                                                   
  end                                                                  

  def action                                                           
  end                                                                  

  private                                                              
    def post_params                                                    
      params.require(:post).permit(:title, :text)                      
    end                                                                

end   

Here is my view:

这是我的观点:

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

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

My form is:

我的表格是:

<h1> New Post </h1>

 <%= form_for :posts, url: posts_path do |f| %>

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

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

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

The error states that I don't have an action method defined, but I'm not sure how to define one in the first place, however this is not all. When I go to my localhost:3000/posts/new page I get the same error but for PostsController#new

该错误表明我没有定义一个动作方法,但我不知道如何首先定义一个动作方法,但这不是全部。当我去我的本地主机:3000 /帖子/新页面我得到相同的错误,但对于PostsController #new

Can someone please help me!!

有人可以帮帮我吗!!

1 个解决方案

#1


19  

It's not clear why your controller is inheriting from a model class:

目前尚不清楚为什么你的控制器继承了一个模型类:

class PostsController < ApplicationController
  # ...
end

#1


19  

It's not clear why your controller is inheriting from a model class:

目前尚不清楚为什么你的控制器继承了一个模型类:

class PostsController < ApplicationController
  # ...
end