【发布时间】:2016-03-19 08:45:48
【问题描述】:
这是我遇到的错误
主题中的 NoMethodError#edit
显示 /home/ktutor/Documents/simple_code/app/views/subjects/edit.html.erb 其中第 7 行提出: nil:NilClass 的未定义方法“id” 提取的源代码(第 7 行附近):
<h2>Update Subject</h2>
<%= form_for(:subject, :url => {:action => 'update', :id => @subjects.id}) do |f| %>
<table summary="Subject form fields">
<tr>
Rails.root:/home/ktutor/Documents/simple_code 应用程序跟踪 |框架跟踪 |全跟踪 app/views/subjects/edit.html.erb:7:in `_app_views_subjects_edit_html_erb__3163959245167106416_44757760'
Request
Parameters:
{"id"=>"6"}
这是我的主题控制器
class SubjectsController < ApplicationController
def index
list
render('list')
end
def list
@subjects = Subject.order("subjects.position ASC")
end
def show
@subject = Subject.find(params[:id])
end
def new
@subject = Subject.new(:Name => "")
end
def create
#new_position = params[:subject].delete(:position)
# Instantiate a new object using form parameters
@subject = Subject.new(subject_params)
# Save the object
if @subject.save
@subject.move_to_position(new_position)
# If save succeeds, redirect to the list action
flash[:notice] = "Subject Created."
redirect_to(:action => 'list')
else
# If save fails, redisplay the form so user can fix problems
@subject_count = Subject.count +1
render('new')
end
end
private
def subject_params
allow = [:name, :position, :visible, :created_at, :updated_at]
params.require(:subject).permit(allow)
end
def edit
@subject = Subject.find(params[:id])
end
def update
# find a new object using form parameters
@subject = Subject.find.(params[:id])
# Save the object
if @subject.update_attributes(params[:subject])
# If save update, redirect to the list action
redirect_to(:action => 'show', :id => @subject.id)
else
# If save fails, redisplay the form so user can fix problems
render('edit')
end
end
def edit
@subject = Subject.find.(params[:id])
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>
<div class="form-buttons">
<%= submit_tag("Update Subject") %>
</div>
<% end %>
</div>
【问题讨论】:
标签: ruby-on-rails