在我的rails应用程序中,我为我的声音创建评级。而在评级中,用户可以添加信息,比如他为什么给这个分数等。
我的评分效果很好,但现在我想添加best_in_place信息的编辑输入。
所以在我看来 #Sound 显示
<%= best_in_place @rating, :info, :type => :textarea, :nil => "Ajouter un commmentaire à votre vote !" %>
在我的控制器中: 评级 :
def create
@sound = Sound.find_by_id(params[:sound_id])
@rating = Rating.new(params[:rating])
@rating.sound_id = @sound.id
@rating.user_id = current_user.id
if @rating.save
respond_to do |format|
format.html
format.js
end
end
end
def update
@sound = Sound.find_by_id(params[:sound_id])
@rating = current_user.ratings.find_by_sound_id(@sound.id)
@rating.sound_id = @sound.id
if @rating.update_attributes(params[:rating])
respond_to do |format|
format.html
format.js
end
end
end
但是当我想用best_in_place编辑"信息"时,我有这个错误:
将 id 称为 nil,这将错误地为 4 - 如果你真的想要 NIL 的 ID,使用 object_id
车道是:
@rating = current_user.ratings.find_by_sound_id(@sound.id)
我希望你理解我的问题,你可以帮助我完成in_place_editing工作。
谢谢
我认为最好的 gem 使用 ajax,所以你需要向控制器添加一些代码。文档显示,该 gem 附带了一个用于响应 json 的控制器帮助程序。
respond_to do |format|
if @rating.update_attributes(params[:rating])
format.html { redirect_to(@sound, :notice => 'Rating was successfully updated.') }
format.json { respond_with_bip(@rating) }
else
format.html { render :action => "edit" }
format.json { respond_with_bip(@rating) }
end
end
您可能还需要将 jQuery 初始值设定项添加到文档的任何地方:
$(document).ready(function() {
jQuery(".best_in_place").best_in_place();
});
希望有效!另外,这是我有史以来的第一个堆栈溢出答案!