我正在创建instagram应用程序,从 https:/medium.comluanotesbuild-instagram by-ruby-on-rails-part-2-d70b44f5c7e6。.
在这个主题下。在主页上添加一个创建新帖子的表格
我得到这个错误。undefined method 'id' for nil:NilClass
我的索引视图文件
<%= form_for Post.new do |f| %>
<div class="form-group">
<%= f.text_field :description %>
</div>
<div class="form-group">
<%= f.file_field :image %>
</div>
<div class="form-group">
<%= f.text_field :user_id,nil, value: current_user.id, class:'d-none'%>
</div>
<br>
<div class="text-center">
<%= f.submit 'Create Post', class: 'btn btn-primary' %>
</div>
<% end %>
控制器文件。
class PostsController < ApplicationController
def create
Post.create(post_params)
redirect_to root_path
end
private
def post_params
params.require(:post).permit(:description, :image, :user_id)
end
end
路由是:
root to: 'home#index'
devise_for :users
resources :users, only: [:show, :edit, :update]
resources :posts, only: [:new, :create]