1

我正在使用 Best_in_Place gem 使我网站上的区域可编辑。我有两个模型:学生和教育,关系是每个学生都有_许多教育。在编辑直接在学生模型中的属性时,我可以使用 Best_in_place 功能,例如

<%=best_in_place @student, :name =>

但是,我无法用它来更新教育的属性。

<%=best_in_place @education, :college =>

在学生/节目的视图中,我
在 2012-03-25 13:06:57 -0400 收到错误 Started PUT "/educations" for 127.0.0.1

ActionController::RoutingError (没有路由匹配 [PUT] "/educations")

不仅不起作用,可编辑的位置也完全消失了。我不知道是什么导致了问题,两个模型/控制器的一切似乎都是一样的。我的路线很简单:

resources :students
resources :educations
root :to => 'pages#home'
devise_for :students

我的控制器也是如此:

def update
@student = Student.find(params[:id])

respond_to do |format|
  if @student.update_attributes(params[:student])
    format.html { redirect_to @student, notice: 'Student was successfully updated.' }
    format.json { head :no_content }
  else
    format.html { render action: "edit" }
    format.json { render json: @student.errors, status: :unprocessable_entity }
  end
end
end

对比

def update
@education = Education.find(params[:id])
respond_to do |format|
  if @education.update_attributes(params[:education])
    format.html { redirect_to @education, notice: 'Education was successfully updated.' }
    format.json { head :no_content }
  else
    format.html { render action: "edit" }
    format.json { render json: @education.errors, status: :unprocessable_entity }
  end
 end
end

如果我耙路线,我会得到:

educations GET    /educations(.:format)               educations#index
                          POST   /educations(.:format)               educations#create
            new_education GET    /educations/new(.:format)           educations#new
           edit_education GET    /educations/:id/edit(.:format)      educations#edit
                education GET    /educations/:id(.:format)           educations#show
                          PUT    /educations/:id(.:format)           educations#update
                          DELETE /educations/:id(.:format)           educations#destroy
students GET    /students(.:format)                 students#index
                          POST   /students(.:format)                 students#create
              new_student GET    /students/new(.:format)             students#new
             edit_student GET    /students/:id/edit(.:format)        students#edit
                  student GET    /students/:id(.:format)             students#show
                          PUT    /students/:id(.:format)             students#update
                          DELETE /students/:id(.:format)             students#destroy

任何指针都会有很大的帮助。

4

1 回答 1

0

两件事情:

  1. 您的视图语法已关闭。您正在使用=>而不是关闭标签%>。所以试试这个:

    <%=best_in_place @student, :name %>

    <%=best_in_place @education, :college %>

  2. 尝试更新to的respond_to块。这应该使用户能够通过 javascript 进行编辑,同时让用户留在页面上以通过 AJAX 查看他们的更新。format.jsonrespond_with_bip(@student)

请参阅自述文件以了解 best_in_place 控制器响应和 respond_with_bip

请参阅下面的更新:

def update
  @student = Student.find(params[:id])

  respond_to do |format|
    if @student.update_attributes(params[:student])
      format.html { redirect_to @student, notice: 'Student was successfully updated.' }
      format.json { respond_with_bip(@student) }
    else
      format.html { render action: "edit" }
      format.json { render json: @student.errors, status: :unprocessable_entity }
    end
  end
end



def update
  @education = Education.find(params[:id])

  respond_to do |format|
    if @education.update_attributes(params[:education])
      format.html { redirect_to @education, notice: 'Student was successfully updated.' }
      format.json { respond_with_bip(@education) }
    else
      format.html { render action: "edit" }
      format.json { render json: @education.errors, status: :unprocessable_entity }
    end
  end
end
于 2012-07-30T01:43:16.607 回答