Question

I have a remote form for updating one of my objects. However, after updating, Rails redirects to another page, essentially as if this form wasn't remote at all. What am I doing wrong?

From the view:

  <td id='translation-<%= base_text.id.to_s %>' class='editable'>
    <span class="translationstring"><%= @translation.content %></span>
    <%= form_for @translation, :remote => true, :html => { :id => ('translation-form-for_' + base_text.id.to_s) } do |f| %>
      <%= text_area(:translation, :content, :rows => 2) %><%= f.hidden_field(:base_text_id) %><%= f.hidden_field(:lang) %>
    <% end %>
  </td>

I have jQuery submit the form using .submit(). Then the controller does:

def update
@translation = Translation.find(params[:id])

respond_to do |format|
  if @translation.update_attributes(params[:translation])
    format.html { redirect_to @translation, notice: 'Translation was successfully updated.' }
    format.json { head :no_content }
  else
    format.html { render action: "edit" }
    format.json { render json: @translation.errors, status: :unprocessable_entity }
  end
end

end

I have also tried an alternative response on the server side which I found online, no change:

format.js { render :js => @translation, :status => :updated, :layout => !request.xhr }

Please advise.

EDIT: this is what my server says for the call:

Started PUT "/translations/1" for 127.0.0.1 at 2012-04-04 09:45:49 +0200
Processing by TranslationsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"WbYiaRgermr2T4wzTPX/ftBsfOB8+
pHCYmhDFJMUu9I=", "translation"=>{"content"=>"Testet Schlüssel wieder.", "base_t
ext_id"=>"1", "lang"=>"de"}, "id"=>"1"}
  ←[1m←[36mTranslation Load (1.0ms)←[0m  ←[1mSELECT `translations`.* FROM `trans
lations` WHERE `translations`.`id` = 1 LIMIT 1←[0m
  ←[1m←[35m (0.0ms)←[0m  BEGIN
  ←[1m←[36m (81.0ms)←[0m  ←[1mUPDATE `translations` SET `content` = 'Testet Schl
üssel wieder.', `updated_at` = '2012-04-04 07:45:49' WHERE `translations`.`id` =
 1←[0m
  ←[1m←[35m (38.0ms)←[0m  COMMIT
Redirected to http://localhost:3000/translations/1
Completed 302 Found in 133ms (ActiveRecord: 120.0ms)
Was it helpful?

Solution

Found a solution: providing :format => :json on my form tag. Maybe jquery_ujs isn't doing its job, though "destroy" links are working fine...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top