Rails: Disabling link_to link not working but hidden link_to working - ruby-on-rails-4.1

The link_to method is as which is not disabled:-
<%= link_to edit_cabinet_path(object), remote: true, disabled: true do %>
<span class="glyphicon glyphicon-pencil"></span>
<% end %>
but if i do like below which hides the link
<%= link_to edit_cabinet_path(object), remote: true, style: "display:none;" do %>
<span class="glyphicon glyphicon-pencil"></span>
<% end %>
Now the question is how to disable this type of link with block, and whats the reason that second code is working and first is not.

Probably, you are looking for link_to_if. link_to_if makes your link clickable only if your condition pass.
Your code should be something like:
<%= link_to_if false, edit_cabinet_path(object), remote: true do %>
<span class="glyphicon glyphicon-pencil"></span>
<% end %>
To make it dynamic you can call condition that satisfy whether to active or inactive that link, something like:
<%= link_to_if cabinate.active?,
"<span class='glyphicon glyphicon-pencil'></span>".html_safe,
edit_cabinet_path(object), remote: true %>
Hope this answer your question..

Actually there is no disabled attribute available for link_to, only for button_to tag.
For more information please refer here: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
In this case, you might want to use link_to_if, please have a look into this: http://apidock.com/rails/v4.2.1/ActionView/Helpers/UrlHelper/link_to_if

I wrote some simple JS to allow you to add disabled: true to link_to method
//this allows us to use html disabled attribute in rails
//to prevent clicking on a disabled link from doing anything
$('a[disabled]').click(function(e){
e.stopImmediatePropagation()
e.preventDefault();
});

link_to_if doesn't work as expected. It only renders the given block when your condition is false as a fallback.
https://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to_if#1589-Passing-a-block-does-not-behave-as-expected
In my case I made a Proc for a given block
<%link_block = Proc.new{%>
<span class="glyphicon glyphicon-pencil"></span>
<%}%>
<%if condition?%>
<%= link_to edit_cabinet_path(object), remote: true, disabled: true, &link_block%>
<%else%>
<%link_block.call%>
<%end%>

Related

Instance variable in partial view

I have a navbar that is a partial view that I need to render on a devise page for the user to edit their profile. As it is, I only have one page, but adding the path to perform account maintenance has messed up my navbar loading because of the instance variable not being present. How can I get a global instance variable that works across the board for my navbar, no matter what?
application_helper.rb
def get_company
#company = current_user.company
end
def get_locations(company)
#locations = if #company
current_user.company_locations.order(:name)
else
[]
end
end
pages_controller.rb
def home
#company = get_company
#locations = get_locations(#company)
#reports = if #company
#company.reports.order("created_at DESC").limit(5)
else
[]
end
end
views/devise/registrations/edit.html.erb
<%= render partial: "pages/navigation" %>
... devise form ....
pages/_navigation.html.erb
<li class="right-menu-item">
<button class="btn dropdown-toggle " type="button" id="dropdownMenu1" data-toggle="dropdown">
<% if #company %>
<%= #company.name %>
<% else %>
<%= current_user.email %>
<% end %>
<span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-menu-right" role="menu" aria-labelledby="dropdownMenu1">
<% #locations.each do |location| %>
<li role="presentation">
<%= link_to root_path(location_id: location.id), role: "menuitem", tabindex: "-1" do %>
<%= image_tag "check-green.svg" if location == #location %>
<span><%= location.name %></span>
<% end %>
</li>
<% end %>
</ul>
Where the issue lies:
What am I missing here to get this working across all views?
Thanks!
Ok, seems that Where is the controller of Devise ? how to add data from other models in users/edit page? was helpful. I had to do the following to make this work in the partial for the devise views:
rails g devise:controllers users -c registrations
which made a registrations controller under controllers/users/registrations.rb, then i was able to add the instance variables I needed do the edit method:
def edit
#company = get_company
#locations = get_locations(#company)
end
Had to also change the route.rb file around a little to facilitate the new controller:
devise_for :users, skip: [:sessions],
controllers: {
registrations: 'users/registrations'
}

How to include "span" and "class" with submit button?

How can I make the new_value_path actually be a submit button that creates a new value instead of just refreshing back onto itself?
My current new.html.erb:
<h1>New Value</h1>
<%= render 'form' %>
<%= link_to new_value_path, class: 'btn' do %>
<b><span class="glyphicon glyphicon-plus"</span></b>
<% end %>
I want to keep the class: 'btn' and the span class="glyphicon glyphicon-plus" I think it has something to do with CRUD, but I haven't found an answer that specifically deals with including a class and a span.
I had to solve the answer in the _form itself using f.submit

Rails 4 button_to using helper improper redirection

