I have an app in which the back end is rails, and the front end is built within angular. I am in the process of editing my tokens controller that works on username and password errors. In this controller is a method that creates all of the magic.
The problem that I am having is that within the fail messages, I am trying to create a link within the second half of my fail message. Yet i've been having some problems with it.
def create
username = params[:username]
password = params[:password]
user = User.find_for_authentication(login: username)
fail!(message: 'Account not found. Please try again with new information or #{link_to(new_patient_registration_path)} click here to create a new account', status: :unauthorized) unless user
With what I have right now, I'm unable to have the message process. I wind up the entire error message being displayed, but the without the link.
Would anybody be able to take a quick gander at this for me to see what I am missing out on?
Much thanks in advance!
You need to use view_context
fail!(message: "Account not found. Please try again with new information or #{ view_context.link_to("Click here to create a new account", new_patient_registration_path)}".html_safe , status: :unauthorized) unless user
Related
I am trying a simple download from a server. This is using Rails 5.01, Ruby 2.24p230.
The view has a link to the controller to download data. It gets there just fine.
The controller method is just this:
def download
send_data("some sample text", filename:sample.txt)
flash[:success] = "it worked"
end
The result is that a file is named sample.txt containing the correct text is downloaded to the client. flash never happens. The view which linked to the controller is still on the screen, without any page refresh. A view called "download.html.erb" is never called.
My questions are:
Is there a simple way to cause some communication with the client following the send_data? It would be nice to tell the person on the client something after a successful download.
After the send_data, what should happen?
Thank you for taking the time to answer this.
To understand what is going on here you have to understand how the session and flashes work.
A flash message is stored in the session and carried on to the next request. When the request is finished flash messages from the previous request are removed from the "flash hash" which is stored in the session.
class TestController
# GET /foo
def foo
flash[:notice] = "Hello"
redirect_to '/bar'
end
# GET /bar
def bar
flash.now[:alert] = " world!"
end
end
So when the user requests /foo they are redirected to /bar and the flash hash will contain:
{
notice: "Hello",
alert: " world!"
}
So how is this relevant? When the client clicks the download button the flash message you set will be seen in the next request they perform. Which is not really that useful. Instead what you want to do is probably just use javascript and display a popup or some sort of message when the user clicks the download link.
When you send data to the client few will actually allow you to set any kind of redirect to be followed or will ignore any headers you send. This is because the huge number of potential annoying or malicious uses.
I used Sorcery to set up authentication in Rails and I'm trying to create a model where the user id for the user is linked as reference to the model for data entered, but I get an error:
Couldn't find User without an ID
it refers to the following code:
def create
#user = User.find(params[:user_id])
#certificate = #user.certificates.create(certificate_params)
redirect_to certificate_path(#certificate)
end
To answer basic questions I've asked myself, the user is logged in...
That's actually a very weird error if the above is really your code. You should have gotten something like Couldn't find Bar with 'id'= instead. The error above is usually only given if you provide no args to find at all, ie. User.find()
Anyway, the underlying problem here is that params[:user_id] doesn't contain the value you expect it to, and probably it contains no value at all. You haven't shown enough of your other code to determine why that is, BUT it doesn't matter...
Unless you want people to be able to just change the URL and add a certificate for some other user in your system then you shouldn't rely on the params[:user_id] for creating associated objects, you should use current_user - that's kind of the whole point of authentication.
So:
def create
#certificate = current_user.certificates.create(certificate_params)
redirect_to certificate_path(#certificate)
end
i am trying to set reset password for my application by following the tutorial at Railscast Reset password tutorial and everything went well until the last step after clicking the reset password link in the mail , so when i update the password it is telling "RuntimeError in PasswordResetsController#update" caused due to "Could not find a valid mapping for #
you can check out the error in more detail in the screen shot below . and also i had a migration error at the start of the process while i was trying to migrate the reset_password into the users table ,it said remember_token table already exists so skipping it ,do u think it had any thing to do with the error.
would really like to know what i am doing wrong , thank u...
[Unfortunately my reputation is not high enough to post images so i have posted links of the images in order , dont mind.]
Make the link to reset the password as
localhost:3000/password_resets?remember_token=abcdefghikfhgj
In password_resets_controller.rb:-
def edit
#user = User.find_by_remember_token(params[:remember_token])
end
def update
#user = User.find_by_remember_token(params[:remember_token])
#other codes
end
i've been thinking of what is the better way of creating a profile generation functionality, RESTwise.
The idea is that my menu always has a link to a user's profile, whether it's generated or not. However, a profile has to be generated by the user at the first time and it will then be shown to them.
To illustrate, there can be a menu item pointing to '/profiles/new' and change dynamically to 'profile/:id' once the user creates the profile for the first time. This seems natural, but i don't like that i have to get a #profile instance variable in my applications controller(since it will have to be checked everytime, to dynamically generate the link).
The other idea is to have a redirection happening, probably the best of the two. The user clicks on the /profiles/new path(or an alias) and gets redirected to 'profile/:id', if they do have a profile set already.
What do you think is the best way to do it and is there another alternative i've not thought of ?
A redirect doesn't quite make sense to me, RESTwise, as it introduces an ambiguous resource at /profile/new, but I don't think you necessarily need to get #profile every time. Assuming that user_signed_in? is a helper that would confirm that a user (current_user) is, in fact, signed in, you could create a link helper in your application helper directory that looks something like this:
def link_to_user_profile(html = {})
if user_signed_in?
#profile = current_user.profile
link = profile_path(#profile)
text = 'Your Profile'
else
link = new_profile_path
text = 'Create a Profile'
end
link_to text, link, html
end
I'm making up variables all over the place, but hopefully this helps!
You can also think of building a new profile after_create a user with default values. In this case the user will always have a default profile.
I have my login-partial in my application.html.erb-layout.
Now I want to validate it, if the user pushes the submit button and show error-messages for it, if e.g. the passwordfield is empty.
But how do I do that?
For getting the error messages, I must use
render :action => 'create'
But there I have to know, from which action I came from. Beside different pages, need different seperate instance variables (which arn't reproduced with render :action).
Second try with
redirect_to :back
loses the errors-array...!
How can I solve this problem?
Found nothing in the net =(
So not a direct answer to your question, but I would skip rolling your own login and checkout something like the restful authentication plugin
script/plugin install git://github.com/technoweenie/restful-authentication.git
script/generate authenticated user sessions
I'm using restful_authentication!
and I'm including the login-partial into my appliation.html.erb layout...?!
Let me see if I get you correctly:
You have a login box in your application layout.
When you press the submit button you are posting to the create action of your session controller
If that's the case, you can just render the new action of your session controller and render that page as well.
So you can login from everypage, but if anything goes wrong you end up in the session controller new action.
Would that be an option?
Solved it - used Ajax-Forms!