【问题标题】:best_in_place error id for nilbest_in_place 错误 ID 为 nil
【发布时间】:2013-11-08 19:14:33
【问题描述】:

在我的 rails 应用程序中,我为我的声音评分创建评分。在评分中,用户可以添加信息,例如他为什么给这个评分等。

我的评分效果很好,但现在我想用 best_in_place 添加信息的编辑输入。

所以在我看来#Sound show

 <%= 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 编辑“信息”时,我有这个错误:

为 nil 调用 id,它会错误地为 4——如果你真的想要的话 nil的id,使用object_id

车道是:

@rating = current_user.ratings.find_by_sound_id(@sound.id)

我希望你能理解我的问题,并且可以帮助我进行 in_place_editing 工作。

谢谢

【问题讨论】:

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


    【解决方案1】:

    我认为最好的 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();
    });
    

    希望有效!此外,这是我的第一个堆栈溢出答案!

    【讨论】:

    • 我尝试了您的解决方案,但出现了同样的问题
    • 尝试保留您的旧代码并将 format.js 更改为 format.json。另外,看看你是否能在this railscast 中找到任何东西。您可能需要添加一个 jQuery 初始化程序。
    猜你喜欢
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2016-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多