0

在编辑页面,如果我将验证码留空,然后按“保存按钮”,它会将我带到#new 页面并显示“验证码错误!”。事实上,它应该让我再次编辑页面。
为什么我的控制器不会检测到请求来自编辑页面?

社区主题控制器.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 %>

更新:

路线.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
4

2 回答 2

1

@MKK<%= form_for :community_topic, url: community_topic_url(@community_topic), :html => { :class => 'form-horizontal' } do |f| %>但在您的 rake 路线中检查它

于 2012-12-27T15:09:45.770 回答
1

因为请求方法是post而不是put。要使用 put 请求,您应该编写:

<%= form_for @community_topic, url: community_topics_url,
于 2012-12-27T14:19:01.493 回答