【问题标题】:Rails best_in_place gem not updating dataRails best_in_place gem 不更新数据
【发布时间】:2014-06-23 19:41:45
【问题描述】:

我到处寻找解决方案,但这个问题的所有常见解决方案都不适用于我。非常感谢任何帮助。

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

控制器代码:

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>  

提前致谢!

编辑:从控制器添加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 .

renters.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()

【问题讨论】:

  • 这里的renter_params是什么?在您设置之前,@renter 也不太可能包含任何内容。请参阅github.com/bernat/best_in_place 上的示例
  • 上次提交是一年前,有 148 个问题,可能不适用于 rails 4。
  • best_in_place gem 在我的 Rails 4 上运行良好。在没有 best_in_place 的情况下,您的更新操作是否有效?
  • 另外,如果您需要登录才能执行更新,如果您还没有登录,它将不会让您更新。
  • 感谢您的快速回复 :) 添加了renter_params 以获得更多上下文。 Wali - 我也是这么想的,但我只是在编辑我当前登录的用户。 Dsatch - 抱歉,我没有提供完整的上下文,但 @renter 的初始化如下:@renter=Renter.find(params[:id]) ...这是代码中的其他地方,因为它是经过过滤的,但它已初始化为正确的用户

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


【解决方案1】:

如果您还没有,请按以下方式修改您的 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 是一种“您应该在控制器中使用的实用程序方法,以便使用 :json 格式提供 javascript 端所期望的响应”,文档说。我不确定这有多重要,因为我的 Rails 4 应用程序在没有它的情况下也能正常工作。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 2014-02-06
    相关资源
    最近更新 更多