首先,我的资源使用 to_param 嵌套在社区模型的slug中。
我在example.com/shop/walmart/topic/14/edit。
如果我在没有验证码输入的情况下按更新,显然应该带我再次使用闪存错误信息再次编辑页面
但是它需要我来example.com/shop/14/topic/14/edit。 < =它采用相同的参数。第一个参数应该是'walmart', community_name ,主题应该是:id 。
所有字段的设置与我在上一页输入的字段相同。
我怎么能避免这个?它应该重定向到与上一页相同的URL。
的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
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
答案 0 :(得分:1)
我没有看到你的控制器动作,也不知道变量的名称,但无论如何,在嵌套路由的情况下,你必须使用命名路由精确定义所有URL,或者使用多态帮助器(就像我一样)。 / p>
所以你的表单助手必须看起来像下一个:
<%= form_for([@community, @community_topic], :html => { :class => 'form-horizontal' }) do |f| %>
必须向/ shop / walmart / topic / 14 / update发送请求(如果@community_topic是新记录,则为'new')
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'.