我到处寻找解决方案,但是这个问题的所有常见解决方案都不适用于我。任何帮助是极大的赞赏。

我的问题只是 rails best_in_place gem 在我进行更改后不会保存我的任何数据。它将允许我输入新的更改,但在我按下 Enter 后它不会更新数据库(如果我刷新页面,它也不起作用,这是这个 gem 的 RailsCast 上的初始问题)。

Controller 代码:

def update
respond_to do |format|
  if @renter.update_attributes(renter_params)
    format.json { render json: @renter }
    flash[:success] = "Profile updated"
    redirect_to @renter
  else

    render 'edit'
  end

end

结尾

查看页面代码
    <li><span class="content"><b>Name: </b><%= best_in_place @renter, :name %></span></li>

提前致谢!

编辑:从 Controller 添加renter_params
def renter_params
    params.require(:renter).permit(:name, :email, :password, :password_confirmation)
end

(注意:我从来没有遇到过renter_params 本身的问题,我的更新总是保存在数据库中)

编辑:从 javascripts/application.js 添加代码
//= require jquery
//= require best_in_place
//= require best_in_place.purr
//= require jquery_ujs
//= require jquery.purr
//= require bootstrap
//= require turbolinks
//= require_tree .

租客.js.coffee
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
jQuery ->
    $('.best_in_place').best_in_place()

最佳答案

如果您还没有,请将您的 gem 修改为:

gem 'best_in_place', github: 'bernat/best_in_place'

简单地做 gem 'best_in_place' 对我不起作用。

如果这不起作用,正如 documentation 建议的那样,替换:
format.json { render json: @renter }

和:
format.json { respond_with_bip(@renter) }
respond_with_bip 是“您应该在 Controller 中使用的实用方法,以便使用 :json 格式提供 javascript 端预期的响应”,文档说。我不确定这有多重要,因为我的 Rails 4 应用程序在没有它的情况下工作。

最后,如果 update 操作仅限于登录用户,请确保您已登录。

关于ruby-on-rails - Rails best_in_place gem 不更新数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23507470/

10-09 20:28
查看更多