2010-10-23 21 views
0

我有一個名爲公司模式,並在顯示視圖有以下幾點:?我怎樣才能讓標籤編輯在線(或submittable,而無需進入「編輯」模式示範

<div id = 'tags'> 
    <strong>Tags:</strong> 
    <% unless @company.tag_list.empty? %> 
    <%= @company.tag_list %> 
    <% end %> 
    <% form_remote_tag(:url => {:action => 'update'}, 
        :update => 'tags') do %> 
    <%= text_field :company, :tag_list %> 
     <%= submit_tag 'Save' %> 
    <% end %> 
</div> 

我我使用acts_as_taggable_on寶石

這是公司控制器更新方法:

def update 
    @company = Company.find(params[:id]) 
    if @company.update_attributes(params[:company]) 
     flash[:notice] = "Successfully updated company." 
     redirect_to @company 
    else 
     render :action => 'edit' 
    end 
    end 

我想我需要的結果是,我可以添加標籤,看到他們通過AJAX添加,都沒有東東ding來編輯模型,但是從show View看,就像你可以在Wordpress中添加標籤的方式一樣。

UPDATE:這是我的錯誤(它看起來像它沒有使用更新操作:

POST http://localhost:3000/companies/10 
No action responded to 10. Actions: create, destroy, edit, email_this_week, index, new, show, and update 

嗯,不知道爲什麼它是這樣做....它應該做更新動作?,右做我需要做的東西在我的路線

回答

0

你快到了。

首先創建一個局部tags呈現您的代碼和您的Ajax的形式(即你在你的問題顯示)。

那麼你的控制器方法中這樣寫:

def update 
    @company = Company.find(params[:id]) 
    if request.xhr? 
    # add the given tag to the company 
    @company.tags << params[:company][:taglist] 
    @company.save 
    render :partial => 'tags' 
    else 
    if @company.update_attributes(params[:company]) 
     flash[:notice] = "Successfully updated company." 
     redirect_to @company 
    else 
     render :action => 'edit' 
    end 
    end 
end 

我該怎麼辦:我測試,如果該請求是一個Ajax請求(xhr?),然後嘗試更新的,這是鑑於該領域的標籤:你將不得不糾正我猜的代碼。我假設只是做update_attributes不起作用,因爲你會想要添加一個標籤到現有標籤列表。如果成功,則由於您在表單中指定的選項update,您將呈現將替換前一個的部分。

這應該讓你開始。

爲了進一步提高它,你應該有一個鏈接添加到您的每一個標籤的圖像(紅十字會)的刪除指定代碼。

+0

謝謝!我對.xhr不熟悉?方法...在2.3.5中工作?是的,我有一種感覺,更新不起作用....讓我玩這個,所以我把視頻中的標記代碼,並使其部分? – Angela 2010-10-24 18:53:20

+0

這是非常接近的 - 需要對它進行調整,也許還有一個問題,即我的實例路由的方式。 – Angela 2010-11-02 04:34:05

相關問題