<%= 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
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
In my project I have the following form_tag to select a Site
<%= form_tag({:controller => "hvacs", :action => "index"}, :method => "get") do %>
<div class="field">
<%= select :p, :site_sel, #user_sites.map{|s| [s.name, s.id]} %>
</div>
<div class="actions">
<%= submit_tag("Select site") %>
</div>
<% end %>
This form_tag updates the index page through calling its method in the controller again.
I have the following button_to
<td><%= button_to 'Select', {:controller => "hvacs", :action => "select"}, :method => "get" %></td>
I would like to achieve a similar update with this as above rather than redirect to a new page with "select_path" etc, but the above does not seem to work.
How can I achieve this? Cheers!
OK, this looked so much like my AJAX problem, I tried to make it one!
I think all you need is a simple render statement in your select action
render :index
or
render :action => 'index'
But see http://guides.rubyonrails.org/layouts_and_rendering.html#using-redirect_to for more.
The following solution worked. Apologies if I was not so clear on what I was looking for.
<%= button_to 'Select', review_hvacs_path(:h => hvac, :a => params[:a], :s => params[:s]) %>
I was trying to pass parameters with the button, while staying on the review page.
When I click on the 'delete' link (to delete the project), I wish to show a new page for the user to confirm the deletion ('are you sure' page). But when I click on the link, error No route matches [GET] "/assets" appears, I don't know why. Can anyone help me?
This is my projects/index.html.erb
1 <% #projects.each do |p| %>
2 <%= link_to p.name, project_path(p) %>
3 <%= link_to "edit", edit_project_path(p) %>
4 <%= link_to "delete",{ :action => 'delete', :controller => 'projects', :id => p.id } %>
5 <% end %>
This is my controllers/projects_controller.rb
38 def delete
39 #project = Project.find(params[:id])
40 render 'delete'
41 end
42
43 def destroy
44 project = Project.find(params[:id])
45 project.destroy
46 flash[:notice] = "Page succesfully removed"
47 redirect_to(projects_path)
48 end
This is my views/projects/delete.html.erb
1 <%= form_for(:project, :url => {:action => 'destroy', :id => #project.id }) do |f| %>
2 <p>Are you sure you want to remove "<%= #project.name %>"</p>
3 <%= submit_tag ("Yes, I am sure")
4 <% end %>
EDIT:My Url looks like this >>> http://localhost:3000/assets?action=delete&controller=projects&id=1
EDIT2: the solution is going to routes.rb and adding this line
match ':controller(/:action(/:id))(.:format)'
Now the delete page appears. However, when I click the delete button Rails returns an error >> Unknown action The action '6' could not be found for ProjectsController
The problem is this line:
<%= form_for(:project, :url => {:action => 'destroy', :id => #project.id }) do |f| %>
You're not telling it to use the HTTP method DELETE when making this
<%= form_for(:project, :url => {:action => 'destroy', :id => #project.id }, :method => :delete) do |f| %>
You could also write this much, much shorter like this:
<%= form_for(#project, :method => :delete) do |f| %>
Rails will know precisely what URL to build, so you don't need to tell it what to do.
Bonus
You could also do this with the initial link_to:
<%= link_to "delete", [:delete, project], :method => :delete %>
The array as the second argument here will compile the route using the polymorphic route builder in Rails.
I'm using link_to in RoR 3
When I use it like this, it works fine:
<%= link_to "Add to your favorites list",:controller =>
'favourite_companies', :action =>'create',
:company_id=>"#{#company.id}",
:company_name=>"#{#company.company_name}" %>
But I would like to pass in a class as well
however, this is not working for me. The class works, but it breaks the link. Any ideas?
<%= link_to "Add to your favorites list",{:controller =>
'favourite_companies', :action =>'create'},
:company_id=>"#{#company.id}",
:company_name=>"#{#company.company_name}",
:class=>"ui-button-text button_text"} %>
<%= link_to "Add to your favorites list",{:controller =>
'favourite_companies', :action =>'create'},
:company_id=>"#{#company.id}",
:company_name=>"#{#company.company_name}",
:class=>"ui-button-text button_text"} %>
try this
<%= link_to "Add to your favorites list", :controller =>
'favourite_companies', :action =>'create',
:company_id=>"#{#company.id}",
:company_name=>"#{#company.company_name}",
{ :class=>"ui-button-text button_text" } %>
Since the :class should be in :html_options (refering to API)
link_to(body, url, html_options = {})
The proper way of doing what you have is as follows:
link_to "Foo", { URL_FOR PARAMS HERE }, :class => "bar"
As far as setting the controller and action manually like this, well, it's crap. Rails builds url helpers for you; use them and save yourself some time, energy, and add clarity, all at once:
link_to "Foo", favourite_companies_path(#company), :method => :post
What you're doing with the string interpolation is a bad idea too…it's just wasteful and cluttered for no reason at all. The following is the same, just better:
link_to "Foo", :company_id => #company.id, :company_name => #company.name
As far as why your link wasn't working, if wrapping it in a div helped it sounds like you have a problem with your HTML structure, not the link_to syntax.
I'm using a link_to do-end block so the above previous solutions didn't work for me.
If you want to embed other tags in your a tag, then you can use the link_to do-end block.
<%= link_to favourite_companies_path(:company_id => #company.id, :another_url_param_here => "bar"), { :class => "ui-button-text button_text", :title=> "We can have more html attributes as well" } do %>
<i class="fa fa-star"></i>
<%= #company.company_name %>
<% end %>
In this case it's
<%= link_to path(url_params), html_options = {} do %>
<% end %>
Be careful because in Rails 5 the above methods will still result in a wrong URL generation. The controller and action need to be put in a literal hash in order for it to work in Rails 5. What you will have should be something like this
<%= link_to "Add to your favorites list",
{ controller: "favourite_companies", action:"create"},
company_id: #company.id,
company_name: #company.company_name,
class: "ui-button-text button_text" %>
I am using link_to img tag like following
<%= link_to image_tag("Search.png", :border=>0, :class => 'dock-item'),
:action => 'search', :controller => 'pages'%><span>Search</span></a>
Which results in following html
<a href="/pages/search"><img alt="Search" border="0" class="dock-item"
src="/images/Search.png?1264132800" /></a><span>Search</span></a>
I want the class="dock-item" to go to the <a> tag instead of the img tag.
How can i change this?
Update:
<%= link_to image_tag("Search.png", :border=>0), :action => 'search',
:controller => 'pages', :class => 'dock-item' %>
results in
<a href="/pages/search?class=dock-item"><img alt="Search" border="0"
src="/images/Search.png?1264132800" /></a>
hi you can try doing this
link_to image_tag("Search.png", border: 0), {action: 'search', controller: 'pages'}, {class: 'dock-item'}
or even
link_to image_tag("Search.png", border: 0), {action: 'search', controller: 'pages'}, class: 'dock-item'
note that the position of the curly braces is very important, because if you miss them out, rails will assume they form a single hash parameters (read more about this here)
and according to the api for link_to:
link_to(name, options = {}, html_options = nil)
the first parameter is the string to be shown (or it can be an image_tag as well)
the second is the parameter for the url of the link
the last item is the optional parameter for declaring the html tag, e.g. class, onchange, etc.
hope it helps! =)
Just adding that you can pass the link_to method a block:
<%= link_to href: 'http://www.example.com/' do %>
<%= image_tag 'happyface.png', width: 136, height: 67, alt: 'a face that is unnervingly happy'%>
<% end %>
results in:
<a href="/?href=http%3A%2F%2Fhttp://www.example.com/k%2F">
<img alt="a face that is unnervingly happy" height="67" src="/assets/happyface.png" width="136">
</a>
This has been a life saver when the designer has given me complex links with fancy css3 roll-over effects.
Best will be:
link_to image_tag("Search.png", :border => 0, :alt => '', :title => ''), pages_search_path, :class => 'dock-item'
this is my solution:
<%= link_to root_path do %>
<%= image_tag "image.jpg", class: "some class here" %>
<% end %>
Easy:
<%= link_to image_tag("Search.png", :border=>0), :action => 'search', :controller => 'pages', :class => 'dock-item' %>
The first param of link_to is the text/html to link (inside the a tag). The next set of parameters is the url properties and the link attributes themselves.
I tried this too, and works very well:
<%= link_to home_index_path do %>
<div class='logo-container'>
<div class='logo'>
<%= image_tag('bar.ico') %>
</div>
<div class='brand' style='font-size: large;'>
.BAR
</div>
</div>
<% end %>
To respond to your updated question, according to http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html...
Be careful when using the older argument style, as an extra literal hash is needed:
link_to "Articles", { :controller => "articles" }, :id => "news", :class => "article"
# => Articles
Leaving the hash off gives the wrong link:
link_to "WRONG!", :controller => "articles", :id => "news", :class => "article"
# => WRONG!
The whole :action =>, :controller => bit that I've seen around a lot didn't work for me.
Spent hours digging and this method definitely worked for me in a loop.
<%=link_to( image_tag(participant.user.profile_pic.url(:small)), user_path(participant.user), :class=>"work") %>
Ruby on Rails using link_to with image_tag
Also, I'm using Rails 4.
Hey guys this is a good way of link w/ image and has lot of props in case you want to css attribute for example replace "alt" or "title" etc.....also including a logical restriction (?)
<%= link_to image_tag("#{request.ssl? ? #image_domain_secure : #image_domain}/images/linkImage.png", {:alt=>"Alt title", :title=>"Link title"}) , "http://www.site.com"%>
Hope this helps!
<%= link_to root_path do %><%= image_tag("Search.png",:alt=>'Vivek',:title=>'Vivek',:class=>'dock-item')%><%= content_tag(:span, "Search").html_safe%><% end %>
You can also try this
<li><%= link_to "", application_welcome_path, class: "navbar-brand metas-logo" %></li>
Where "metas-logo" is a css class with a background image