相信你能帮到我。
我正在尝试向遗留代码(错字)添加新功能。但似乎路由有一些问题。
在项目中路由是通过以下方式生成的:
%w{advanced cache categories comments content profiles feedback general pages
resources sidebar textfilters themes trackbacks users settings tags redirects seo post_types }.each do |i|
match "/admin/#{i}", :to => "admin/#{i}#index", :format => false
match "/admin/#{i}(/:action(/:id))", :to => "admin/#{i}", :action => nil, :id => nil, :format => false
end
我的功能是关于合并文章。为此,我在/admin/content控制器中添加了新操作:
def merge
#some code here
end
我添加的一部分视图(_form.html.erb):
<% if current_user.admin? and !@article.id.nil?%>
<div class=''>
<h4><%= _("Merge Articles") %></h4>
<%= label_tag :merge_with, 'Article ID' %><%= text_field_tag :merge_with, nil, :size => 20 %>
<%= button_to 'Merge', admin_content_merge_path(:id => @article.id) %>
</div>
<%end%>
此部分由另一个部分 (_edit.html.erb) 呈现
<%= form_tag(form_action, :id => "#{form_type}_form", :enctype => "multipart/form-data", :class => className) do %>
<%= render :partial => "form" %>
<% end %>
最后 _edit.html.erb 由视图 new.html.erb 渲染
<%= render "admin/shared/edit", { :form_type => "article", :form_action => { :action => "new", :id => @article.id , :class => ('autosave')} } %>
问题是如何为上面的控制器操作编写正确的路由,这将允许我呈现包含新合并文章的编辑页面。我写:
match "/admin/content/merge/:id" => "admin/content#merge",:as => 'admin/content/merge'
耙路线输出:
admin_content_merge /admin/content/merge/:id(.:format) {:controller=>"admin/content", :action=>"merge"}
但正如我所见,正在调用新的或编辑操作。
显然,我的路线是错误的,不是吗?
你能帮我解决这个问题吗?
提前致谢!
更新
最新的new.html.erb:
<% @page_heading = _('New article') %>
<%= render "admin/shared/edit", { :form_type => "article", :form_action => { :action => "new", :id => @article.id , :class => ('autosave')} } %>
<% if current_user.admin? and !@article.id.nil?%>
<%= form_tag "/admin/content/merge/#{@article.id}" do %>
<h4><%= _("Merge Articles") %></h4>
<%= label_tag :merge_with, 'Article ID' %>:
<%= text_field_tag :merge_with %><br />
<%= submit_tag "Merge" %>
<% end %>
<% end %>