<%= form_for @post do |f| %>
<%= f.label :title %><br>
<%= f.text_field :title %><br>
<br>
<%= f.label :body %><br>
<%= f.text_area :body %><br>
<br>
<%= submit %>
<% end %>
................ plz帮助我解决这个问题....................
class PostController < ApplicationController
def index
end
def new
@post = Post.new
end
end
当我执行此代码时,将显示此错误“表单中的第一个参数不能包含nil或为空”
答案 0 :(得分:0)
在db / create_post.rb
中t.string :title
t.text :body
然后执行db:migrate(它迁移数据库的架构)
class PostController < ApplicationController
before_action :find_post, only: [:show, :edit, :update, :destroy]
def new
@post = Post.new
end
def create
@post = Post.new(post_params)
if @post.save
redirect_to posts_path
else
render 'new'
end
end
def show
end
def post_params
params.require(:post).permit(:title, :body)
end
def find_post
@post = Post.find(params[:id])
end
end
在post / new.html.erb
中<%=form_for @post do |f|%>
<%= f.label :title %><br>
<%= f.text_field :title %><br>
<%= f.label :body %><br>
<%= f.text_area :body %><br>
<%end%>
在post / index.html.erb
中<%@post.each do |post|%>
<%=post.title%>
<%=post.body%>
<%end%>