【问题标题】:jEditable in Rails 3j在 Rails 3 中可编辑
【发布时间】:2013-01-11 04:49:51
【问题描述】:

我正在尝试将 jeditable 与我的 rails 3 应用程序一起使用。我想内联编辑一些字段。

我的看法

<script>
    jQuery(document).ready(function($) {

$(".edit_textfield").each( function() {    
      $(this).editable('update', {
            type        :'textarea',
            cancel      :'Cancel',
            submit      :'OK',
            indicator   :'Saving...',
            tooltip     :'Click to edit...',
            rows        :10,
            method      :"PUT",
            submitdata  :{id: $(this).attr('id'), name:$(this).attr('name') }
        });
});
});
</script>

<p id="notice"><%= notice %></p>

<p>
  <b><%=t :Name%>:</b>
  <dd class="edit_textfield" id="<%= @student.id %>" name="name"><%= @student.name %></dd>
</p>

<p>
  <b><%=t :Age%>:</b>
  <dd class="edit_textfield" id="<%= @student.id %>" name="age"><%= @student.age %></dd>
</p>

<p>
  <b><%=t :Address%>:</b>
  <dd class="edit_textfield" id="<%= @student.id %>" name="address"><%= @student.address %></dd>
</p>

<p>
  <b><%=t :Phone%>:</b>
  <dd class="edit_textfield" id="<%= @student.id %>" name="phone"><%= @student.phone %></dd>
</p>

我的控制器:

def update
    @student = Student.find(params[:id])
    @student.name = params[:value]
    @student.age = params[:value]
    @student.address = params[:value]
    @student.phone = params[:value]
    @student.save
    respond_to do |format|
      if @student.update_attributes(params[:student])
        format.html { redirect_to @student, :notice => 'Student was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render :action => "edit" }
        format.json { render :json => @student.errors, :status => :unprocessable_entity }
      end
    end
  end

错误

    Processing by StudentsController#update as HTML
  Parameters: {"id"=>"update", "value"=>"Sreekesh O S", "name"=>"name"}
WARNING: Can't verify CSRF token authenticity
  Student Load (1.6ms)  SELECT `students`.* FROM `students` WHERE `students`.`id` = 0 LIMIT 1
Completed 500 Internal Server Error in 6ms

ActiveRecord::RecordNotFound (Couldn't find Student with id=update):
  app/controllers/students_controller.rb:59:in `update'

有人知道如何纠正这个问题吗?

【问题讨论】:

    标签: jquery ruby-on-rails ruby-on-rails-3 jeditable


    【解决方案1】:
    <script>
    jQuery(document).ready(function($) {
       $(".edit_textfield").each( function() {    
          $(this).editable('<%=@studet.id%>', {
                type        :'textarea',
                cancel      :'Cancel',
                submit      :'OK',
                indicator   :'Saving...',
                tooltip     :'Click to edit...',
                rows        :10,
                method      :"PUT",
                submitdata  :{id: $(this).attr('id'), name:$(this).attr('name') }
            });
        });
    });
    </script>
    

    我像这样编辑了 javascript,它开始工作了 :)

    【讨论】:

      【解决方案2】:

      jeditable 的第一个参数(在您的情况下为“update”)是它发布到的 URL。默认情况下,在 Rails 中,您希望发布到“显示”网址(即,students/19)以更新记录。从错误消息中,我还猜测您位于单个资源页面(“myapp/students”)上,因此它发布到“myapp/students/update”。

      如果您已经在记录显示页面上,那么发布到当前窗口位置应该可以解决它。如果您在索引页面上,则需要编写一些 javascript 来确定您应该发布到哪条记录。

      【讨论】:

        猜你喜欢
        • 2011-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-25
        • 2023-03-13
        • 1970-01-01
        • 2011-05-21
        • 1970-01-01
        相关资源
        最近更新 更多