格式中的第一个参数不能包含nil或为空

时间:2019-01-31 12:57:56

标签: ruby-on-rails

 <%= 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或为空”

1 个答案:

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