Ruby on Rails looking for value from key in devise gem - ruby-on-rails

I'm new to ruby on rails, and I installed devise on my application. I was wondering where I can customize the values if a value is given in views/devise/registrations/new.html.erb
for example:
<%= f.input :password_confirmation, :required => true %>
this gives out an input and a label, the label is * Password confirmation. How do I customize this label and the input field?
Thanks

<%= f.input :password_confirmation, :required => true, :label => 'something', :class => 'something' %>
Define the something class in CSS and you'll be fine.
And of course, generate the partials via copying from devise directly or using its generators. I usually copy them from devise repo since I am not that much fan of generators, sometimes they generate files I don't even need or want.

rails generate devise:views users
You will need to generate the partials first. After that you can use those generated files to override the default ones. Also, it might be good to build the authentication from scratch the first time in order to better understand the process.
http://railscasts.com/episodes/250-authentication-from-scratch

Related

How do I get simple_form and bootstrap to work together?

I'm using simple_form to generate my forms in my Rails application. I'm wanting to use bootstrap with these forms. So I've installed the following gems and added them to my gemfile... (1)simple_form, (2)twitter-bootstrap-rails. Finally, per instructions on the simple_form git page I ran, rails generate simple_form:install --bootstrap.
After running bundle install everything is installed and the server runs. The forms however are still not displaying correctly as they would with the class "form-control".
Form
= simple_form_for #user, html: { multipart: :true, class: "form-horizontal" } do |f|
= f.input :headshot_image, as: :file
= f.input :remote_headshot_image_url, placeholder: "Image Url"
= f.input :first_name, required: true
= f.input :middle_name
= f.input :last_name, required: true
I am able to get it to "work" by two different methods (neither of which I believe are correct). First I can add , class: "form-control" to each input, or I can add to the simple_form_for :defaults => { :input_html => { :class => "form-control" } }. The former which kinda defeats the purpose I believe, and the latter works, but it also applies the class to the file input which is not ideal.
Is there a setup step that I missed? Or have I done something incorrect. If I need to provide more information or left something out please let me know. Thanks!
Probably you forgot about adding bootstrap assets to application.css file
simple_form Readme has following line:
You have to be sure that you added a copy of the Bootstrap assets on your application.

How can I turn off autocomplete in the activeadmin login form

To comply with a WAST security report I need to switch off autocomplete in the activeadmin devise views for the admin user email and password.
I can do this easily on my user login views as I've generated the html.erb files from devise and modified them, can't see an easy way to do this with the activeadmin views though.
I've checked for a setting and can't find one, any ideas?
You can add the attribute directly from the register block:
form(html: { autocomplete: :off })
or for the individual fields:
f.input :email, input_html: { autocomplete: :off }
You can create a new form by overriding the old one.
Create
/views/active_admin/devise/sessions/new.html.erb
The current file can be seen here

Ruby on Rails, formtastic gives different HTML

I have this template:
- f.inputs do
= user.input :is_vip?, :as => :boolean, :label=>'VIP'
= f.input :test, :as => :boolean, :required => false, :label => "This is TEST company"
= f.input :multi_destination, :as => :boolean, :required => false, :label => "Multi destination"
It's fully working thing. But the code that the browser receive is different in my localhost and in stage server. I don't know where to dig.
CSSes are exactly the same, sources are same, everything is the same.
Formtastic, by default, uses a method named label_with_nested_checkbox for rendering boolean input fields. That method renders the checkbox inside the label - like you are getting in localhost.
Formtastic doesn't have any "default way" to "take the input field out of the label" (his author confirmed this to me on this question) It must have been patched somehow to do that. Here're some possible places to look:
Check inside the apps/inputs directory, if there is any. That's the "standard" place where someone can modify Formtastic plugins
Check the config/initializers directory. To see if there is any Formtastic monkeypatching there.
Check the vendors directory.
Finally, even if the gem versions are the same, some could (grasp!) have changed the code of the gem itself in the server. I certainly hope they nave not. But anyway, uninstalling and reinstalling the gem, and maybe rebooting the server, should check that one out.

Forcing "humanized" names of fields to be lowercase in Rails 3

As far as I know, the accepted way to set the "humanized" names of fields in Rails 3 is to use locales:
# config/locales/en.yml
en:
activerecord:
attributes:
member:
username: 'username' # rather than 'Username'
However, I simply want Rails 3 to use lowercase versions of its default humanized names. Is there an easy, built-in way to do this?
An example to help clarify: When inside of a form_for, <%= f.label :username %> displays "Username" by default. I want it to display "username".
I had the same problem.
I solved it via css:
In foo.html.erb:
<%= f.label :username, :class => "my_class" %>
In bar.css:
label.my_class {
text-transform: lowercase;
}
I would prefer a different solution, too. But that's the only one I've been able to find, so far.
The label helper defaults to using human_attribute_name to turn an attribute into a human name. If you look at the source, human_attribute_name tries a few things before falling back to attributes.to_s.humanize. It tries the translation first, and then it looks for a :default option in the options hash.
So, the simplest/best way to get the functionality you want is to override human_attribute_name with your own that provides a :default option and then calls the original. Rails provides a reasonable way to do this sort of thing with alias_method_chain, so...
I've heard enough, just give me the answer!
Put the following in any file in config/initializers and restart your app:
module ActiveModel
module Translation
def human_attribute_name_with_foo attribute, options = {}
human_attribute_name_without_foo attribute, options.merge( :default => attribute.humanize.downcase )
end
alias_method_chain :human_attribute_name, :foo
end
end

Formtastic non-model form, integration with external site, override/specify the input ID values

I'm using formtastic to collect information from a form and post dirctly to an external site.
I have no problem generating the form itself. However, since this is being submitted to an external site, they require that each input field have the specific IDs they specify, eg email or last_name -- not the closest Formtastic form, eg _email_input or _last_name_input.
I've looked at the Formtastic v1.2.3 code and I'm 90% sure the answer is "sorry, can't do that." I figured it couldn't hurt to check if I'm missing something. I would like some way to specify the ID completely, as in:
= semantic_form_for('', :url => "https://external_site.com/handler, :method => "post") do |form|
= form.input :last_name, :id => "last_name"
[etc]
Is this possible?
(I will note that I recognize that another, arguably superior approach would be to create an appropriate controller, sanity check the parameters locally, and dispatch the remote call from within the app only when it's well formed; however, that's not what I'm trying to do at the moment.)
Firstly i think you need to use semantic_fields_for for non-model forms. Next, to pass ids to each field, you can use the input_html options to specify them. for eg
form.input :email, :input_html => {:name => 'email', :id => 'email' }

Resources