I am building an index for one of my models. Instead of the usual table, I want to have two combo-boxes: one for selecting the object, the other for selecting the method (either edit or destroy). Upon clicking the submit button, I should be redirected to the appropriate method reference (e.g. admin_users/1/edit or admin_users/17/destroy). I have written a helper that constructs the url reference, but for some reason it does not work. when I use button_to I get redirected to the create method, when I use button_tag nothing works. Any ideas?
view code:
<%= form_tag do %>
<p id="notice"><%= flash[:notice] %></p>
<h1>Listing admin_users</h1>
<p><strong>select admin user</strong></p>
<%= select_tag(#req_id, options_from_collection_for_select(#admin_users, :id, :login)) %>
<br/>
<p><strong>select action</strong></p>
<%= select_tag(#oper, options_for_select([['edit'],['destroy']])) %>
<br/>
<%=button_tag 'go go!', get_path(#req_id,#oper) %>
<% end %>
helper code:
module AdminUsersHelper
def get_path(req_id,oper)
a=[req_id, oper].join("/")
["admin_users", a].join("/")
end
end
The default method for a form submit is POST. You'll need to change the method accordingly.
<%= form_tag( '/users/confirm', method: :get ) %>
May I suggest submitting with Javascript? You'll have a more dynamic control over the method of the submission. Take a look Here
so, eventually I went on the javascript solution, just as #Hassanin Ahmed suggested. Thanks for Helping!
<%= form_tag('/admin_users', id: "admin_users_menu") do %>
[...]
<font size=4>
<u>select user:</u>
</font>
<br>
<br>
<select id="admin_user_id" name="admin_user[id]">
<%= options_for_select #admin_users.collect{|user| [user.login, user.id]} %>
</select>
<br>
<br>
<font size=4> <u>select option:</u></font><br><br>
<input type="radio" checked name="select_op" value= "edit" > edit
<br>
<br>
<input type="radio" name="select_op" value= "delete" > delete
<br>
<br>
<%= submit_tag "go" %>
<br>
<br>
<% end %>
<script>
$(function (){
$("#admin_users_menu").submit(function() {return direct_me() })
})
function direct_me(){
//getting the admin_user id
var au_id=$('select').val()
//getting the desired method
var op=$('input[name=select_op]:radio:checked').val()
//making the default url
var u="admin_users/"+au_id
var f=$("form")
if(op=='edit'){
u=u+"/edit"
f.attr("method","GET")
}
else if(op=='delete'){
f.attr("method","POST")
f.append("<input type='hidden' name='_method' value='delete'>");
}
f.attr("action",u)
if(op=='delete') return confirm("Are you sure?")
}
</script>

Parsing erb within erb in a view. How to add CSS class?

I have a navbar, which is showing an unread messages count. The unread count has to go within the navbar text for formatting, so I have used the below:
<li><%= link_to "My Messages #{current_user.mailbox.receipts.where({:is_read => false}).count(:id, :distinct => true).to_s}", conversations_path, class: 'small button' %></li>
How do I add a class to the count? I want to add the bootstrap navbar-unread class.
I can put:
<span class='navbar-nav'><%= current_user.mailbox.receipts.where(:is_read => false).count(:id, :distinct => true).to_s %></span>
...outside the list but then the unread count is not in the right place.
Thanks for any help, can't find an answer anywhere, or at least a better way to do this.
EDITED:
The output I need from the html would look like this:
<li>
<a class="small button" href="/converssations/link">
My Messages<span class="navbar-unread">0</span>
</a>
</li>
So I need to pass a class into a span around the ruby that starts 'current_user.mailbox...etc
It's to show the number of unread messages, which is a small number in the corner of the button.
Maybe this can help you.
Look at ActionView Helpers reference also.
<%= link_to content_tag(:span, "My Messages #{current_user.mailbox.receipts.where({:is_read => false}).count(:id, :distinct => true).to_s}", class: "navbar-nav"), conversations_path, class: "small button" %>
will have output like this
<a class="small button" href="/converssations/link">
<span class="navbar-nav">My Messages Count</span>
</a>
To achieve your mentioned behavior you can pass a block to link_to like this
<%= link_to "#your_link", class: "small button" do %>
My Message <%= content_tag(:span, "1", class: "navbar-unread") %>
<% end %>

Modifying <%= f.submit %>'s default CSS input class

I have been trying without much luck to get <%= f.submit %> to appear as the same as my other "buttons", all in a row. I have found this helpful post on modifying the class of f.submit, but realized upon examining its element in browser that it took on the class of input, regardless of which additional classes I added as option parameters, thus restricting its appearance.
Essentially, each of my other buttons has the following form:
<div class="sort-nav">
<ul>
<li><h4><%= link_to "Some stuff", some_link_path %></h4></li>
</ul>
</div>
And I was wondering if there is a way to fit all of these styles compacted into one class, and override that of the input class contained in f.submit. Thanks.
For edification, this button is going to be my "Follow"/Unfollow" button used to create or destroy Relationships, which I first intend to render a _follow_form partial with the following code:
<% if current_user.following?(#course) %>
<%= render 'unfollow' %>
<% else %>
<%= render 'follow' %>
<% end %>
With each of the individual _followpartial looking like the following:
<%= form_for(current_user.relationships.build(followed_id: #course.id)) do |f| %>
<%= f.hidden_field :followed_id %>
<%= f.submit "Follow course" %>
<% end %>
a bit hackish but you can always use js to submit the form so instead of using f.submit, change it to
<div class="sort-nav">
<ul>
<li>
<h4><%= link_to_function "Some stuff", '$(this).closest("form").submit()' %></h4
</li>
</ul>
</div>

Resources