我对编码还很陌生,所以我决定开始阅读 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/