1

首先,我的资源在社区模型中 使用to_param嵌套在 slug 中。

我在 example.com/shop/walmart/topic/14/edit 。
如果我在没有验证码输入的情况下按更新,显然应该让我再次返回编辑页面并显示 Flash 错误消息。
然而,它带我去 example.com/shop/14/topic/14/edit 。<= 它采用相同的参数。它应该采用 'walmart' ,即community_name作为第一个参数,:id作为主题。
所有字段的设置都与我在上一页输入的内容相同。我怎样才能避免这种情况?它应该重定向到与上一页相同的 url。

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

models/community.rb请注意,我在这里使用 slug

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
4

1 回答 1

1

我看不到您的控制器操作,也不知道变量的名称,但无论如何,在嵌套路由的情况下,您必须使用命名路由或多态助手(就像我一样)精确定义所有 url。

因此,您的表单助手必须如下所示:

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

它必须向 /shop/walmart/topic/14/update 发送请求(如果 @community_topic 是新记录,则为“new”)

社区.rb:

you can just

def to_param
  community_name
end

路线.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'.
于 2012-12-27T16:32:31.770 回答