ROR 3 Michael Hartl tutorial - assign Admin role invalidates the user - ruby-on-rails-3

I am experiencing an issue as I am following Michael Hartl's tutorial and currently at the end of Chapter 10.
When I use toggle in the rails console to flip the admin property of a user from false to true (I'm trying to create an Admin user), I am successful in doing so; however that user becomes invalid - when I try to log in to the application with this user's credential I am getting an error that the credentials are incorrect (even though they are)
Just from checking the logs and the looking at the terminal, it seems the authenticity token is either missing or incorrect - any ideas what I am doing wrong here?
I tried following the tutorial as close as possible, I doubt I missed something, but its obviously possible.

I have had a similar problem as well. It appears (and perhaps someone else can explain why) that when you run user.toggle!(:admin), it resets the password to ''. To fix it I just ran user.update_attributes(:password => "yourpassword", :password_confirmation => 'yourpassword in the console.

Yes. I too stumbled here. Part of what made this puzzling to me as a newcomer to rails was that the error was in my understanding of how the testing sqlite db is handled, not the code that was written. I thought I was guaranteed a fresh start (new copy of project_root/db/test.sqlite3) each time testing began, but this is not always the case. More here:
Rails 3 Tutorial Chapter 11 "Validation failed: Email has already been taken" error
and here:
RailsTutorial - chapter 8.4.3 - Test database not clearing after adding user in integration test
and here:
http://rubygems.org/gems/database_cleaner (click on 'Documentation')
Most of these discussions surrounds the testing db, but the exchange also clued me in to the existence of development and production databases as well. Look at your directory listing for project_root/db for your ah-ha! moment.
hth,
Perry

I got it to work, but I still don't understand how it comes about and whether its by design or not. But, I did manage to register an Admin user, my mistake was that I didn't set the password field in the console before calling user.toggle!(:admin).
i.e.
u = User.find_by_id(1)
u.password = "password"
u.toggle!(:admin)
and it worked.

Related

"User is unauthorized" error with sample web viewer app

I'm following the instructions here: https://www.itwinjs.org/learning/tutorials/develop-web-viewer/ to create a sample web viewer app. The doc says to update variable IMJS_CONTEXT_ID which is not in the .env file and doesn't appear with "Show IDs" for my iModel (I have created a github issue for the doc already). I've tried updating the variable IMJS_ITWIN_ID instead with the "iTwin ID" from my iModel. When I start the app I get an error page "An Error Has Occurred: The user is unauthorized. Please provide valid authentication credentials". I have checked the variables IMJS_AUTH_CLIENT_CLIENT_ID, IMJS_AUTH_CLIENT_REDIRECT_URI, IMJS_AUTH_CLIENT_SCOPES, IMJS_ITWIN_ID, IMJS_IMODEL_ID and all appear to be correct. Is there something else I need to configure?
Sorry, the tutorial you're referring to is a bit outdated now. Have you taken a look at https://developer.bentley.com/tutorials/web-application-quick-start/?
In the new #itwin/web-viewer-react templates we renamed IMJS_CONTEXT_ID to IMJS_ITWIN_ID. We also made some changes to the required scopes, you'll need namely imodelaccess:read, imodels:read, and realitydata:read. Please make sure your auth client has the above scopes and you have access to the model. If you still have trouble, feel free to continue the discussion over at https://github.com/iTwin/itwinjs-core/discussions

Rails 5 routing: why is "redirect_to #model" different than "redirect_to model_path(#model.id)"?

I am working on a Rails 5 app and have a model called #offer. In the Offers controller I have an action where I want to redirect to a specific offer I got from the database (Offer Id = 14 in the examples below.)
Working in the development environment, if I use redirect_to offer_path(#offer.id) in the controller, the browser correctly displays the offer in the https://dev.example.com/offers/14 URL. Notice the dev part in the URL. So far so good.
However, if I use redirect_to #offer in the controller, the browser tries to open the https://example.com/offers/14 URL (that's the production URL) and the page shows an error (We're sorry, but something went wrong.
If you are the application owner check the logs for more information.)
I would like to use redirect_to #offer, but first, I think I need to understand why one redirect method behaves differently than the other. Thanks for any insight.
This question is old but still I am answering as it might help other rails users in the future.
In Offers controller redirect_to #offer or redirect_to offers_path(#offer) would resolve to the same path /offers/:id be it production or development.
I think, in the production database offer with id 14 does not exist and also in the controller, if op is using find method without rescuing exception then, the show action might be erroring while trying to fetch the Offer with id 14 from the production database but the find method my have returned exception which if not rescued the rails might show a default error response.
We don't have controller code posted by the op but to me, this seems to be the most logical answer.

Devise 3.0.0rc custom attributes and strong parameters in Rails 4

I used this solution based on devise_parameter_sanitizer to set some custom attributes to my Devise (User) model and make them available at sign_up and edit forms.
Everything works fine but from time to time (and it happens pretty often) specifically when I sign in, sign out or edit user (so I assume the problem is related to sessions), I get the following error:
Is there any logic that I can add to my code above to present this issue?
I know this was a little while ago but wanted to put an answer here because this actually took me a bit to solve and other people might have a similar issue.
Because you used an initializer to load the User:ParameterSanitizer when you change the application controller or anything that would cause it to reload (because you are in debug mode) the initialization is lost.
This is easy to test to confirm this is what was happening to you:
First: start the server fresh and go to your login page: should work.
Next: modify application_controller.rb (just put a space somewhere and hit save)
Next: visit your login page again (not working anymore) :(

Kohana 3 auth username as number

I want to use numbers as username in Kohana Auth. For example, username 100001?
While adding new user Kohana returns me error: ORM_Validation_Exception [ 0 ]: Failed to validate array
Is is possible to user numbers as username in Kohana?
EDIT: This answer looks simpler and better than mine, but try to understand it at all.
You need to extend User Model, I'll help you using auth with the ORM driver.
Steps to extend User Model:
If you didn't yet, configure Auth module to use orm and create a database table with the fields you want. Here is a good example of how to doing it (It's an old tutorial using ko3.1 but you can still learn from it). PS.: you can have any columns at the 'users' table and you don't need to have the 'username' column if you do not want.
Open and read carefully this file: MODULES/orm/classes/model/auth/user.php (It's self documented and I hope you understand it. If not, stop reading this answer here and read the kohana docs. Some shortcuts: Auth - Kohana User Guide, Auth (orm) methods, addons:auth
Copy the file (don't edit the original) to APPPATH/classes/model/auth/user.php and edit it how you want. Some functions that you may like to edit are: rules, filters and unique_key (<- useful). Be creative, you also can add custom functions.
Test and change whatever else needed.
You can change the login method to works as you like. You can set login by e-mail, make a custom validation method or parse values before saving in the database (see public function filters()). This is helpful for whatever you try to do with auth module using ORM... But... if you really don't want to use ORM, you can build your own driver, learn how.
I made this some time ago in kohana 3.2 but I think you won't get problems with 3.3. If you still have questions, this question on kohana forum may help.

NameError in Devise/registrationsController#create

Im trying to use the gem Devise for a admin authentication. Ive used it before when I only wanted a user login but now I would like to have just a admin login. I followed the railscast for it and instead of naming the model User I gave it the name Admin. Everything worked fine until I tried to login then I got this error message:
uninitialized constant Devise::Encryptors::Bcrypt
I cant seem to find the problem. I have compared the code in the project with the other one I did and there is no difference. Is the problem due to the fact that I´m using Admin the wrong way?! Should I not use it as you would with a user?! Thankful for all help.
Regards
That encryption method appears to have been removed from Devise.
See: https://github.com/plataformatec/devise/issues/issue/527