2012-12-27 29 views
0

在編輯頁面,如果我將captcha留空,並按下「保存按鈕」,它會將我帶到#new頁面並顯示「錯誤驗證碼!」。實際上,我應該再次編輯頁面。
爲什麼我的控制器無法檢測到請求來自編輯頁面?爲什麼它不會檢測到請求來自編輯頁面?

comunity_topics_controller.rb

before_filter :simple_captcha_check, :only => [:update, :create] 


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 

_form.html.erb

<%= form_for :community_topic, url: community_topic_index_url, :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 %> 

UPDATE:

的routes.rb

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

新更新:

耙路線| grep community_topic

  community_topic_index GET /shop/:community_id/topic(.:format)   community_topics#index 
           POST /shop/:community_id/topic(.:format)   community_topics#create 
      new_community_topic GET /shop/:community_id/topic/new(.:format)  community_topics#new 
      edit_community_topic GET /shop/:community_id/topic/:id/edit(.:format) community_topics#edit 
       community_topic GET /shop/:community_id/topic/:id(.:format)  community_topics#show 
           PUT /shop/:community_id/topic/:id(.:format)  community_topics#update 
           DELETE /shop/:community_id/topic/:id(.:format)  community_topics#destroy 

回答

1

因爲請求方法是post和not put。使用PUT請求,你應該寫:

<%= form_for @community_topic, url: community_topics_url, 
+0

謝謝!如果我這樣做,它會顯示路由錯誤 按下'更新按鈕'後,沒有路由匹配[PUT] – MKK

+0

您可以粘貼您的routes.rb文件嗎? – sailor

+0

我剛更新了。我認爲':因爲'正在搞亂我的道路,但我想用它很糟糕。 – MKK

1

@MKK <%= form_for :community_topic, url: community_topic_url(@community_topic), :html => { :class => 'form-horizontal' } do |f| %>但你的耙路內檢查

+0

謝謝!現在它返回**路由錯誤 沒有路由匹配{:action =>「show」,:controller =>「community_topics」,:community_id =># MKK

+0

正在使用具有自定義**:路徑的嵌套資源路由, :像這樣**這麼硬的東西就像這樣? – MKK

相關問題