I have an application that I have been working on and have everything wired up to my liking but was wondering if this is possible:
I am displaying a users username in an edit.html.erb template. I want it to display the users username inside of the <%= f.text_field :username, :class=> "uneditable-input" %>. I am using Bootstrap and in the documentation, it states to add a class of uneditable-input, when I hove rover the username text field, it shows a little white stop sign but I am still able to click on the field and edit it. Any help?
<%= render 'shared_partials/errors', errors_object: #user %>
<div class="page-header">
<h2> Edit Your Profile<small> - <%= #user.username %></h2></small>
</div>
<div class="well">
<%= form_for #user do |f| %>
<%= f.label :username %>
<%= f.text_field :username, :class=> "uneditable-input" %>
<%= f.label :password %>
<%= f.password_field :password, :class=> "input", :placeholder=>"Password goes here" %><br/>
<%= f.submit "Update Changes", class: 'btn btn-primary' %>
<% end %>
</div>
Make the field disabled.
<%= f.text_field :username, :class=> "uneditable-input", :disabled => true %>
While submitting the form, browser will not send the value for the field username and thats correct, why it should be sent if it is un-editable.
Related
Without making a custom Sessions controller that inherits from the hidden devise controller, I want to figure out a way of adding the parameter, "g-recaptcha-response" to be included in the authenticating process from the default devise view.
Here's my view with the recaptcha figure included. When I click on it I do get a response when my app calls the SessionController#create method. I'm trying to figure out how to make this process detect that the captcha form has been clicked and authenticates the user logging in because of it.
<h2>Log in</h2>
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<% if devise_mapping.rememberable? -%>
<div class="field">
<%= f.check_box :remember_me %>
<%= f.label :remember_me %>
</div>
<% end -%>
<div class="g-recaptcha" data-sitekey="6LeeL5gUAAAAADbq0vt4d9biAs8oegkPx9ttUkKb"></div>
<div class="actions">
<%= f.submit "Log in" %>
</div>
<% end %>
<%= link_to 'Sign Up', new_user_registration_path %>
In essence, if a bot were to attempt automatically signing in repeatedly, the devise controller would see that the captcha was not clicked and stops them from trying to log in.
We're using the colorbox-rails gem to show a sign-up page to our users for our Rails 4 application. At the bottom of the popup, we have a link to that saysAlready a member? Login. The Login text redirects the user to our login page.
However, because Rails 4 is using turbolinks, the Login page's html is being loaded with AJAX in the background. This has the effect of loading the Login page behind the colorbox, while the colorbox still remains open.
Here is the html that triggers the colorbox:
<%= link_to 'Sign Up', '/blank/sign_in_helper', class: 'nav-link-box',
:id => 'sign-up-link', :data => {
:colorbox => true}
%>
And the HTML of the colorbox (sign_in_helper):
<h2>Sign Up</h2>
<%= form_for(:user, url: registration_path(:user)) do |f| %>
<div class="field">
<%= f.label :name, 'Display Name' %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %>
<% if #minimum_password_length %>
<em>(<%= #minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<p>Already a member? <%= link_to 'Login.', new_user_session_path, class: 'lightbox-close' %> </p>
<% end %>
I've tried binding a colorbox close event to the link (coffeescript), but for some reason it only works the second time the link is clicked:
$('.lightbox-close').click ->
$.colorbox.close()
Is there a way to do this while still using turbo-links? Or is the best way to disable turbo-links for that specific link?
Thanks!
If you want to disable turbolinks for a certain link add data-no-turbolinks attribute to it
<%= link_to 'Login.', new_user_session_path, class: 'lightbox-close', data: {no_turbolinks: true} %>
When using turbo links with javascript bindings it's better to execute the code that you used to on document.ready to page:load, for example (given that you're using jquery) in your application.js:
var ready;
ready = function(event) {
//code when the page is loaded
};
$(document).on('ready page:load', ready);
I'm making a Rails web app with the limited knowledge I have - mostly from the Hartl Rails Tutorial. I'm also using Bootstrap.
I decided it would be good to have a dropdown login form in the navbar, in addition to the form at '/login'. The form looks nice enough, but I can't get it to work.
Here is my current code for my working, '/login' form:
<%= form_for(:session, url: login_path) do |f| %>
<div class="form-group">
<%= f.label :username %>
<%= f.email_field :username, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
</div>
<%= f.submit "Log in", class: "btn btn-primary center-block" %>
<% end %>
Here's my code for the non-working navbar form:
<%= form_for(:session, url: login_path, method: :post,
html: { role: "form", id: "navbar-login" }) do |f| %>
<div class="form-group-sm">
<%= f.label :username %>
<%= f.text_field :username, class: 'form-control',
name: 'username', placeholder: 'Username' %>
</div>
<div class="form-group-sm">
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control',
id: 'password', name: 'password',
placeholder: 'Password' %>
<%= link_to "Forgot password?", '#' %>
</div>
<%= f.submit "Log in", class: "center-block" %>
<% end %>
And here's my sessions_controller.rb create method:
def create
user = User.find_by(username: params[:session][:username])
if user && user.authenticate(params[:session][:password])
log_in user
redirect_to user
else
flash.now[:danger] = "That email and password combination isn't
correct."
render 'new'
end
end
The error I get when submitting the form is as follows:
NoMethodError in SessionsController#create
undefined method `[]' for nil:NilClass
The error also points to this line of the create method:
user = User.find_by(username: params[:session][:username])
I guess the answer is pretty simple, but I can't quite figure it out. I'm sure it must be either form_for or the create method that isn't working.
Thanks in advance for any help.
Remove the name attribute from every field in the form.
Rails handles it for you and the way you are naming it is not creating the param that you expect.
I want to make a button that will allow me to create a new text_field for :phone_number in addition to the one I already have. For example if I want to submit two phone number instead of one in separate fields how would that be accomplished? In the code below I have begun to make a button_to but am not sure what actions to take in order to make a new field of entry. thanks.
<div>
<%= form_for #reminder do |f|%>
<%= f.label "Your Reminder"%>
<%= f.text_area :text %>
<!-- button here to make new text_field to input in an extra phone number -->
<%=button_to "add another number", %>
<%= f.label "Phone Number" %>
<%= f.text_field :phone_number %>
<%= f.label "When to be sent" %>
<%= f.text_field :time%>
<%= f.label "Picture URL" %>
<%= f.text_field :picture %>
<%= f.label "Favorite?" %>
<%= f.check_box :favorite %>
<%= f.submit %>
<% end %>
</div>
You can accomplish this using JavaScript. Get the id of the button and then add the new input to the page when clicked.
My Code in View Page(views/session/new.html.erb) :
< %= form_for(:session, :url => sessions_path) do |f| %>
<%= f.label :email, 'Username'%>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.submit 'Login'%>
<%= link_to 'Forgot Your Password?', new_password_reset_path >
< % end %>
I have written proper validation codes in my model file.
But I want that on Submit, the validation errors should pop up at the adjacent to the fields rather than at the top of the Page.
Thanks in Advance !
If you're inclined to use Rails' built-in server side validations, you can check for whether validation errors exist, then print them if they do:
<%= form_for(:session, :url => sessions_path) do |f| %>
<%= f.label :email, 'Username'%>
<%= f.text_field :email %>
<% if f.object.errors[:email] %>
<%= f.object.errors[:email].join(", ") %>
<% end %>
<%= f.label :password %>
<%= f.password_field :password %>
<% if f.object.errors[:password] %>
<%= f.object.errors[:password].join(", ") %>
<% end %>
<%= f.submit 'Login'%>
<%= link_to 'Forgot Your Password?', new_password_reset_path >
<% end %>
Note a few things:
First, because you're passing a symbol, rather than an object in your form, you'll need to access the object's attributes from within the form_for block by invoking f.object.
Second, because some fields may yield more than one validation error, you'll want to print them out in sequence. Using Ruby's join method can help you concatenate these in a coherent manner.
UPDATE:
If you want the errors to render directly beside the corresponding text field, you'll need to apply some CSS. Clearly, it's recommended that a stylesheet be used to for this, but in the interest of simplicity, here's of an example of how this might be rendered using inline styling:
<%= f.text_field :email, :address, :style => "display: inline-block; float: left;" %>
<% if f.object.errors[:email] %>
<span style="display: inline-block; float: left;">
<%= f.object.errors[:email].join(", ") %>
</span>
<% end %>
<div style="clear: left;"></div>
I'd go with something like this:
<%= form_for(:session, :url => sessions_path) do |f| %>
<%= f.label :email, 'Username' %>
<%= f.text_field :email %>
<%= f.object.errors[:email].to_sentence if f.object.errors[:email] %>
<% end %>
I reckon you'll figure out the rest.
You can access the values of error messages of different model attributes with errors hash like errors[:some_attribute_name]. As long as your custom validators store error message in the right model attribute, you can probably do something like this.
< %= form_for(:session, :url => sessions_path) do |f| %>
<%= f.label :email, 'Username'%>
<%= f.text_field :email %>
<%= f.object.errors[:email] %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.object.errors[:password] %>
<%= f.submit 'Login'%>
<%= link_to 'Forgot Your Password?', new_password_reset_path >
< % end %>
I'll let you worry about how the error is displayed beside the form and stuff. :)