Jump to content

Rails 3 remote form processing as HTML

- - - - -

  • Please log in to reply
No replies to this topic

#1
aguswgs

aguswgs

    Newbie

  • Members
  • Pip
  • 1 posts
I'm sure I'm missing something simple. I want to simply update a record in the background without rendering anything or changing the page, but my remote form passes data as HTML, not JS.

In the view:

<%= form_for event, :remote => true do |f| %>

      <p class="bottom small"><%= f.select :status, Event::STATUSES, { :prompt => 'Mark as:' }, :class => 'status_update' %></p>

    <% end %>

Then, my JQuery to submit form on change:

<script type="text/javascript" charset="utf-8">

  $(function() {

    $('.status_update').live('change', function() {

      $(this).parents('form:first').submit();

    });

  });

</script>

In my controller:

def update

    @event = Event.find(params[:id])

    @event.attributes = params[:event]

    parse_time_and_duration if params[:starts]

    @event.save

    

    respond_to do |format|

      format.html

      format.json do

        render :json => { :id => @event.id, :title => @event.display_title }

      end

    end

  end

When I do this, I get a missing template error:

Missing template events/update with {:formats=>[:html] ...

Because the controller is trying to process it as HTML, not as Javascript. I thought that was what :remote => true was for? If not, what's the point?

So I found I can get around this by explicitly adding :format => :json to my form_for:

<%= form_for event, :format => :json, :remote => true do |f| %>

But can anyone tell me why I have to do that? What am I missing?

Edited by Roger, 13 June 2011 - 02:29 PM.
removed spam link





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users