0
當我點擊提交按鈕時,它會創建具有空值的帖子。當我在rails控制檯中創建帖子時沒有問題。有任何想法嗎?無法創建新帖子
_form.html.erb
<%= form_for(@post) do |f| %>
<div class="field">
<%= f.label :title %>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :body %><br>
<%= f.text_area :body %>
</div>
<div class="field">
<%= f.label :user_id %><br>
<%= f.number_field :user_id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
posts.controller
class PostsController < ApplicationController
def index
@posts = Post.all
end
def new
@post = Post.new
end
def create
@post = Post.new(post_params)
@post.save!
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:title, :body, :user_id)
end
end