2011-09-24 57 views
1

我正在嘗試更新我的攝影師模型。但是,如何解決Rails路由和回形針問題不會取代圖像。Rails如何更新圖像?以及如何進行RESTful路由?

當我提交我的表單的網址是:http://localhost:3000/admin/photographers/save.75這給出了一個錯誤。 Couldn't find Photographer without an ID

而且我的圖像沒有更新。

我的形式:

<%= 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 %> 

我的更新控制器:

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 

我的路線:

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

回答

1

你在做什麼基本上是一個update行動。當您執行resources :photographers時,會自動創建更新路由,您可以通過在終端中鍵入rake routes來驗證此更新路由。

你應該從save你的控制器動作重命名爲update和刪除自定義路線:

namespace :admin do 
    resources :photographers 
end 

然後使用update路線的形式:

:url => admin_photographer_path(@photographer) 

你也應該改變你的HTML方法,更新操作使用PUT

:method => :put 

希望這會有所幫助。

+0

我試過了,但在提交表單時得到以下內容:沒有路由匹配「/ admin/photographers/75」 –

+0

@Railsbeginner'rake routes'說什麼?我也假設你的控制器名稱是'Admin :: PhotographersController'? –

+0

我的耙路線:http://pastie.org/2584598 –

相關問題