Domanda

Believe you can help me.

I'm trying to add new functionality to legacy code (Typo). But it seems that there is some problem about routing.

In the project routes are generated the following way:

%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

My functionality is about merging articles. For that I've added new action in the /admin/content controller:

def merge
#some code here
end

A piece of a view partial (_form.html.erb) added by me:

  <% 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%>

This partial is rendered by another partial (_edit.html.erb)

<%= form_tag(form_action, :id => "#{form_type}_form", :enctype => "multipart/form-data", :class => className) do %>

  <%= render :partial => "form" %>

<% end %>

And finally _edit.html.erb is rendered by view new.html.erb

<%= render "admin/shared/edit", { :form_type => "article", :form_action => { :action => "new", :id => @article.id , :class => ('autosave')} } %>

The problem is how to write a correct route for the controller action above which will allow me to render an edit page containing newly merged article. I wrote:

  match "/admin/content/merge/:id" => "admin/content#merge",:as => 'admin/content/merge'

rake routes output:

admin_content_merge        /admin/content/merge/:id(.:format) {:controller=>"admin/content", :action=>"merge"}

But the new or edit action is being invoked as I can see.

Apparently, my route is wrong, isn't it?

Could you please help me with this.

Thanks in advance!

Update

Up-to-date 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 %>
È stato utile?

Soluzione

Read the hint from the course:

HINT:Nesting is invalid in HTML.

That means that you can't nest form tags, don't put the form tag in another form tag, your nested form wont be able to do a correct action.

Since you have to put your code at the end of the page, try and see how to do it with having your merging form tag below the main edit article form tag. So basically you can find where the big form tag ends and put it below it.

Try to see if you can figure it out, and if not, don't hesitate to ask :)

Btw. I think everybody had some problem with this

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top