2013-01-11 133 views
1

我正在嘗試在我的rails 3應用程序中使用jeditable。我想編輯一些內聯字段。jEditable in 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' 

有沒有人對如何解決這一線索?

回答

2
<script> 
jQuery(document).ready(function($) { 
    $(".edit_textfield").each(function() {  
     $(this).editable('<%[email protected]%>', { 
      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和它開始工作:)

0

jeditable的第一個參數(在你的情況下,「更新」)是它發佈到的URL。默認情況下,您需要在「顯示」網址(即學生/ 19)上張貼以更新記錄。我還會從錯誤消息中猜測,您是在單獨的資源頁面(「myapp/students」)上,因此它會發布到「myapp/students/update」。

如果您已經在記錄顯示頁面上,則發佈到當前窗口位置應該可以修復它。如果你在索引頁面上,你需要編寫一些JavaScript來確定你應該發佈哪條記錄。