link_to doesn't work, controller appears as a html attribute - ruby-on-rails

I have been googling last 2 hours and unfortunately I cannot fix this, though I thought it must be easy.
<%= link_to 'Log Out', {:controller => 'static_pages', :action => 'index'} do %>
<i class="glyphicon glyphicon-log-out link-icon"></i>
<% end %>
This the code I have. So, I need "Log Out" link to appear with log-out icon, which is from bootstrap. But HTML output of this code is this:
<a controller="static_pages" action="index" href="Log Out">
<i class="glyphicon glyphicon-log-out link-icon"></i>
</a>
I tried many ways to fix this, but I also tried removing "do" so, I can see if it causes the problem. This is the link after I removed "do":
<%= link_to 'Log Out', {:controller => 'static_pages', :action => 'index'} %>
HTML output of this is:
Log Out
It doesn't make sense. Because I have same link_to in my footer and it works. Footer:
<%= link_to 'Log In', {:controller => 'users', :action => 'login'} %>
Footer output:
Log In
So, why this isn't working in another page? Thank you.

When you pass a block to the link_to helper this will be a body for it.
<%= link_to :controller => 'static_pages', :action => 'index' do %>
<i class="glyphicon glyphicon-log-out link-icon"></i>
Log Out
<% end %>

Related

Devise destroy_user_session_path on heroku

