MKK
MKK

Reputation: 2753

Why it won't detect that the request is come from edit page?

At the edit page, if I leave captcha empty, and press "save button", it takes me to #new page and says 'Wrong Captcha!'. In fact, it should take me to edit page again.
Why my controller won't detect that the request was come from edit page?

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

New Update:

rake routes | 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

Upvotes: 0

Views: 59

Answers (2)

Phobos98
Phobos98

Reputation: 456

@MKK <%= form_for :community_topic, url: community_topic_url(@community_topic), :html => { :class => 'form-horizontal' } do |f| %> but check it within your rake routes

Upvotes: 1

sailor
sailor

Reputation: 8034

Because the request method is post and not put. To use a put request you should write:

<%= form_for @community_topic, url: community_topics_url,

Upvotes: 1

Related Questions