I'm sure that this has something to do with my N00b syntax but I'm having trouble working out what it is...
I am creating a menu by looping through items in my subpages table and creating a link for each item that is returned, Like this:
<% #subpages.each do |menu| %>
<%= link_to(menu.name, {:controller => 'public', :action => "page", :id => menu.permalink }, :class => "show action footer-link") %>
<% end %>
this is working fine on the homepage of my site but if you visit one of the subpages, let's say the about us page:
http://localhost:3000/public/page/about-us
and then try to use the menu again to visit "contact us", instead of taking you to this link as i would expect:
http://localhost:3000/public/page/contact-us
It takes you to this link:
http://localhost:3000/public/page/about-us?id=contact-us
What school-boy error am I making here?
Thanks in advance.
edit: my routes
root :to => "public#index"
get 'admin', :to => 'access#menu'
get 'public/show/:permalink', :to => 'public#show'
get 'public/page/:permalink', :to => 'public#page'
Try with this :
<% #subpages.each do |menu| %>
<%= link_to(menu.name, page_public_path(menu.permalink), :class => "show action footer-link") %>
<% end %>
Thanks
I managed to get it to work by passing the :permalink param to the controller instead of the ID
<% #subpages.each do |menu| %>
<%= link_to(menu.name, { :action => "page", :permalink => menu.permalink }, :class => "show action footer-link") %>
<% end %>
Related
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 %>
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
<%= link_to (:controller => "company_stuff", :action => "index", :anchor => :menu), :class => 'links' do %>
<li>Terms of Use</li>
<% end %>
I am having difficulty linking a page which is on a different controller and also the link is an anchor. Basically the controller is called company_stuff the action is index and the anchor is called #terms
The problem was that the :controller :action :anchor was not being passed through as a hash, separate from the CSS class
Below is the solution
<%= link_to "Terms Of Use", {:controller => "company_stuff", :anchor => "terms"}, :class => "links" %>
I believe you can try something like this
<%= link_to index_company_stuff_path + "#terms", :class => 'links' do %>
<li>Terms of Use</li>
<% end %>
Or
<%= link_to index_company_stuffs_path + "#terms", :class => 'links' do %>
<li>Terms of Use</li>
<% end %>
Depending on your controller name and route.
You can find more information on this question How to create an anchor and redirect to this specific anchor in Ruby on Rails
I show in my homepage 4 pictures, and when a user click on one of it, I want to change a parameter :asked in my db.
In my view I've added
<%= link_to image_tag(friends.picture), {:controller => "static_pages", :action => "recomend", :id => friends.user_id} %>
In the Static_Pages_Controller I have
def recomend
a = Friends.find_by_user_id(params[:id])
a.update_attribute(:asked, true)
end
And in routes.rb
resources :static_pages do
resources :recomend
end
but when I click on it, the server refresh my home (why?!) and in the server logs i see
Started GET "/auth/failure?action=recomend&controller=static_pages&id=101" for 127.0.0.1 at 2012-11-17 19:59:25 +0100.
Maybe it's not recognize the link. I suppose friends.picture is a link for image, so you can try this:
<%= link_to( "", :controller => "static_pages", :action => "recomend", :id => friends.user_id) do %>
<%= image_path(friends.picture) %>
<% end %>
I have a page on the website, which has lots of anchor link eg #menu | #sauces etc
On the page itself the links work fine, its brilliant.
However when I am on a different controller/view, The links do not take me back to the main controller, and to the anchor point clicked.
here is an example of one anchor link, which is in the header (which is on ALL controller views)
<%= link_to '#main', :id => 'menu_link' do %>
<li>Menu</li>
<% end %>
That is in :controller => "main", :action => "index"
When I am in another controller, for example my locations controller,
The links become like this localhost:3000/locations#menu
It should really be localhost:3000/#menu
The root is set to go to the main controller and index action.
Here is my routes.rb file
root :to => "main#index"
match 'admin', :to => 'access#admin_index'
match 'locations', :to => 'ranch_locations#locations'
match ':controller(/:action(/:id))(.:format)'
You need to specify that it is going to a different controller.
<%= link_to '#main', :controller => "main", :action => "index", :id => 'menu_link' do %>
<li>Menu</li>
<% end %>
You should use root_path(:anchor => :main) instead of "#main"
link_to "Comment wall", profile_path(#profile, :anchor => "wall")
# => Comment wall
http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to
upd:
<%= link_to '#main', :id => 'menu_link' do %>
should be changed to
<%= link_to root_path(:anchor => :main), :id => 'menu_link' do %>
I'm making a form_tag panel that contains information (checkboxes) specific to a controller action. This action is set up in "routes.rb" as follows:
resources :students do
collection do
get :send_student_report_pdf
end
end
This setup works perfectly when I call the action from a link_to:
<%= link_to "Download PDF Report", :action => 'send_student_report_pdf', :controller => 'students'%>
However when I used it in a form_tag, it keeps giving me this error:
Routing Error
No route matches "/students/send_student_report_pdf"
The form_tag code I have is here:
<%= form_tag :controller => 'students', :action => 'send_student_report_pdf', :method => 'get' do %>
<%= label_tag "Include columns" %> <br>
<%= check_box_tag "first_name", params[:first_name], checked = true %> <%= label_tag "First Name" %><br>
<%= submit_tag "Download PDF Report", :action => 'send_student_report_pdf', :controller => 'students'%>
<% end %>
I have tried giving it the url, path like:
<%= form_tag send_student_report_pdf_students_path, :method => 'get' do %>
But it has been consistently giving me the same Route error (as if the action doesn't exist at all in routes.rb, even though it works perfectly using link_to instead of form_tag submit
Here is the code for the action in the controller, it basically sends back a file.
def send_student_report_pdf
#students = search_sort_and_paginate
puts "params[:first_name] = ", params[:first_namea]
send_data(generate_pdf_report(#students), :filename => "report.pdf", :type => 'application/pdf')
end
If you see that I'm missing something here, please help me.
Thank you very much,
Regards,
The :method => 'get' part in your form_for is in the url_for_options hash, not the options hash, so Rails will be putting it onto the url as cgi params instead. Try changing it to this:
form_tag url_for(:controller => 'students', :action => 'send_student_report_pdf'), :method => 'get' do ...
The reason you can't use the named route is because you didn't name it in your routes. If you name it in your routes and use the named route in your form_tag, you won't need to use url_for...
resources :students do
collection do
get :send_student_report_pdf, :as => :send_student_report_pdf
end
end
You can check whether your routes are as you expect by running rake routes