I am trying to log out a user from a (test) site in production on heroku.
The problem is heroku transforms this <%= link_to 'Logout', destroy_user_session_path, method: :delete %> in a GET /users/sign_out
I've tried this <%= link_to 'Logout', destroy_user_session_path, method: :delete, :data => { :no_turbolink => true } %> as suggested here Devise destroy_user_session_path does'nt work
I've found some solutions in reply to destroy_user_session_path is triggering GET instead of DELETE in Rails but I don't feel comfortable with the answers as I'd like to avoid a GET.
Logout was ok in development, so I checked on https://devcenter.heroku.com/ and made some modifications, but I am still stuck.
I worked on the asset pipeline and added these gems:
gem 'rails_serve_static_assets'
gem 'rails_stdout_logging'
These gems are probably useful in other cases, but not mine (I also have a problem with links to images that don't persist).
EDIT
Here is my full template
<div class="" style="background-color: #1a252f;">
<ul class="nav justify-content-center">
<% if user_signed_in? %>
<li class="nav-item">
<%= link_to 'Edit account|', edit_user_registration_path %></li>
<li class="nav-item">
<%= link_to 'Logout', destroy_user_session_path, method: :delete, :data => { :no_turbolink => true } %></li>
<% else %>
<li class="nav-item">
<%= link_to 'Login', new_user_session_path %></li>
<% end %>
</ul>
</div>
EDIT 2
I've also tried this in routes.rb:
devise_for :users do
get "/users/sign_out" => "devise/sessions#destroy", :as => :destroy_user_session
end
and I still get a status 404 in the heroku logs.
The solution provided here: destroy_user_session_path is triggering GET instead of DELETE in Rails was the one for me,
i.e. with config.sign_out_via = :get in devise.rb

Keep getting no route matches for edit. Tried using "edit", now :update and still no luck

ActionView::Template::Error (No route matches {:action=>"update",
:controller=>"text_posts"}):
<h1><%= text_post.blog %></h1>
<%= form_tag do %>
<%= link_to "New Text Post", controller: "text_posts", action: "new" %>
<%= link_to "Edit", controller: "text_posts", action: "update" %>
<%= link_to "Delete", controller: "text_posts", action: "destroy" %>
<% end %>
<span class='likes pull-left'>
new_image_link GET /image_links/new(.:format) image_links#new
edit_image_link GET /image_links/:id/edit(.:format) image_links#edit
image_link GET /image_links/:id(.:format) image_links#show
PATCH /image_links/:id(.:format) image_links#update
PUT /image_links/:id(.:format) image_links#update
DELETE /image_links/:id(.:format) image_links#destroy
text_posts GET /text_posts(.:format) text_posts#index
POST /text_posts(.:format) text_posts#create
new_text_post GET /text_posts/new(.:format) text_posts#new
edit_text_post GET /text_posts/:id/edit(.:format) text_posts#edit
text_post GET /text_posts/:id(.:format) text_posts#show
PATCH /text_posts/:id(.:format) text_posts#update
PUT /text_posts/:id(.:format) text_posts#update
DELETE /text_posts/:id(.:format) text_posts#destroy
You can do something like this:
<%= link_to "Edit", edit_text_post_path(text_post) %>
Since you use resources in your routes.rb(according to the routes your provided). If you want to go to the edit page, it's better to use the path helper, remember to provide the text_post you want to edit in the argument:
<%= link_to "Edit", edit_text_post_path(text_post) %>
<%= link_to "Delete", text_post_path(text_post), :method => :delete %>
The way you wrote is when you have something define in your routes.rb like this:
match ':controller(/:action(/:id(.:format)))', :via => :all
Even so, you still have to give it the text_post you want to edit because it requires an id in the end of the url. So it should be like:
<%= link_to 'Edit', :controller => 'text_posts', :action => 'edit', :id => text_post %>
<%= link_to 'Delete', :controller => 'text_posts', :action => 'destroy', :id => text_post %>
You don't need to wrap the links into a form_tag.
Hope it helps.
Try this .................
<h1><%= text_post.blog %></h1>
<%= link_to "New Text Post", new_text_post_path %>
<%= link_to "Edit", edit_text_post_path(text_post) %>
<%= link_to "Delete", text_post_path(text_post), method: :delete %>
Hope this will work for you.

How to render a multiple partials in one view using ajax in rails?

I've been struggling with an error appearing while rendering multiple partials to one specific page. A have 4 different buttons in my administration panel and I want each of them to call an ajax call to render 4 different partials accordingly. Unfortunately, when I'm trying to make it happen, it happens to work on one button only, the rest doesn't respond at all.
Here's my action in controller:
def administration
#orders = Order.all
respond_to do |format|
format.json
format.html
format.js
end
end
My view:
<% provide(:title, "Administration") %>
<h1>Manage your site</h1>
<section>
<div class="tabs tabs-style-bar">
<nav>
<ul>
<li><%= link_to "Masters", {:controller => 'masters', :action => 'index'}, class: "link fa fa-group", :remote => true %>
</li>
<li><%= link_to "Orders", {:controller => 'masters', :action => 'administration'}, class: "fa fa-tasks", :remote => true %>
</li>
<li><%= link_to "Services", {:controller => 'masters', :action => 'services'}, class: "fa fa-tags", :remote => true %>
</li>
<li><%= link_to "Settings", edit_master_path(current_master), class: "fa fa-cogs" %>
</li>
</ul>
</nav>
</div><!-- /tabs -->
</section>
<div class="orders"></div>
<div class="masters"></div>
<div class="services"></div>
and My JS receiver:
var orders_inject = $("<%= j raw render 'orders' %>");
$('.orders').html(orders_inject);
So please, let me work it all out as it's been quite long since the error occurred!
Hope this will work for you, try this and change your render files. I just suppose here partial test1 and test2
$(".orders").replaceWith("<%= j(render('test1') %>");
$(".services").replaceWith("<%= j(render('test2') %>");

how to display caret via rails link_to for dropdown in bootstrap

I want to display the little drop down menu symbol in my rails app that uses twitter bootstrap via the link_to method in my rails app.
I can't figure out how to do it. When I add the <b class="caret"></b> (that twitter bootstrap uses) to the 'Setup ' argument below, it gets escaped and shows up as text not html.
<%= link_to 'Setup ', root_path, :id => 'setupdrop', :'data-toggle' => 'dropdown', :class => 'dropdown-toggle' %>
Of all the rails examples that I have seen so far, none use link_to when using this feature of TBS (and include the caret).
Try this,
<%= link_to 'Setup <b class="caret"></b>'.html_safe, root_path, :id => 'setupdrop', :'data-toggle' => 'dropdown', :class => 'dropdown-toggle' %>
I remember having an issue with this very thing. If I remember correctly it had to do with the tag order - it had to be inside a <li> tag. Here's my code that works:
<ul class="nav" role="navigation">
<li class="explore">
<%= link_to 'explore <b class="caret"></b>'.html_safe, root_path, :id => 'setupdrop', :'data-toggle' => 'dropdown', :class => 'dropdown-toggle' %>
<ul class="dropdown-menu" role="menu" aria-labelledby="drop1">
<li class="nav-header">Majors</li>
<li class="dropdown-link"><%= link_to "View All", majors_path %>
<li class="dropdown-link">By School</a></li>
<li class="dropdown-link">Recently Added</a></li>
<li class="dropdown-link">Most Liked</a></li>
</ul>
</li>
</ul>
Thanks to Amar I didn't have to play around with converting it into erb as he did it. Here's what my code now looks like with Amar's help.
link_to takes a block so you can do
link_to root_path, :id => 'setupdrop', :'data-toggle' => 'dropdown', :class => 'dropdown-toggle' do
Setup
<b class="caret"></b>
end

Using link_to with embedded HTML

I'm using Twitter's Bootstrap stuff and I have the following HTML:
<a class="btn" href="<%= user_path(#user) %>"><i class="icon-ok icon-white"></i> Do it#</a>
What's the best way to do this in Rails? I'd like to use <%= link_to 'Do it', user_path(#user) %> but the <i class="icon-ok icon-white"></i> is throwing me off?
Two ways. Either:
<%= link_to user_path(#user) do %>
<i class="icon-ok icon-white"></i> Do it#
<% end %>
Or:
<%= link_to '<i class="icon-ok icon-white"></i> Do it#'.html_safe, user_path(#user) %>
I had the same need recently. Try this:
<%= link_to '<i class="icon-ok icon-white"></i> Do it'.html_safe, user_path(#user) %>
You have also the possibility to create an helper method like below:
def link_fa_to(icon_name, text, link)
link_to content_tag(:i, text, :class => "fa fa-#{icon_name}"), link
end
Adapt the classes to your needs.
In normal HTML we do,
<i class="fa fa-user-plus"></i> Register
In Ruby On Rails:
<%= link_to routeName_path do %>
<i class="fa fa-user-plus"></i> Link Name
<% end %>
<%= link_to register_path do %>
<i class="fa fa-user-plus"></i> Register
<% end %>
If you want a link in rails that uses that same icon class from twitter bootstrap all you need to do is something like this.
<%= link_to "Do it#", user_path(#user), :class => "btn icon-ok icon-white" %>
Using HAML:
= link_to model_path do
%img{src: '/assets/someimg.png'}
In the gem twitter-bootstrap-rail : they create a helper glyph
def glyph(*names)
content_tag :i, nil, :class => names.map{|name| "icon-#{name.to_s.gsub('_','-')}" }
end
So you can use it like: glyph(:twitter)
and you link helper could look like: link_to glyph(:twitter), user_path(#user)
I will give this a shot since you haven't accepted an answer yet
and the other answers are not 100% what you were looking for.
This is the way to do it the Rails way.
<%= link_to(user_path(#user), :class => 'btn') do %>
<i class="icon-ok icon-white"> </i> Do it!
<% end %>
Edit: leaving my answer for future reference,
but #justin-herrick has the correct answer when
working with Twitter Bootstrap.
I think you can simplified it through a helper method if you use it frequently in your application.
put it in helper/application_helper.rb
def show_link(link_text, link_source)
link_to("#{content_tag :i, nil, class: 'icon-ok icon-white'} #{link_text}".html_safe,
link_source, class: "btn")
end
Then call it from your view file just like link_to
<%= show_link "Do it", user_path(#user) %>
If you are using the bootstrap 3.2.0, you can use this helper in your app/helpers/application_helper.rb
module ApplicationHelper
def glyph(*names)
content_tag :i, nil, :class => names.map{|name| "glyphicon glyphicon-#{name.to_s.gsub('_','-')}" }
end
end
and then, in your views:
link_to glyph(:pencil) + ' Edit', edit_post_path(#post), class: 'btn btn-warning'
def show_link (source, text)
link_to source, {'data-original-title' => 'Show', 'data-toggle' => 'tooltip', :class => 'btn btn-xs btn-success'} do
"#{text} #{content_tag :i, nil, class:' glyphicon glyphicon-eye-open' }".html_safe
end
end
Helper based on Titas Milan's suggestion, but using a block:
def show_link(link_text, link_source)
link_to link_source, { class: 'btn' } do
"#{content_tag :i, nil, class: 'icon-ok icon-white'} #{link_text}".html_safe
end
end

Resources