Disable link_to tag in Rails3+ - ruby-on-rails

I used the following code:
<%= link_to image_tag("edit.png", :alt => "Edit"), edit_user_path(user) %>
I want to disable this link and image, so I added :disabled=>true to the code, but it's not disabling. Why not, and how do I disable them?

I'm not sure what #lamrin wanted with this question, but I suppose that it is something like this:
<%= link_to_if condition?, image_tag("edit.png", :alt => "Edit"), edit_user_path(user) %>
With this code above, the image would have a link if the condition? is true
In my case this code below worked (a more complicated example):
link_to_unless disabled, (content_tag :div, "", :class => "vote " + vote_class, :title => title), resource_user_path({ :id => resuser.id, :resource_user => {:id => resuser.id, :resource_id => resource_id, :user_id => current_user_id, :vote => vote_value}}), :remote => true, :method => http_method
This link may also help with this approach:
http://railskey.wordpress.com/2012/07/19/rails-link_to-link_to_if-and-link_to_unless/

Unlike buttons, hyperlinks cannot be "disabled". You can do the following though, assuming you have jQuery included on your pages:
<%=link_to image_tag("edit.png", :alt=>"Edit"), edit_user_path(user), :id => "mylink" %>
Add the following Javascript to your page:
$('#mylink').click(function(e){
e.preventDefault();
});

In answer to your question, there is no :disabled option for the link_to helper in Rails, and it is not a valid attribute for a elements either. I believe the reason people tend to get confused with this in Rails is that ":disabled => true" does work IF you are using Bootstrap. So to fix this issue you can either follow Gupta's approach, or just add Bootstrap (which will give you some default CSS as well, so people don't get frustrated trying to click the link)!
Re: link_to method in rails: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to)
Re: the "disabled" attribute on a elements: Is 'disabled' a valid attribute for an anchor tag
Re: Bootstrap "disabled" class or attribute with bootstrap: http://getbootstrap.com/css/#anchor-element-1

1)One solution is to render just image_tag when you do not want link and use link_to when u want link to be click enabled. you can use instance variables to control what to render.
2) or use Javascript as suggested.
Use 2 if you want to dynamically do it.

