当我试图在我的应用上实现一种帖子功能时,我碰到了这个错误,这是我的头脑。
ArgumentError in Projects#show
first argument in form cannot contain nil or be empty
<%= form_for(@micropost, html: { multipart: true }) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
Micropost控制器。
class MicropostsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: [:create, :destroy]
def create
@project = Project.find(params[:project_id])
@micropost = @project.microposts.build(micropost_params)
@micropost.user.id = current_user.id
if @micropost.save
flash[:success] = "Micropost created!"
redirect_to @project
else
@feed_items = []
render 'static_pages/home'
end
end
Micropost.model
class Micropost < ApplicationRecord
belongs_to :user
belongs_to :project
default_scope -> { order(created_at: :desc) }
mount_uploader :picture, PictureUploader
validates :user_id , presence: true
validates :project_id, presence: true
validate :picture_size
#...
end
来自routes.rb的摘录
resources :users do
member do
get :following, :followers
end
end
resources :projects do
resources :microposts
end
和表格。
<%= form_for(@micropost, html: { multipart: true }) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "Compose new micropost..." %>
<%= hidden_field_tag :user_id, :value => current_user.id %>
</div>
<%= f.submit "Post", class: "btn btn-primary" %>
<span class="picture">
<%= f.file_field :picture, accept: 'image/jpeg,image/gif,image/png' %>
</span>
<% end %>
有什么想法吗?
-------更新1 ---------
项目控制器
class ProjectsController < ApplicationController
before_action :logged_in_user, only: [:index, :show, :create, :destroy]
before_action :correct_user || :admin_user , only: :destroy
def index
end
def show
@project = Project.find(params[:id])
end
def new
@project = Project.new
end
def create
@project = current_user.projects.build(project_params)
if @project.save
flash[:success] = "Project Created"
redirect_to @project
else
render 'new'
end
end
def destroy
Project.find(params[:id]).destroy
flash[:success] = "Project Deleted"
redirect_to request.referrer || root_url
end
end
------更新2 --------
class ProjectsController < ApplicationController
before_action :logged_in_user, only: [:index, :show, :create, :destroy]
before_action :correct_user, only: :destroy
before_action :admin_user, only: :destroy
def show
@project = Project.find(params[:id])
@micropost = @project.microposts.build
#I cannot have them both defined at the same time so how do I go about this
@micropost = @project.microposts.paginate(page: params[:page])
end
def new
@project = Project.new
end
def create
@project = current_user.projects.build(project_params)
if @project.save
flash[:success] = "Project Created"
redirect_to @project
else
render 'new'
end
end
def destroy
Project.find(params[:id]).destroy
flash[:success] = "Project Deleted"
redirect_to request.referrer || root_url
end
private
def project_params
params.require(:project).permit(:name,:category,:picture)
end
def correct_user
@project = current_user.projects.find_by(id: params[:id])
redirect_to root_url if @project.nil?
end
end
答案 0 :(得分:0)
好的,问题是,
在项目控制器展示操作中,您正在初始化@project
但不是@micropost
,这将是nil
并且我认为您在Projects#show
中渲染相同的部分,它正在尝试为@micropost
呈现表单,在这种情况下为nil
<%= form_for(@micropost, html: { multipart: true }) do |f| %>
应该是
<%= form_for(@project, html: { multipart: true }) do |f| %>