我对编码很陌生,所以我决定启动版本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 %>
错误表明我没有定义动作方法,但我不知道如何首先定义一个动作方法,但这不是全部。当我去我的本地主机:3000 /帖子/新页面时,我得到了相同的错误但PostsController#new
有人可以帮帮我!!
答案 0 :(得分:19)
目前尚不清楚为什么你的控制器继承了模型类:
class PostsController < ApplicationController
# ...
end