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!
Related
before anyone get's mad I would like to start by apologizing by how stupid it may seem to some but i'm really having a hard time finding out how.
I have been looking through tutorials online on how to program in ruby on rails but I can't seem to find out how to make a button that does it's action and won't redirect to another page. For example when I want my program to count the number of people present in my database I need to make another view where the action would be executed. I would like to make it do the action on the same view and not redirect to another page.
Every tutorial that I've gone through only redirects the action to another page.
Could anyone please help me? Thanks in advance.
Suppose that you want to have that button in an index page of some controller. If you add something like this (I'm using HAML here):
= link_to "Something"
Then clicking the link will refresh the page but calling the index method of your controller again before rendering. Now the only problem is to figure out if the index method has been called by clicking on the Something link or just by calling the page? You can do this by passing some parameters (e.g. ?mylink=true) when clicking the link.
HOWEVER I personally prefer to implement a custom controller method (via routes.rb) and then redirect to the desired page (e.g. index). Something like this:
routes.rb
resource :my_resource
collection do
get "test"
end
end
my_resource_controller.rb
...
def test
# Do stuff
redirect_to action: "index
end
my_resource/index.html.haml
= link_to "Something", text_my_resource_path
Now clicking on Something link gets you to test method and after doing your stuff you are redirected back to where you were (i.e. index). Read more about redirect_to HERE.
I'm sure that there is an answer to this out there, but I'm not entirely certain how to properly phrase this question, so my apologies if this is repetitious.
I am working on implementing a badge/achievement system for a site. The backend stuff is there, but I'm working on the front-end now, and I'm basically trying to figure out how to redirect to a sort of "Congratulations!" page when someone gets a new badge.
The congratulations page is going to be a modal, but for simplicities sake, if anyone has an idea of how to trigger an action like this only once when a new Badge is created, that would be a huge help. Right now, when a user performs a given action, say... adding money to their account, a user_badge is created (adds a Badge ID to an array).
Thanks in advance!
You can use before_filter for actions, where you want to check that new badge appears (probably you don't want to check it in auth actions, so you can't use global before_filter). Then you can use redirect_to in this before filter, if user has a new badge.
But I don't recommend to do so, since you'll break the users flow. It's better to show alert/modal on the next requested page. To achieve this you can define a new instance variable in your before_filter (like #show_modal) and include the partial with modal to your footer.
example:
#before filter
def check_for_new_badges
if current_user.has_new_badges?
#show_modal = true
current_user.set_badges_as_seen!
end
end
#included in layout
- if #show_modal
= render 'shared/congrats_modal'
Good day...
Still a beginner question. I mostly asking to save myself hours of trial and error... I had those already and there was no result.
What I wanna do... I have a controller that has an action, of course. I want to send this action two IDs, it should make some database entry of those. No problem, the action works well.
But I don't want a view to it, I want the user to click on a link and not even leave the site. Ideally there should be some Ajax (using jQuery a lot on the site) that changes the link to a 'Thanks' or anything.
So yes, my main question is: How can I activate an action of an other controller without leaving the site, from a link. Long intro, but I hope you can give me some hints.
Take a look at link_to_remote for the view side and then you can just have your controller function render nothing => true.
To keep it un-obtrusive & return something from the controller action... I think you probably want something like:
def my_action
# your code... e.g. #model = Model.find(params[:id])
render :json => { :whatever => #model.whatever }
end
In your javascript, do what thou wilt with the returned information & prevent the default action of your hyperlink by returning false or using jQuery's prevent default functionality for the click event.
Look up link_to_remote method in rails. That should make an ajax call out to a controller function and not leave the page.
I have an action that doesn't require a form. So it really only needs the one 'edit' method instead of the RESTful 'edit' --> 'update'. Is there any reason not to do this or a better way?
def edit
#Do a POST(PUT)
end
The harm is that a user could easily navigate to that url and perform a potentially destructive action.
/noform/edit #URL typed by user => Action Performed
/noform/update #URL typed by user => Error is thrown, No Action Performed
A normal browsing experience generates GET requests to the server. The assumption is, any page you can easily navigate to (or type into your address bar) will not perform any data changing functions.
A POST request, generated via a form submission or a AJAX request expects the result that data is changed on the server.
Similarly the two rails "faked" versions of PUT and DELETE also are not actions you could simply navigate to using a browser.
The solution
The solution is to have only the update action and where you originally would have linked to edit use something like the following:
button_to "Add new tracker", noform_path, :method => :put
If there is any type of error, you may still need an edit path to show the user so they can correct something. But from what you have described, a single update action should do the trick.
Gets should always be idempotent -- that is they should not perform any action that will alter the state of the application, database, etc.
Just as an aside -- in true RESTful form an edit would be performed by an HTTP Update action, but Rails simulates this with a post and a hidden value on the form, since browsers don't have HTTP Updates.
It's still not clear to me why you need an update without an input field. Perhaps a little more detail would be helpful.
I want links that when hovered over you see the link to look something like this:
http://www.website.com/redirct_to=linkID2
(maybe not exactly like that but try to get the idea)
I have a form area in my blog that I can input a website url but I want to make it redirect_to an external website when it shows the post.
redirect_to "https://website.com"
should do it as long as protocol is included. For added flexibility, you can parse it with URI to ensure that all fields are correct. You might want to URI.encode/URI.decode
redirect_to "https://website.com", allow_other_host: true
Create a new model, for example called Link, to store the URLs you want to redirect to.
Then generate a new controller (you can use scaffold to generate controller and model at the same time) and change the show action in order to fetch the record with given ID and redirect_to #link.url.
If you don't want to use the #show action for this step, create a new action do handle the redirect (for example goto, redirect...).
redirect_to isn't available in views or is not a helper method, be sure to use it in your controllersbut if you really want to redirect from view, use the javascript solution
window.location.href=<%= post.link_url %>