I would like to add username inside the username text field in my application (vs having a label next to it) the user would be able to type over this text. I've seen it done in html but I'm confused as to how to do it on my new.html.erb page since im using the form_for tag and the text_field tag.
thanks in advanced!
Use "placeholder".
<%= form_for #user do |f| %>
<%= f.text_field :username, :placeholder => "Username" %>
...
<%= f.submit %>
<% end %>
This will only work for html5 supported browsers by the way, so make sure you have jquery fallback for IE 8, 7 etc.
This is achieved by using the HTML5 placeholder attribute.
In rails, you can simply add the placeholder attribute:
<%= f.text_field :whatever, :placeholder => "text for placeholder..." %>
Related
I have this form field:
<%= f.url_field :url, class: "form-control", placeholder: "Website" %>
I'd like to be able to have the user impute a URL and then have it automatically post as a clickable link. Right now all it does is submit a string that you'd have to copy and past.
Any suggestions?
Based on your comment: Change the index view to render a link instead of rendering plain text like so.
<%= link_to startup.url, startup.url %>
Or, if you're not forcing them to enter http:// in the form you can do
<%= link_to startup.url, "http://#{startup.url}" %>
I am trying to add a character counter to an app I am helping with but the app uses simple form gem for the page i need to work with. The Gem seems pretty cool expect that it auto generates the html and i need to figure out how to add my custom div after the label and the input so i can add the character count and change it as it goes.
I can do this with jquery but i am wanting to do an initial hard coded count for initial load just in case the user has an old browser without javascript.
<%= simple_form_for #organization do |f| %>
<div class="form-inputs">
<%= f.simple_fields_for :projects do |project| %>
<div class="row">
<%= project.input :description, :placeholder => 'What does your project do?', input_html: { class: 'project-description' }, :maxlength => 255 %>
<% end %>
I want to add the character limit to the description tag.
I'm developing a simple rails app where I want to plot some stock charts. The problem is that when I start my server and load localhost the default value/ticker symbol is not loading which means that I have to type in a ticker in my form for it to work.
I found this thread where I learnt how to write a default value in my form/view, like so:
<%= form_for :find_it do |f| %>
Ticker symbol: <%= f.text_field :string, :value => "JPM" %></br>
<%= f.submit "Find" %>
<% end %>
and that's all fine, but it does not submit the value by default.
So how do I go about fixing this and what is the best practice?
In your input field you have list your attribute as a string, while that is the type, it most likely isn't the actual name of the attribute you wish to save "JPM". So you should change
<%= f.text_field :string, :value => "JPM" %>
to
<%= f.text_field :attribute_name, :value => "JPM" %>
If I copy and paste your form into a Rails app on my machine it does display a text field populated with "JPM", which I believe is correct.
When you hit submit the form will post to a create action with params containing:
"find_it"=>{
"string"=>"JPM"
}
Another thing I noticed is that you have f.text_field :string. This should be the name of your attribute, rather than the type (i'm assuming that you don't have a field called string).
I have been recently working with Ruby on Rails and have run into an issue that I can not quite figure out. I need to create a bunch of form mockups, that do not function. That is they should have the submit button, but it should not do anything upon being clicked. Normally using html I would do something along the lines of
<form action="#">
</form>
Trying to convert this to use Rails form helpers, I have done the following
<%= form_tag "#" do %>
<%= label_tag :username, "Username: " %>
<%= text_field_tag :username %>
<br />
<%= label_tag :password, "Password: " %>
<%= password_field_tag :password %>
<br />
<%= submit_tag "Login" %>
<% end %>
This generates a form that is similar to what I want to achieve, however when clicking the submit button it tries to access /# via post which is not the desired result. Currently the only thing I can think of to achieve this is to set the disabled attribute of the button, but is there a better way?
Unfortunately this can't be achieved with form helpers. Defining a form_for or a form_tag requires an action for the form. You can set
:action => "#"
But this will require including the action in routes -> having a controller with action for it -> rendering some page yet again.
You could manipulate the form after loading with javascript however (sust remember to set :remote to true - ). Or alternatively, if you insist on using the form helpers - replace the submit_tag with a button_tag:
<%= button_tag "Login", :type => 'button'%>
Try
<% form_tag "#", :onSubmit => "return false" do %>
Have you tried with button_tag instead of submit_tag? See here. Just make sure you don't use the default, or you will be right back where you started.
I'm looking to include a link in a form label as such:
<%= form.check_box 'eula' %>
<%= form.label 'eula', "I agree to the <a href='#' id='eula-link'>EULA</a>", class: 'label-checkbox' %>
Rails writes the HTML out, as it probably should, but how would I accomplish this? Clicking EULA opens a JS popup. I was thinking of embedding a link_to in there somehow ?
Using html_safe with parens will render the html, like so:
<%= f.input :eula, :as => :boolean, label: ("I agree to the #{link_to 'Terms of Service', terms_path}.").html_safe %>
Assuming you're using vanilla rails form helpers, you can do this:
f.label :eula do
'I agree to the #{link_to("EULA", "#")}'
end
Source: http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-label
19 ways tried, with either the hyperlink being encoded or html_safe replacing hyphens in the url ???
This is what worked for me
<%= f.label :cookies do
"Do you agree to our #{link_to('Cookies Policy', 'http://www.your-url.co.uk/privacypolicy')}".html_safe
end %>
The specific use of " and ' appears significant.
The answer from jenson-button-event nearly worked for me, but required a change in the placement of a parenthesis close to load without errors.
For me the below solved it. Note the close-bracket after 'Cookies Policy' here, rather than after the link path itself.
<%= f.label :cookies do
"Do you agree to our #{link_to('Cookies Policy'), 'http://www.your- url.co.uk/privacypolicy'}".html_safe
end %>
try "I agree to the #{link_to 'EULA', #, :id => 'eula-link'}"
I wanted a simple way to add a font awesome help link button after the form field label, this is what I used in my haml file:
= form_for [#preplan, #structure] do |f|
= f.label :template do
Template
= link_to 'https://intercom.help/blazemark/preplans-and-structures/structure-templates', target: '_blank' do
= fa_icon 'fw info-circle'
As of rails 6.0.2.1 (January 2020), this is what worked for me:
<div class="form-group form-check">
<%= form.check_box :accept_terms, class: "form-check-input", required: true %>
<%= form.label :accept_terms, class: "form-check-label" do %>
<span>
Accept <%= link_to 'Terms and Conditions', 'https://your.url.here.com' %>
</span>
<% end %>
</div>