Okky
Okky

Reputation: 10466

jEditable in Rails 3

I'm trying to use jeditable with my rails 3 apps. I would like to edit some fields inline.

My view

<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>

my controller:

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

Error

    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'

Does anyone have a clue on how to correct this?

Upvotes: 2

Views: 370

Answers (2)

Okky
Okky

Reputation: 10466

<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>

I edited the javascript like this and it started working :)

Upvotes: 2

Joe Pym
Joe Pym

Reputation: 1836

The first argument to jeditable (in your case, "update") is the URL that it posts to. By default in rails, you want to be posting to the "show" url (ie, students/19) in order to update the record. I would also guess, from the error message, that you are on a singular resource page ("myapp/students"), and therefore it is posting to "myapp/students/update".

If you are already on the record show page, then posting to the current window location should fix it. If you are on the index page, you'll need to write a bit of javascript to determine which record you should be posting to.

Upvotes: 0

Related Questions