EDIT2:[已解决]控制器文件中的“创建”方法在“编辑”方法之后才会“结束”。但是,直到 8 小时后,我才能回答我自己的问题。
编辑:我注意到这可能是我的 routes.rb 文件的问题。就在下面。
路线.rb
SimpleCms::Application.routes.draw do
root :to => "subjects#list"
match ':controller(/:action(/:id))(.:format)'
end
本质上这是一个@instance_variable 问题。更具体地说,@instance_variable[:id => nil] 问题。不知何故,我将 nil (null) :id (primary key) 值传递给 rails 但我单击的编辑按钮(此列表中的最后一个文件是 list.html.erb 文件,其中包含需要的按钮您编辑.html.erb) 应该传递与列表页面上给定主题相对应的 :id 值。
因此,首先是来自浏览器的错误消息:
RuntimeError in Subjects#edit
Showing C:/Users/davo/Desktop/RailsProjects/simple_cms/app/views/subjects/edit.html.erb where line #6 raised:
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
Extracted source (around line #6):
3: <div class="subject edit">
4: <h2>Update Subject</h2>
5:
6: <%= form_for(:subject, :url => {:action => 'update', :id => @subject.id}) do |f| %>
7:
8: <table summary="Subject form fields">
9: <tr>
Rails.root: C:/Users/davo/Desktop/RailsProjects/simple_cms
Application Trace | Framework Trace | Full Trace
app/views/subjects/edit.html.erb:6:in `_app_views_subjects_edit_html_erb__829468061_51428148'
Request
Parameters:
{"id"=>"1"}
这是控制器(“subjects_controller.rb”),所以你可以看到实例变量的东西。编辑和更新方法在底部 **所有其他调用 params[:id] 的方法都在工作。控制器可能没有任何问题,而只是视图。特别是,根据这种观点,@subject 与 :subject 与subjects.method 的东西......该语法似乎有问题。
class SubjectsController < ApplicationController
def list
@subjects = Subject.order("subjects.position ASC")
end
def index
list
render('list')
end
def show
@subject = Subject.find(params[:id])
end
def new
@subject = Subject.new(:name => 'default')
end
def create
#instantiate a new object using form params
@subject = Subject.new(params[:subject])
#save the subject
if @subject.save
redirect_to(:action => 'list')
#if save succeeds redirect to list action
else
#if save fails, redisplay form
render('new')
end
def edit
@subject = Subject.find(params[:id])
end
#passing an @instancevariable inside of a {hash}...is there anything odd about that?
#I'm just trying to inspire you guys -->
#
#I have a hunch that the :syntax/@syntax/#{etc} involving params, :id and
#:subject could be the root of this issue...just a hunch
#
def update
#Find object using form parameters
@subject = Subject.find(params[:id])
#Update the object
if @subject.update_attributes(params[:subject])
redirect_to(:action => 'show', :id => @subject.id)
else
render('edit')
end
end
end
end
最后,这里是edit.html.erb
<%= link_to("<< Back to List", {:action => 'list'}, :class => 'back-link') %>
<div class="subject edit">
<h2>Update Subject</h2>
<%= form_for(:subject, :url => {:action => 'update', :id => @subject.id}) do |f| %>
<table summary="Subject form fields">
<tr>
<th>Name</th>
<td><%= f.text_field(:name) %></td>
</tr>
<tr>
<th>Position</th>
<td><%= f.text_field(:position) %></td>
</tr>
<tr>
<th>Visible</th>
<td><%= f.text_field(:visible) %></td>
</tr>
</table>
<tr>
<td><%= f.submit 'Update Subject' %></td>
</tr>
<% end %>
</div>
编辑:这是 list.html.erb 文件,其中包含指向 edit.html.erb 文件的链接,该文件目前未打开并显示错误消息。
<html>
<div class="subject list">
<h2>Subjects</h2>
<!--- header row with all the different attributes --->
<table class="listing" summary="Subject list">
<tr class="header">
<th>#</th>
<th>Subject</th>
<th>Visible</th>
<th>Pages</th>
<th>Actions</th>
</tr>
<!-- this is the beginning of a loop, that ends way down there -->
<% @subjects.each do |subject| %>
<tr>
<td><%= subject.position %></td>
<td><%= subject.name %></td>
<td class="center"><%= subject.visible ? 'Yes' : 'No' %></td>
<td class="center"><%= subject.pages.size %></td>
<td class="actions">
<%= link_to("Show", {:action => 'show', :id => subject.id}, :class => 'action show') %>
<%= link_to("Edit", {:action => 'edit', :id => subject.id}, :class => 'action edit') %>
<%= link_to("Delete", '#', :class => 'action delete') %>
</td>
<% end %>
</tr>
</table>
<%= button_to "Create New Subject", {:action => 'new'}, {:class => "buttonTo"} %>
</div>
</html>
编辑: - 如果您需要查看特定的 model.rb 文件,请告诉我。如果您需要查看其中的任何一个,在我的模型文件夹中,我有“subject.rb”、“section.rb”、“section_edit.rb”、“page.rb”和“admin_user.rb”。我对这个有点困惑,所以也许它们会有用。它们都包含一堆模式(has_many、belongs_to 等)指令和一些自定义控制台调用。