You may use conditional link_to:
<%=
link_to_if(#current_user.nil?, "Login", { :controller => "sessions", :action => "new" }) do
link_to(#current_user.login, { :controller => "accounts", :action => "show", :id => #current_user })
end
%>

Related

Rails link_to_if Class Doesnt Display Correctly

For the life of me, i cant figure out why this problem is happening. I used the link_to helper all the time, but iv only used the link_to_if helper a few times, and this time I cant get the link to take a CSS class.
Here's my link
<%= link_to_if(step.sequence > 1, raw('<i class="icon-chevron-up"></i>'), url_for(:controller => :test_steps, :action => :update_sequence, :direction => 'up', :id => step.id, :test_script_id => #test_script), { :class => 'btn btn-mini' })%>
The image displays, with no link as expected, but the CSS class defined at the end does not, instead it just has no class. This is the same format I use in all my link_to helpers.
Can anyone explain why?
Using link_to_if, only the name --in your case the result of raw('<i class="icon-chevron-up"></i>')-- will be returned if the condition fails. All options that would otherwise apply to the link tag will be ignored.
See the source.
Try passing an empty hash as the options argument, so that { :class => '...' } is assigned to the html_options argument. Untested
<%= link_to_if(step.sequence > 1, raw('<i class="icon-chevron-up"></i>'), url_for(:controller => :test_steps, :action => :update_sequence, :direction => 'up', :id => step.id, :test_script_id => #test_script), {}, { :class => 'btn btn-mini' })%>
Wrap the link_to_if or link_to_unless in a span:
%span.pull-right
= link_to_unless Foo.deleted.empty?, "<i class='icon-white icon-trash'></i> Undelete Foo".html_safe, deleted_foos_path
Above code sets a css class (pull-right) on whatever is displayed - full link or just its text.

Issue with using link_to :remote - Rails 3

I am using link_to :remote to update one of the div elements on the HTML. This is just a beginner's code. However, the update does not occur on clicking the link. Here is the code:
class SampleController < ApplicationController
def updater
render :text => Time.now
end
end
this is the list.html.erb:
<script type = "text/javascript" src = "/javascripts/prototype.js">
<%= link_to "Hello Testing",
:update => "test_id",
:url => {:action => 'updater'},
:remote => true
%>
<div id="test_id"></div>
So on click the link "Hello Testing", the URL in the browser changes to:
http://localhost:3000/samples?remote=true&update=response_id&url[action]=updater
However, the div element that I am trying to set to the current time does not occur on the UI. What can be the issue here?
Updated the post with:
routes.rb: http://pastebin.com/wmKsa1DD
Generated HTML Code : http://pastebin.com/stU3FpL8
HTML Response in Firebug: http://pastebin.com/WjsB7zAh
Using url_for does not change the behaviour.
please use url_for:
<%= link_to "Hello Testing", url_for(:action => :updater),
:update => "test_id", :remote => true %>
I'm not 100% sure, but I don't think that :update is going to work, since Rails3 now mainly relies on ujs.
The "rails way" to update your div would look like (jquery) :
$('div#test_id').bind('ajax:complete', function(event, xhr) {
$(this).html($.parseJSON(xhr.responseText));
})
1: You should include also rails.js file. Ordinary in Rails it makes like this:
<%= javascript_include_tag :defaults %>
2: I prefer to use this naming for urls: updater_samples_path or [:updater, :samples]
3: You should show your routes. If updater method using not GET method, then you should define it:
<%= link_to "Hello Testing", :update => "test_id", updater_samples_path, :method => :put, :remote => true %>
4: Use FIREBUG to se your AJAX responses. So you can easily debug your app. Also you can show us its response, so we will better understend situation

Convert snippet to Rails 3

I found a wonderful article
But it uses rails version previous to rails 3.
In particular, this snippet:
<%= link_to_remote( "click here",
:update => "time_div",
:url => { :action => :say_when },
:position => "after" ) %>
I converted it to this:
<%=button_to 'Click',:remote=>true,:update=>"time_div",:position=>"after",:action=>"say_when"%>
But, there's something wrong.The entire page is being rendered afresh.
What should be done to make it work as described on that site?
it is not button_to, it is link_to 'Click', :remote => true. Also you have to add csrf_meta_tag in the head tag in the layout for Rails 3. See link
In your layout <%= csrf_meta_tag %> And
link_to "some action", my_action_path(#post), :remote => true
For details Try this link
http://www.themodestrubyist.com/2010/02/24/rails-3-ujs-and-csrf-meta-tags/

Select_tag submitted needs to redirect to show page with :id?

Hi
I have asked a question similar to this before but never got it resolved. So I am trying again.
This seems like it should be so simple. I am not using Rails 3 yet BTW.
All I want to do is have a drop down menu and when a person chooses that location and presses "go" they go to that page.
<% form_tag installation_path([:id]), :url => { :action => "show" }, :method => :get do %>
<%= select_tag :id, options_from_collection_for_select(Installation.find(:all), :id, :name) %>
<%= submit_tag 'Go' %>
<% end %>
This becomes the issue: http://localhost:3000/installations/id?id=1&commit=Create. It can't find the :id. I just don't know how to route this correctly. It seems like this shouldn't be that difficult.
Any help would be great. Thanks.
I think there might be a problem with your form_tag. It seems you're defining the path twice.
Both
installation_path([:id])
and
:url => { :action => "show" }
are used to generate the path but I don't think you should be using both. Just go with
installation_path([:id])
or
:url => { :controller => "installations", :action => "show", :id => id }
You need to create and use a new "show" route that is not based on the installation id (and doesn't collide with Rails resource routes), and continue to send the installation id into the controller's show action as part of the params object.
In routes.rb,
get 'show_installation', to: 'installations#show'
In your view,
<% form_tag show_installation_path, :method => :get %>
...

Simple link_to_remote function - can't get it to work

After utilizing the great trial and error for over an hour along with dozens of tutorials and blogs posting examples, I still cannot get the simple ajax functionality to work. Here is what is in my partial:
<span id="friend">
<%= link_to_remote image_submit_tag("/images/add_as_friend.gif"), :url => {:controller => 'friends', :action => 'add', :id => id} %>
</span>
This is what is in my controller:
class FriendsController < ApplicationController
def add
unless params[:id].nil?
Friend.create(:user_id => #current_user.id, :friend_id => params[:id], :friend_type => 2)
end
end
end
This is what is in my add.rjs file:
page.replace_html 'friend', "A request to be friends with this player has been sent."
The image for the link comes up fine. When I click on it, however, nothing happens. I've checked the database and nothing is going on. I must be missing something, any thoughts?
It may be because you are using image_submit_tag rather than image_tag for the content of your link. image_submit_tag is for submitting forms so will have its own onclick behaviour which may override the onclick behaviour that will be added as part of the link_to_remote functionality.
I would try:
<%= link_to_remote image_tag("/images/add_as_friend.gif"),
:url => {:controller => 'friends', :action => 'add', :id => id} %>

Resources