目前,用户注册并创建个人资料。登录后,他们可以创建一个项目。在项目展示页面上,他们可以选择使用此链接添加包含说明的其他图像:
<%= link_to“New Image”,new_project_image_path(current_user.profile_name,@ project)%>
这些链接有效,它会调出images / new.html.erb,但是当我点击“创建”时,我会收到错误:
undefined method `images' for nil:NilClass
突出显示的行在create方法中 -
@image = @project.images.new(image_params)
我的user.rb文件包含以下内容:
has_many :projects, dependent: :destroy
project.rb
belongs_to :user
has_many :images, dependent: :destroy
image.rb
belongs_to :project
的routes.rb
scope ":profile_name" do
resources :projects do
resources :images
end
end
projects_controller.rb(我还没有完成这个,但它可以正确地创建项目)
before_action :set_project, only: [:show, :edit, :update, :destroy]
def index
@projects = Project.all.order('created_at desc')
end
def new
@project = current_user.projects.build
end
def create
@project = current_user.projects.new(project_params)
if @project.save
flash[:success] = "Your project has been created."
redirect_to project_path(@project.user.profile_name, @project)
else
flash[:alert] = "Sorry, your project could not be created."
render :new
end
end
def edit
end
def show
end
def update
if @project.update(project_params)
flash[:success] = "Project updated."
redirect_to root_path
else
flash[:alert] = "Update failed. Please check the form."
render :edit
end
end
def destroy
@project.destroy
flash[:success] = "Your project has been deleted."
redirect_to root_path
end
private
def project_params
params.require(:project).permit(:title, :attachment, :description, :slug, :user_id)
end
def set_project
@project = Project.friendly.find(params[:id])
end
end
images_controller.rb(未完全完成,因为我需要在保存时对重定向进行排序)
class ImagesController < ApplicationController
before_action :set_project
# GET /images
# GET /images.json
def index
@images = Image.all
end
# GET /images/1
# GET /images/1.json
def show
end
# GET /images/new
def new
@image = Image.new
end
# GET /images/1/edit
def edit
end
# POST /images
# POST /images.json
def create
@image = @project.images.new(image_params)
respond_to do |format|
if @image.save
format.html { redirect_to @image, notice: 'Image was successfully created.' }
format.json { render :show, status: :created, location: @image }
else
format.html { render :new }
format.json { render json: @image.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /images/1
# PATCH/PUT /images/1.json
def update
respond_to do |format|
if @image.update(image_params)
format.html { redirect_to @image, notice: 'Image was successfully updated.' }
format.json { render :show, status: :ok, location: @image }
else
format.html { render :edit }
format.json { render json: @image.errors, status: :unprocessable_entity }
end
end
end
# DELETE /images/1
# DELETE /images/1.json
def destroy
@image.destroy
respond_to do |format|
format.html { redirect_to images_url, notice: 'Image was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
# Never trust parameters from the scary internet, only allow the white list through.
def image_params
params.require(:image).permit(:user_id, :project_id, :title, :description, :link)
end
# Use callbacks to share common setup or constraints between actions.
def set_project
@project = Project.friendly.find(params[:id])
end
end
我无法解决导致它最终成为零的原因:NilClass。有任何想法吗?
如果您需要任何进一步的信息,请告诉我,因为我不确定要包括哪些内容。
答案 0 :(得分:0)
尝试将before_action
中的images_controller.rb
回调更改为以下内容:
def set_project
@project = Project.friendly.find(params[:project_id])
end