my rails checkbox is always checked in the dom. How can I make it so when the page loads it's checked if the object is true or not?
= f.check_box :split_funding, {class: "split_funding", :disabled => #survey.completed?, id: "split_funding_#{school.id}", "data-attribute" => "split_funding", "data-id" => school.id}
Pass it the checked attribute like so:
= f.check_box :split_funding, {checked: #survey.split_funding, class: "split_funding", :disabled => #survey.completed?, id: "split_funding_#{school.id}", "data-attribute" => "split_funding", "data-id" => school.id}
I'm not sure what you mean by 'if the object is true', so I am assuming that :split_funding will return true/false. If it's a relation, you can chain :present? on the end to check for presence of the relation.
Related
I want to show a "This field is required" if a person has not selected any option in options field.I am trying to make :required => true work.
Please find below the code line. Also if there is no direct way it would be great if someone can point out to any java script which can be used in onChange callback to make things work.
<%= select_tag('params[type]',
options_for_select({"abc" => "ABC","xyz" => "XYZ}, nil),
:required => true ,
:class => "fk-select-box",
:multiple => true,
:tabindex => "4",
:prompt => "Select Type") %>
You can pass javascript function like this on change event of select box,
function Change_selectbox()
{
var selectboxid = document.getElementsByClassName("Selectboxid")[0];
var selectboxchange = selectboxid.options[selectboxid.selectedIndex].value;
if (selectboxchange === "nil")
{
document.getElementById("errors_span_id").innerHTML=("<center>This field is required");
document.getElementsByClassName("selectboxid")[0].focus();
return false;
}
}
Other best option you should use model validation.
I have a gravatar method in the User_helper of a Rails3 application. The code is below:
module UsersHelper
def gravatar_for(user, options = {:size => 50})
gravatar_image_tag(user.email.downcase, :alt => user.name,
:class => "gravatar",
:gravatar => options)
end
end
Currently this method sets the default size as 50px yet this is changed in some implementations throughout the app. I want the gravatar to become part of the "round" class (as well as the "gravatar" class) when the size of the gravatar is greater than 30px. How would I do this?
Thanks in advance :)
module UsersHelper
def gravatar_for(user, options = {:size => 50})
options[:size] > 30 ? #class = "gravatar round" : #class = "gravatar"
gravatar_image_tag(user.email.downcase, :alt => user.name,
:class => #class,
:gravatar => options)
end
end
Can someone tell me how to set these check boxes to checked? I'm sure it's simple, but after an hour of trying i think I need to ask! Thanks!
= form_tag movies_path, :id => 'ratings_form', :method => :get do
Include:
- #all_ratings.each do |rating|
= rating
= check_box_tag "ratings[#{rating}]",
= submit_tag 'Refresh', :id => 'ratings_submit'
Ref check_box_tag
check_box_tag "ratings[#{rating}]", 1, !!(rating.rating)
Your 2nd parameter must be value of checkbox
Your 3rd parameter must be a boolean condition which return true/false and depends on it checkbox is checked/unchecked
check_box_tag "ratings[#{rating}]", 1, #selected.include?("#{rating}")
where #selected is an array with the element selected.
According to the api dock, check box tag takes the following options:
check_box_tag(name, value = "1", checked = false, options = {})
This means the first value is the name, the second value is a 'value' and the third value is whether the box is checked, which is default to false. So, in order to check or uncheck the box you can do the following:
- if (some condition)
= check_box_tag "ratings[#{rating}]", "anystring", true
- else
= check_box_tag "ratings[#{rating}]"
The second line just puts a random string into the value field because in this case it does not matter.
Use true for Checked or false for Unchecked at the end of the line
check_box_tag "ratings[#{rating}]", true #checked
or
check_box_tag "ratings[#{rating}]", false #unchecked
Building on the answer by Sali. Strangely, the check_box_tag returns a checkbox with no label text. Here's how you can display text if you're iterating over an array.
- Puppies.each do |puppy|
= check_box_tag(puppy.name, puppy.name, puppy.goodboy?)
= puppy.name
= check_box_tag "ratings[#{rating}]",{},{:checked => ""}
i'm having a bit of trouble with adding a certain feature. i'm working on a buy/sell site and i want to be able to compare posts. here's what i have so far:
in the posts view:
<%= button_to "Add to Compare", :action => "addCompare" %>
in the corresponding controller:
##a = Array.new()
def addCompare
##a << Post.id
end
so, all i want to do is add the post's id to the array ##a. when i test this, i click on the "Add to Compare" button and I'm welcomed with this:
Template is missing
Missing template posts/addCompare with {:locale=>[:en, :en], :formats=>[:html], :handlers=>[:rxml, :rjs, :builder, :rhtml, :erb]} in view paths "/home/mja32/470repo/traders/app/views", "/var/lib/gems/1.8/gems/devise-1.4.2/app/views"
So I guess it's trying to redirect to a view. How do I prevent it from doing this? All I want this button to do is to add the post's id to the array and nothing more.
Thanks in advance,
Matt
First of all, storing persistent data in a controller's class variable isn't going to work the way you want it to. There's no guarantee that ##a will be the same array on your next addCompare call; for example, your next addCompare call could be handled by a different process. Also, what happens if two different clients call addCompare? Do you really want to mix their data together in one pile? Probably not. Your first task is to replace ##a with a real per-user persistent store.
If you want to return nothing at all from your controller, just do this at the end of your controller method:
render :nothing => true, :status => :ok
That will tell Rails that something has already been rendered so it doesn't need to try the default rendering action (which is to render the posts/addCompare view) and returns nothing more than a 200 status code to the client.
Once that's in place, you'll probably want to AJAXify your button with :remote => true:
:remote - If set to true, will allow the Unobtrusive JavaScript drivers to control the submit behaviour. By default this behaviour is an ajax submit.
So this:
<%= button_to "Add to Compare", { :action => "addCompare" }, { :remote => true } %>
Note that button_to looks like this:
button_to(name, options = {}, html_options = {})
and that :action is for options but :remote is for html_options so you have to explicitly set up the hashes with {}; you could just wrap the options in braces:
<%= button_to "Add to Compare", { :action => "addCompare" }, :remote => true %>
but I prefer the consistency of wrapping them both by hand.
It's obvious from the documentation (and google) how to generate a link with a segment e.g. podcast/5#comments. You just pass a value for :anchor to link_to.
My concern is about the much simpler task of generating the <a name="comments">Comments</a> tag i.e. the destination of the first link.
I've tried the following, and although they seemed to work, the markup was not what I expected:
link_to "Comments", :name => "comments"
link_to "Comments", :anchor => "comments"
I think I'm missing something obvious. Thanks.
You are getting confused by Ruby's syntactic sugar (which Rails uses profusely). Let me explain this briefly before answering your question.
When a ruby function takes a single parameter that is a hash:
def foo(options)
#options is a hash with parameters inside
end
You can 'forget' to put the parenthesis/brackets, and call it like this:
foo :param => value, :param2 => value
Ruby will fill out the blanks and understand that what you are trying to accomplish is this:
foo({:param => value, :param2 => value})
Now, to your question: link_to takes two optional hashes - one is called options and the other html_options. You can imagine it defined like this (this is an approximation, it is much more complex)
def link_to(name, options, html_options)
...
end
Now, if you invoke it this way:
link_to 'Comments', :name => 'Comments'
Ruby will get a little confused. It will try to "fill out the blanks" for you, but incorrectly:
link_to('Comments', {:name => 'Comments'}, {}) # incorrect
It will think that name => 'Comments' part belongs to options, not to html_options!
You have to help ruby by filling up the blanks yourself. Put all the parenthesis in place and it will behave as expected:
link_to('Comments', {}, {:name => 'Comments'}) # correct
You can actually remove the last set of brackets if you want:
link_to("Comments", {}, :name => "comments") # also correct
In order to use html_options, you must leave the first set of brackets, though. For example, you will need to do this for a link with confirmation message and name:
link_to("Comments", {:confirm => 'Sure?'}, :name => "comments")
Other rails helpers have a similar construction (i.e. form_for, collection_select) so you should learn this technique. In doubt, just add all the parenthesis.
If you want to go through rails, I suggest content_tag (docs).
Example:
content_tag(:a, 'Comments', :name => 'comments')
<%= link_to('new button', action: 'login' , class: "text-center") %>
created an anchor tag for login.html i.g
new button
and for
new button
use
<%= link_to('new button', controller: 'admin',
action: 'login' , class: "text-center") %>