2012-12-27 49 views
1

首先,我的資源使用to_param嵌套在Community模型中。爲什麼我的代碼帶我到不同的URL?

我在example.com/shop/walmart/topic/14/edit。
如果我按更新沒有captcha輸入,它顯然應該帶我回到編輯頁面與閃光錯誤信息。
但是,我需要example.com/shop/14/topic/14/edit。 < =它採用相同的參數。它應該採取'沃爾瑪'社區名稱第一個參數,和:ID爲主題。
所有字段都與我在前一頁輸入的內容相同。 我該如何避免這種情況?它應該重定向到與上一頁相同的網址。

的routes.rb

resources :communities, :path => "shops", do 
    resources :community_topics, :path => "topics"  
end 

控制器

def simple_captcha_check 
    if !simple_captcha_valid? 
     flash[:error] = 'Wrong Captcha!' 

     if request.put? # We came from an edit request 
      @community_topic = CommunityTopic.find(params[:id]) 
      @community_topic.attributes = params[:community_topic] 
      render :action => :edit 
     elsif request.post? # We came from a new request 
      @community_topic = CommunityTopic.new params[:community_topic] 
      render :action => :new 
     end 

    end 
end 

型號/ community.rb請注意,我用的蛞蝓這裏

def to_param 
    "#{community_name}" 
end 

的意見/ community_topics/_form.html.erb

<%= form_for @community_topic, :html => { :class => 'form-horizontal' } do |f| %> 
    <div class="control-group"> 
    <%= f.label :title, :class => 'control-label' %> 
    <div class="controls"> 
     <%= f.text_field :title, :class => 'text_field' %> 
    </div> 
    </div> 
    <div class="control-group"> 
    <%= f.label :body, :class => 'control-label' %> 
    <div class="controls"> 
     <%= f.text_area :body, :class => 'text_area' %> 
    </div> 
    </div> 

    <div class="control-group"> 
     <div class="controls"> 
    <%= show_simple_captcha(:label => "human authentication") %> 
    </div> 
    </div> 



    <div class="form-actions"> 
    <%= f.submit nil, :class => 'btn btn-primary' %> 
    <%= link_to t('.cancel', :default => t("helpers.links.cancel")), 
       community_topic_index_path, :class => 'btn' %> 
    </div> 
<% end %> 

耙路線| grep的community_topic

 community_community_topics GET /shops/:community_id/topics(.:format)   community_topics#index 
           POST /shops/:community_id/topics(.:format)   community_topics#create 
    new_community_community_topic GET /shops/:community_id/topics/new(.:format)  community_topics#new 
    edit_community_community_topic GET /shops/:community_id/topics/:id/edit(.:format) community_topics#edit 
     community_community_topic GET /shops/:community_id/topics/:id(.:format)  community_topics#show 
           PUT /shops/:community_id/topics/:id(.:format)  community_topics#update 
           DELETE /shops/:community_id/topics/:id(.:format)  community_topics#destroy 

順便說一句,在我的控制指標動作就是這樣的,它的工作好!

community_topics_controller.rb #INDEX

def index 
    @community = Community.find_by_community_name(params[:community_id]) 
    @community_topics = @community.community_topics 

    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render json: @community_topics } 
    end 
    end 

回答

1

我沒有看到你的控制器動作,不知道的變量的名稱,但無論如何,在嵌套路線的情況下,你必須精確定義的所有URL與命名路線,或與多態輔助(如我)。

所以你的形式助手必須尋找在明年:

<%= form_for([@community, @community_topic], :html => { :class => 'form-horizontal' }) do |f| %> 

它必須發送請求/店/沃爾瑪/主題/ 14 /更新(或「新」如果@community_topic是一個新的記錄)

community.rb:

you can just 

def to_param 
    community_name 
end 

的routes.rb:

resources :communities, :path => "shop", do 
    resources :community_topics, :path => "topic"#, :as => :'topic' * 
end 

# * named route 'community_topic' can conflict with 'community_topics' of standalone route for CommunityTopic. Let it be by default: 'community_community_topic'. 
+0

謝謝,但它仍然帶我example.com/shop/14/topic/14/edit – MKK

+0

試圖從路線 –

+0

擦除:as =>:'主題'好吧。我會試一試 – MKK

相關問題