我正在尝试将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'
有人知道如何纠正这一点吗?
<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,它开始工作:(
jeditable的第一个参数(在您的情况下,是"update"(是它发布到的URL。默认情况下,在rails中,您希望发布到"show"URL(即students/19(以更新记录。我还想,从错误消息中,你在一个单一的资源页面上("myapp/students"(,因此它发布到"myapp/situdents/update"。
如果你已经在记录显示页面上了,那么发布到当前窗口位置应该会解决这个问题。如果你在索引页面上,你需要写一点javascript来确定你应该发布到哪个记录。