ruby-on-rails - 为什么我得到 PostsController 的未定义方法 `action'(表不存在)?

标签 ruby-on-rails ruby ruby-on-rails-4

我对编码还很陌生,所以我决定开始阅读 4.0.0 版的 Ruby on Rails 指南,但遇到了一个又一个问题。我目前运行的是 4.0.0 版,我已逐步按照指南进行操作。

一旦我达到 5.2 First form,我开始出现错误,并使用其他人的帖子来解决我的问题,但这个错误似乎并没有发生在其他人身上,所以这里是:

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

这是我的代码:

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   

这是我的看法:

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

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

我的表格是:

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

错误指出我没有定义操作方法,但我不确定首先如何定义一个,但这还不是全部。当我转到我的 localhost:3000/posts/new 页面时,我得到了同样的错误,但是对于 PostsController#new

谁能帮帮我!!

最佳答案

不清楚为什么您的 Controller 继承自模型类:

class PostsController < ApplicationController
  # ...
end

关于ruby-on-rails - 为什么我得到 PostsController 的未定义方法 `action'(表不存在)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18387903/

相关文章:

ruby-on-rails - Ruby on Rails 全局 ActiveRecord::枚举

ruby-on-rails - rails-如何根据时间/日期更新属性

ruby - 检查一个字符串是否包含 Ruby 中另一个字符串的所有字符

ruby-on-rails - 我应该使用 log_level :debug in production on my Rails app

ruby-on-rails - 在 Bundler 中指定 JRuby 版本

ruby-on-rails - 我似乎已经破坏了 validates_confirmation_of

ruby-on-rails - 如何覆盖 ruby​​ on rails 4.0.+ 中的默认主键列?

ruby - Rails 3.2 CRUD : . 其中 'or' 有条件

ruby-on-rails - Ruby 2.3 - 添加超时错误和通知到网络 :http request

ssl - Ruby 网络 HTTP 发布 : Certificate Verify Failed