0

我遇到这样的错误:

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"

路线.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部分失败了

有任何想法吗?谢谢。

4

1 回答 1

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
于 2013-09-25T14:59:25.707 回答