Rails how to update image? And how to do RESTful routing?
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"> </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 开发者_JAVA百科
@photographer = Photographer.find(params[:id])
@photographer.update_attributes(params[:photographer])
if !@photographer.save
flash[:notice] = " "
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
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.
精彩评论