EDIT2: [SOLVED] The “create” method in the controller file does not “end” until after the “edit” method. I can’t answer my own question until 8 hours afterwards, however.
EDIT: It’s been brought to my attention that this may be a problem with my routes.rb file. Which is just below.
routes.rb
SimpleCms::Application.routes.draw do
root :to => "subjects#list"
match ':controller(/:action(/:id))(.:format)'
end
Essentially this is an @instance_variable issue. More specifically an @instance_variable[:id => nil] problem. Somehow I’m passing a nil (null) :id (primary key) value to rails but the edit button which I’m clicking on (the last file in this list is the list.html.erb file which contains the button that takes you to edit.html.erb) is supposed to be passing the :id value corresponding to the given subject on the list page.
So here is the error message from the browser first of all:
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"}
Here is the controller (“subjects_controller.rb”) so you can see the instance variable stuff. The edit and update methods are on the bottom
**All the other methods which call params[:id] are working. It’s possible that there is nothing wrong with the controller and that it’s just the view. In particular, on that view, the @subject versus :subject versus subjects.method stuff…there seems to be something wrong with that syntax.
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
Lastly, here is 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>
EDIT:
here is the list.html.erb file, which contains the link that points to the edit.html.erb file, which, currently, does not open and displays the error message.
<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>
EDIT:
– Let me know if you need to see a particular model.rb file. In my models folder I have “subject.rb”, “section.rb”, “section_edit.rb”, “page.rb” and “admin_user.rb” if you need to see any of those. I’m kind of stumped on this one so maybe they’d be useful. They all contain a bunch of schema (has_many, belongs_to, etc) instructions and a few custom console calls.
I made a typo when doing the some of the def/end syntax in the controller. I think it was ‘def create’, I didn’t end it.