我遇到这样的错误:
Started PUT "/campuses/2" for 127.0.0.1 at 2013-09-25 22:42:45 +0800
Processing by CampusesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"zTMjVJYM47hBin6eUK1zz5Axu0h2xjNHrJgEnvXOvYU=", "campus"=>{"name"=>"University of Texas", "custom_exercises_enabled"=>"true"}, "commit"=>"Submit", "id"=>"2"}
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
Completed 500 Internal Server Error in 2ms
NoMethodError (undefined method `update_attributes' for nil:NilClass):
app/controllers/campuses_controller.rb:121:in `update'
计划在不同的页面中构建edit campus
。
我在home/_campus.html.erb
中有一个选择框,其中有一个连接的change
函数,可以调用AJAX get
函数。
<%= select_tag :campus_id, options_from_collection_for_select(Campus.order('name'), 'id', 'name'), class:"all-campus" %>
home.js.coffee
$(".all-campus").change ->
$.get "/campuses/" + @value + "/form_partial"
的routes.rb
resources :campuses do
get :form_partial, on: :member
end
然后,在 campuses_controller.rb
中 def edit
@campus = Campus.find(params[:campus][:id])
end
def form_partial
@campus = Campus.find params[:id]
respond_to :js
end
def update
if @campus.update_attributes(params[:campus])
flash[:success] = 'Campus Information updated!'
redirect_to @campus
else
render :edit
end
end
form_partial.js.erb
$("#campus-view").html("<%= j render 'campuses/update_campus', campus: @campus %>");
而且,要呈现的部分是 _update_campus.html.erb
<h3><%= @campus.name %></h3>
<%= form_for(@campus,method: "put") do |c| %>
<%= c.text_field :name %>
<table cellpadding="8" cellspacing="0">
<tbody>
<tr>
<td><span>Custom Team Exercises</span></td>
<td><span><%= c.select :custom_exercises_enabled, [["Enabled",true],["Disabled",false]], {}, {class:"custom_select"} %> </span></td>
</tr>
</tbody>
</table>
<br/>
<%= c.submit "Submit" %>
<% end %>
但是在提交表单时,它在update_attributes
部分
有什么想法吗?感谢。
答案 0 :(得分:0)
您未在@campus
行动中设置update
。尝试:
def update
@campus = Campus.find params[:id] # You're missing this line
if @campus.update_attributes(params[:campus])
flash[:success] = 'Campus Information updated!'
redirect_to @campus
else
render :edit
end
end