I have a model that contains required and free fields. Between the required items is also item with password.
In the _form.html.erb I hid the input with a password, so when the user want to update the form, so he will be update the all items except password => that's what I want. But because the item password is require, so I will get an error about bad validations.
I would like to ask you, if exist a good way to do this... I can't find some trick for it
I think it's :
validates_presence_of :password, :on => :create
Related
Would like to display a message when click any product edit page if somebody
already opened/in that same record/page.
how to track users access on edit action for same record?ultimately i want to display message as "somebody already editing this product" to avoid overwrite
process between multiple users.
please share your ideas on it.
Naive solution for this might be adding an association say:
class Product < ActiveRecord::Base
belongs_to :edited_by, class_name: 'User', foreign_key: 'edited_by_user_id'
end
class User < ActiveRecord::Base
has_many :edits, class_name: 'Product', foreign_key: 'edited_by_user_id'
end
and then:
First comes on /edit page, set product.edited_by to that user.
Second user visits /edit page. You can check if edited_by for that product is set, then show him the message; blocking him to update the same product.
Remove the edited_by when user has updated the record.
But this comes at cost. There are lot of corner cases around this:
User might come on edit page and acquire edited_by association. But never update the record (thus never giving another user to update the product)
In case of some logic exception, edited_by might never be reset.
Involves devising a strategy when to reset if case #1 occurs.
Thus, I advise using Optimistic Locking instead. You can add a migration with :lock_version for your Product model. This will prevent the stale object from ever being saved in this scenario, preventing such conflicts.
It will raise the exception ActiveRecord::StaleObjectError when User #2(or first) tries to update an old record, which you can use to show your custom message by handling that exeption.
Hope that helps :)
I have 2 different sign up pages one for normal user and one for fundraiser. for creating user I am using Devise and have normal fields as Name, email and password.
For fundraiser these fields are common with some extra fields so for that I am using this in user.rb
attr_accessible :campaign_fundraisers_attributes
has_one :campaign_fundraisers
accepts_nested_attributes_for :campaign_fundraisers, allow_destroy: true
this in CampaignFundraiser.rb
belongs_to :user
and this in my create_fundraiser.html.erb
<%= form_for(resource, :as => resource_name, :class => 'reg',:url => registration_path(resource_name)) do |f| %>
<% resource.build_campaign_fundraisers if resource.campaign_fundraisers.nil? %>
<%= f.fields_for :campaign_fundraisers do |u| %>
here I am able to create user and save data in both the user's and fundraiser's table on form submission but the problem i am facing here is that whenever I get an error (by devise validation) i am getting redirected to the sign-up page for normal user with error messages being displayed there.
I want a solution so that I get the error message on the same create fundraiser page without being redirected on the normal user's sign up page.
So please suggest any solution for my problem considering I am new to Rails and this is my first post here.
You can use config.scoped_views like in this documentation said, config.scoped_views
config/initializers/devise.rb
config.scoped_views = true
then, you can use views based on the role like users/sessions/new or students/sessions/new. If no view is found it will use the defaults at devise/sessions/new
This may be somewhat basic but cannot find a definitive answer anywhere. I have set up a contact form within my app and have put in a hidden field that when completed disables the submit button with some Jquery. My attempt at stopping automated spam..
Can I also add some validations in my model?
validates :ghost, :presence => false
Looking at the docs this is invalid? I want the form to fail if this field is filled in. Not sure how to go about this one
EDIT
So I have now read that I could possibly use
validates_exclusion_of :ghost, :on => :create
Though this is still failing as i dont think i am passing the correct arguments.
:presence => false means that you disable presence validator.
You need to write own absence validation (though in Rails 4.0 such validation exists, absence: true).
validate :ghost_is_absent
def ghost_is_absent
errors.add :ghost if ghost.present?
end
I am sorry to say , but why are you trying to do things so differently, doing it this way will make things more confusing for any future developer working on this piece of validation.
First thing:
1) You can do the reverse of it , mark it as spam when the field is empty and vice versa and then simply check with the validation validates_presence_of :ghost
2)or if you want to protect spam use capcha (recapcha gem for that)
3) or if you want it do it your way only , then just add a custom validation
Try creating a custom validation.
validate :check_for_spam
def check_for_spam
errors.add_to_base "ghost is present this is a spam" if ghost.present?
end
If you want to check if :ghost is blank:
validates :ghost, inclusion: {in: ['']}
If you want to check if :ghost is nil, you have to rely to a custom validator.
Rails 3.1.0 - Ruby 1.8.6
I'm trying to create a signup page. I came across this 'has_secure_password' with basically does all the magic for you to enter your password twice and checks if they are correct.
I'd like to use this technique to check on email address as well. If it's not possible, what is the easiest way for me to get the user to key in their email twice and check if they match?
Please help. I'm a rails noob.
Thanks
Rails has a validates_confirmation_of validation built in. If your model has
class User < ActiveRecord::Base
attr_accessor :email_confirmation
validates_confirmation_of :email
end
Then you just need an email confirmation text field in your form and rails will check that they match.
I am developing a rails application and am using a gem called devise to manage my users. I have created a new user called "Admin" but am unsure on how to change a user on the application from a "User" to an "Admin".
On the documentation it says:
"The code below can be used to grant admin status to the current user."
current_user.update_attribute :admin, true
But where would this snippet go?
Here is the documentation, The admin role creation info is near the bottom of the page.
https://github.com/plataformatec/devise/wiki/How-To%3A-Add-an-Admin-Role
You are very close to the solution! Nothing like reading through the documentation :-)
By going with Option 2 mentioned on the wikipage, users in your application will be classified
as 'regular' or 'admin', based on the admin attribute. The wikipage gives you the code for granting admin role to the current user, and leaves the decision of where to call this code up to you.
Fair enough, since how users become Admins is specific to each application, depending on how the users want it to be done.
One way to do it would be to have a 'Grant Current User Admin Rights' action in the GUI which would invoke the code. In that case, the code would go within a 'grant_current_user_admin_rights' method in the 'users_controller.rb' file. Of course, the views and the routes should be modified accordingly as well.
You could call that code from after_create callback on the user model, ensuring all users become Admins :-)
Another way to do it would be to set the admin flag for specific users either in the console or through database seeds.
Example from a seeds file on one of my projects:
admin_user = User.new( :email => USER_EMAIL, :password => PASSWORD_STRING, :name => USER_NAME )
admin_user.admin = true
admin_user.save!
Hope this helps.
4 years late but the true answer to OPs question, when he asked where to put:
current_user.update_attribute :admin, true
can be solved by going to the terminal/command prompt.
Type in
rails c
to access the rails terminal.
then go to your user, and since you're probably the first it'll be:
user = User.find(1)
user.update_attribute(:admin, true)
Assuming you followed all the previous steps in Option 2 of the documentation, this will set your user to have a true Admin attribute.
You can verify this by going
User.find(1)
and it should say "admin: true" at the end of the big block of text.