I am using button_to and link_to form helpers in Rails. I need to set an html attribute based on a condition in code. More precisely, I need to disable a button_to component only if a certain condition is true.
How can I achieve that? code snippet below.
<%= button_to "Submit", "#", :id=> "submit", :class => "button_white_big", :disabled => true%>
In above statement I may need :disabled=>true or I may not need it.
Is there a clean way, apart from duplicating the code line?
Cheers
-Priyank
You can replace the "true" in the disabled with the condition (really any expression that returns a boolean) to get the functionality you need. Like this:
<%= button_to "Submit", "#", :id=> "submit", :class => "button_white_big", :disabled => obj.disabled? %>
or
<%= button_to "Submit", "#", :id=> "submit", :class => "button_white_big", :disabled => x == y %>
etc.
What is the condition that is true or false?
If it's triggered in the controller, you can set the disabledbutton value, or write a helper, which would take the condition as a variable, and print out the result.
<%= button_to "Submit", "#", :id=> "submit", :class => "button_white_big", #disabled %>
is_disabled? ? #disabled = {:disabled => 'disabled'} : #disabled = false
Related
I'm following this SE question to put a form in a bootstrap modal with rails.
The person who answered the question states: "Make sure that you have a unique id or class for every modal body.". So I am trying to put a unique id number in my link_to:
<%= link_to "Edit", edit_post_path(post.id), :class => "btn", :remote => true, "data-toggle" => "modal", "data-target" => "<%= post.id %>-my-modal" %>
But this is causing an error. If I take out <%= post.id %> I do not have an error, but the modal behavior does not work.
How can I add the post.id with embedded ruby to the link to?
You have to write it like this:
<%= link_to "Edit", edit_post_path(post.id), :class => "btn", :remote => true, "data-toggle" => "modal", "data-target" => "#{post.id}-my-modal" %>
Once you open a <% ... %> tag, you are writing ruby code. What this means is that you can't nest <% ... %> tags inside another <% ... %> tag because these tags aren't ruby syntax.
Inside the tag, to do string interpolation, use normal ruby methods:
<%= link_to "Edit", edit_post_path(post.id), :class => "btn", :remote => true, "data-toggle" => "modal", "data-target" => "#{post.id}-my-modal" %>
Only mistake is putting another (ruby) tag <% %> inside the same tag.
In erb (embedded ruby) one <%= %> tag cannot have another tag inside.
This code result a syntax error.
<%= link_to "Edit", .... "data-target" => "<%= post.id %>-my-modal" %>
If you plan to output/render data inside the tag and quoted as string,
used #{put_data_hare}
Output like:
<%= link_to "Edit", .... "data-target" => "#{post.id}-my-modal" %>
In other case, you can do like:
post.id.to_s + "-my-modal"
I was wondering if this is possible. I have seen stack questions ask this and the answer seems to be:
<%= link_to 'Manage', '/manage?id='+blog.id.to_s+'#dashboard', :class => 'btn btn-primary', :data-no-turbolink => false %>
But I get the error:
undefined local variable or method `no' for #<#<Class:0x007f8d5270a440>:0x007f8d54a5af30>
So I am wondering what the actual answer is for this. How do you say "don't use turbo links when following this link.
You are using wrong syntax. Try:
<%= link_to 'Manage', '/manage?id='+blog.id.to_s+'#dashboard', :class => 'btn btn-primary', "data-no-turbolink" => false %>
OR
<%= link_to 'Manage', '/manage?id='+blog.id.to_s+'#dashboard', :class => 'btn btn-primary', data: {no_turbolink: false} %>
I would like to be able to add an additional query string parameter on submit that is the same as the value of the classrooms_search_textbox that the user will type. How do I do this?
<%= form_tag classrooms_path, :method => :get, :id => "classrooms_search_form" do %>
<%= text_field_tag "classrooms_search_textbox", "Find a classroom" %>
<%= submit_tag "Find", :id => "classrooms_search_button", :class => "button" %>
Do I need to add a hidden_tag (and if so, how would I go about doing this?) or can I just add to the classrooms_path somehow?
Thanks!
Since you're sending it your controller first, then you can just manipulate the params in your controller method before sending it off:
params[:classrooms_query] = params[:classrooms_search_textbox]
And then go ahead and use those params to send off to the other service. There's no need to add hidden field tags or use some fancy JS code.
<%= form_tag classrooms_path, :method => :get, :id => "classrooms_search_form" do %>
<%= text_field_tag "classrooms_search_textbox", "Find a classroom" %>
<%= hidden_field_tag "classrooms_query" %>
<%= submit_tag "Find", :id => "classrooms_search_button", :class => "button" %>
$('#classrooms_search_form').submit(function() {
$('#classrooms_query').value($(classrooms_search_textbox.value());
});
That would achieve what you want. Nonetheless, maybe it is in your interest to refactor the controller or view so that it doesn't have this kind of conflicts.
Say I have two buttons in my model's index view that are used to create a new instance of the model. I want to pass the variable :number to my controller and use it in the new function so I can alter my form slightly depending on which button was pressed. How can I access :number in the controller?
<%= link_to 'New Run 1', new_test_suite_run_path, :class => "btn btn-custom1" , :number => 1 %>
<%= link_to 'New Run 2', new_test_suite_run_path, :class => "btn btn-custom1", :number => 2 %>
Do this instead
<%= link_to 'New Run 1', new_test_suite_run_path(:number => 1), :class => "btn btn-custom1" %>
And then in your controller my_number = params[:number]
Rails 2.3.5
For a "link_to" tag, I'm trying to pin down the syntax for sending extra parameters and specifying a class. I'm using the jQuery UI library to change links into buttons with a class of 'link_button'.
This sends the extra 'min_max' parameter, but the class will not be applied:
<%= link_to "CLICK HERE", :action => 'edit', :id => #threshold_control.id, :min_max => 'different', :class => 'link_button' %>
This is not sending the extra 'min_max' parameter, but the 'link_button' class is applied:
<%= link_to 'CLICK HERE',edit_threshold_control_path(#threshold_control.id), :min_max => 'different', :class => 'link_button' %>
I haven't seen a specific example of extra link_to parameters AND a class specified, and none of my guesses at the syntax needed for both things to work at the same time have worked. Thanks for any help.
Try:
<%= link_to "CLICK HERE", { :action => 'edit', :id => #threshold_control.id, :min_max => 'different' }, { :class => 'link_button' } %>
link_to expects two hashes after the name of the link. If you don't use curly braces there is no way to know when the first hash ends and the second hash starts.
If anyone needs to put html inside the link text as I did, here's the alternative.
<%= link_to(options = { :action => 'edit', :id => #threshold_control.id, :min_max => 'different' }, html_options = { :class => 'link_button' }) do %>
HTML HERE
<%end%>
link to documentation here