【问题标题】:Using Best_in_Place to edit attributes使用 Best_in_Place 编辑属性
【发布时间】:2012-03-25 17:34:54
【问题描述】:

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

<%=best_in_place @student, :name =>

但是,我无法让它更新教育的属性.. 使用类似

的行
<%=best_in_place @education, :college =>

在学生/节目看来, 我收到错误
在 2012-03-25 13:06:57 -0400 开始为 127.0.0.1 放置“/educations”

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

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

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

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

【问题讨论】:

  • 您找到解决方案了吗?我也遇到了同样的问题

标签: ruby-on-rails ruby ruby-on-rails-3 best-in-place


【解决方案1】:

两件事:

  1. 您的视图语法已关闭。您将使用 =&gt; 而不是 %&gt; 关闭标签。所以试试这个:

    &lt;%=best_in_place @student, :name %&gt;

    &lt;%=best_in_place @education, :college %&gt;

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

See ReadMe for best_in_place Controller response with 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

【讨论】:

    猜你喜欢
    • 2016-06-02
    • 2011-08-01
    • 2012-12-16
    • 2013-03-06
    • 2014-02-05
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多