【发布时间】: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_placegem 在我的 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