1

I am trying to update my photographer model. But how problems with Rails routing and paperclip wont replace image.

When I submit my form the url is: http://localhost:3000/admin/photographers/save.75 which gives an error. Couldn't find Photographer without an ID

And my image is not updated.

My form:

<%= simple_form_for @photographer, :url => save_admin_photographers_path(@photographer), :html => {:multipart => true, :method => :post} do |f| %>
  <%= javascript_include_tag "tiny_mce/tiny_mce" %>
  <%= javascript_include_tag "init_my_tiny_mce" %>

  <%= f.input :name, :label => 'Name' %>
  <%= f.text_area :text, :label => 'Text', :size => '12x12' %>
  <%= f.file_field :image, :label => 'Image' %>
  <% if @photographer %>
    <% if @photographer.image %>
      <p class="ok">
        <label for="dd">image ok</label>
        <%= image_tag("http://s3-eu-west-1.amazonaws.com/kjacobsen/photographer/image/#{@photographer.id}/#{@photographer["image"]}", :size => "39x22") %>
      </p>
      <p>
        <label for="sdd">&nbsp;</label>
        <%= link_to("remove", {:action => "remove_image", :id => @photographer.id}, {:confirm => "Are your sure?"}) %>
      </p>
    <% else %>
      <p class="current">
        <label for="dd"></label>
        No image uploaded for movie
      </p>
    <% end %>
    <br />
  <% end %>

  <br />
  <%= f.file_field :flv, :label => 'Upload FLV' %>
  <br />
  <% if @photographer %>
    <% if @photographer.flv %>
      <p class="ok">
        <label for="dd">flv: <%= @photographer["flv"] %></label>
        <%= link_to("remove", {:action => "remove_flv", :id => @photographer.id}, {:confirm => "Are your sure?"}) %>
      </p>
    <% else %>
      <p class="current">
        No flv uploaded
        <br /><br />
      </p>
    <% end %>
  <% end %>
  <br />
  <%= f.file_field :quicktime, :label => 'Upload Quicktime' %>
  <br />
  <% if @photographer %>
    <% if @photographer.quicktime %>
      <p class="ok">
        <label for="dd">quicktime:  <%= @photographer["quicktime"] %></label>
        <%= link_to("remove", {:action => "remove_quicktime", :id => @photographer.id}, {:confirm => "Are your sure?"}) %>
      </p>
    <% else %>
      <p class="current">
        <label for="dd"></label>
        No quicktime uploaded
        <br />
      </p>
    <% end %>
  <% end %>


  <%= f.button :submit, :value => 'Create movie' %>
<% end %>

My update controller:

def save            
  @photographer = Photographer.find(params[:id])            
  @photographer.update_attributes(params[:photographer])    

  if [email protected]                                
    flash[:notice] = "&nbsp;"
    render_action 'edit'            
  else
    flash[:notice] = "update ok"
    redirect_to :action => 'edit', :id => @photographer.id          
  end       
end 

My routes:

namespace :admin do
  resources :photographers do
    collection do
      post :save
    end
  end
end

1 Answer 1

1

What you're doing is basically an update action. The update route is automatically created when you do resources :photographers, you can verify this by typing rake routes in the terminal.

You should rename your controller action from save to update and remove the custom route:

namespace :admin do
  resources :photographers
end

Then use the update route in your form:

:url => admin_photographer_path(@photographer)

You should also change your html method, the update action uses PUT:

:method => :put

Hope this helps.

3
  • I have tried but get the following when submitting the form: No route matches "/admin/photographers/75" Sep 24, 2011 at 15:09
  • @Railsbeginner what does rake routes says? I also assume your controller name is Admin::PhotographersController? Sep 24, 2011 at 15:10
  • Yes my controller does have that name Sep 24, 2011 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.