views:

15

answers:

1

Hello, I have a _form.html.erb partial that comes from the standard rails 3 template for the model project.

    #view:
    <div id="content">
      <%= link_to 'Edit', edit_project_path(@project), :remote => :true %>
    </div>

    #projects controllers
    def edit
        @project = Project.find(params[:id])

        respond_to do |format|
          format.html # edit.html.erb
          format.js { render "form.js.rjs" }
        end
      end

    #form.js.rjs
    page.replace_html "content", :partial => 'form'

    #_form.html.erb
    <%= form_for(@project) do |f| %>
      <% if @project.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@project.errors.count, "error") %> prohibited this project from being saved:</h2>

          <ul>
          <% @project.errors.full_messages.each do |msg| %>
            <li><%= msg %></li>
          <% end %>
          </ul>
        </div>
      <% end %>
      <p>
        <%= f.label :name %>
        <%= f.text_field :name %>
      </p>

      <div class="actions">
        <%= f.submit %>
      </div>
    <% end %>

While i click that link to edit a project, the form does come through through ajax, but the method for the form is now post instead of put. which means as i submit, it will create new projects with the same attributes as the existing one that I am editing b/c of the create method that's called upon receiving a post request.

I know that form_for(@project) rely on record id to tell whether it's new or not, I looked through all the sources for form_for, form_tag, extras_tags_for_form, form_tag_html but cannot find a place where they specify which method for the form tag is to be used. The closest place where it defines the method is in extra_tags_for_form, but in that method, it is merely sieving through the :method option hash that's already passed to it, but from where is this :method option passed? I cannot find.

any ideas?

A: 

You are on the right track, form_form(obj) will look at the objects dirty flag to figure out what to do with it. That doesn't mean you can't tell it what to do though :)

form_for actually has a few hashes in its optional params, :method lives inside :html. so to force it to be put, just do something like this

form_for @project, :html => {:method => :put} do |f|
Matt Briggs