gpt4 book ai didi

ruby-on-rails - NoMethodError 在 Rails 中构建一个简单的 CMS

转载 作者:数据小太阳 更新时间:2023-10-29 08:12:18 25 4
gpt4 key购买 nike

我一直在关注 this tutorial from lynda.com .

我在第 15 章:在主题中嵌套页面

所以我一直在跟进,甚至只是复制他们提供给您的练习文件。但是我不断收到此错误:

PagesController 中没有方法错误#newnil:NilClass 的未定义方法“id”

它高亮显示第二行:

def new
@page = Page.new({:subject_id => @subject.id, :name => "Default"})
@subjects = Subject.order('position ASC')
@page_count = Page.count + 1
end

这是我的 Controller

class PagesController < ApplicationController

layout "admin"

before_action :confirm_logged_in
before_action :find_subject

def index
# @pages = Page.where(:subject_id => @subject.id).sorted
@pages = @subject.pages.sorted
end

def show
@page = Page.find(params[:id])
end

def new
@page = Page.new({:subject_id => @subject.id, :name => "Default"})
@subjects = Subject.order('position ASC')
@page_count = Page.count + 1
end

def create
@page = Page.new(page_params)
if @page.save
flash[:notice] = "Page created successfully"
redirect_to(:action => 'index', :subject_id => @subject_id)
else
@subjects = Subject.order('position ASC')
@page_count = Page.count + 1
render('new')
end
end

def edit
@page = Page.find(params[:id])
@subjects = Subject.order('position ASC')
@page_count = Page.count
end

def update
# Find an existing object using form parameters
@page = Page.find(params[:id])
# Update the object
if @page.update_attributes(page_params)
# If update succeeds, redirect to the index action
flash[:notice] = "Page updated successfully"
redirect_to(:action => 'show', :id => @page.id, :subject_id => @subject_id)
else
# If the update fails, redisplay the form so user can fix problems
@subjects = Subject.order('position ASC')
@page_count = Page.count
render('edit')
end
end

def delete
@page = Page.find(params[:id])
end

def destroy
page = Page.find(params[:id]).destroy
flash[:notice] = "Page '#{page.name}' destroyed successfully"
redirect_to(:action => 'index', :subject_id => @subject_id)
end

private

def page_params
# Same as using "params[:subject]", except that it:
# - raises as error if :subject is not present
# - allows listed attributes to be mass-assigned
params.require(:page).permit(:subject_id, :name, :permalink, :position, :visible)
end

def find_subject
if params[:subject_id]
@subject = Subject.find(params[:subject_id])
end
end

end

然后是 View :

<% @page_title = "New Page" %>

<%= link_to("<< Back to list", {:action => 'index', :subject_id => @subject_id}, :class => 'back-link') %>

<div class="pages new">
<h2>Create Page</h2>

<%= form_for(:page, :url => {:action => 'create', :subject_id => @subject_id}) do |f| %>

<%= render(:partial => "form", :locals => {:f => f}) %>

<div class="form-buttons">
<%= submit_tag("Create page") %>
</div>

<% end %>
</div>

一个类似这个的问题has been asked here ,但我在那里看不到任何解决方案。

非常感谢任何帮助!

最佳答案

我觉得问题出在

Page.new({:subject_id => @subject.id, :name => "Default"})

@subject 未定义,您正在询问其 id

关于ruby-on-rails - NoMethodError 在 Rails 中构建一个简单的 CMS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23816564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com