Form redirects after create in Rails - ruby-on-rails

I am following the tutorial on railstutorial.org but with modifications. There is a form where I want to create users. But when i create users, it redirects me to the user page. How can I edit it? I want it to remain on the same page with a flash notification stating that the person has already been created. Below is the create function I currently have.
def create
#user = User.new(params[:user])
if #user.save
flash.now[:success] = "User Added!"
render 'new'
else
render 'new'
end
end

Try:
redirect_to new_user_path, notice: 'New User Added'
Or check your routes, and see which page you want to redirect, in console type:
rake routes

Hmm..try redirect_to users_path, notice: 'User Added'

Related

I would like to render the same page if new user is not save

I would like to render on the same page if email is blank. However, when I leave in blank instead of staying on users/new. It goes to /users. It is still render to _from for the URL is different.
def new
respond_with(user)
end
def create
if user.save
user.send_invitation
redirect_to root_url, notice: "Signed up!"
else
render "/users/new"
end
end
def destroy
#user = User.find(params[:id])
if #user.destroy
redirect_to root_url, notice: "User was deleted successfully!"
end
end
def edit
respond_with(user)
end
def update
params[:user].delete(:password) if params[:user][:password].blank?
params[:user].delete(:password_confirmation) if params[:user][:password_confirmation].blank?
if user.save
redirect_to users_path, :notice => "Saved your updates!"
else
render :edit
end
end
render "/users/new"
Just renders template users/new, at create action. So the URL stays /users.
You can try to
def create
if user.save
user.send_invitation
redirect_to root_url, notice: "Signed up!"
else
redirect_to new_user_path
end
end
But then you will have no access to submitted params, so the form will be clear.
The thing in here I think it's a routes problem, as you want to persist the URL.
What Rails does in this cases is it goes to the create action once the form was submitted and if for some reason the record is not being save(in this case) you are rendering the new action but with the object which has errors and data.
To clean up a little bit you could do something like:
def create
if user.save
user.send_invitation
redirect_to root_url, notice: "Signed up!"
else
render :new
end
end
In my opinion, don't try to bend Rails this way, you'll get a lot of problems with it and also, does it really matters to persist the URL?
Redirection is not an option, because you'll loose all the data from the request, as it would become a new request due to the redirect.
Hope this helped!

Rails Actions for Form Posting

I am using Rails 4 and moved from CakePHP.
I have a User Model and to create a new record it uses two Actions - New and Create.
Now when i want to over ride the default for my app. i would like the users to go to Signup action to create a new user. Now when i have a Server side validation and it fails i am posting the form to lets say "create" action the user is shown in the url
'app.com/user/create' instead of 'app.com/user/signup'
Is there any way to keep the user in the same action instead of have multiple action just to display form and save the form?
# GET /users/new
def new
#user = User.new
end
# POST /users
def create
#user = User.new(user_params)
if #user.save
redirect_to #user, notice: 'User was successfully created.'
else
render :new
end
end
You should simply add a redirect in your create action when the user creation fails.
redirect_to :back, #user
I would not recommend using :back all the time but this is going to be helpful for now as by understanding the scenario you have mentioned.
By default, action new just initialize model with-or-without params. Action create save model to database. app.com/user/create is not RESTful and "Rails Way".
users_path #=> app.com/users
new_user_path #=> app.com/users/new
user_path(:id) #=> app.com/user/:id
edit_user_path(:id) #=> app.com/user/:id/edit
# and so on
In controllers you can define redirections for every action. For example:
def create
if #user.save
redirect_to user_path(#user)
else
redirect_to :back # return to previous page
end
end
More information about routing here: http://guides.rubyonrails.org/routing.html
I would stick with rails conventions but you should be able to do this if you really wanted
Routes.rb
get 'signup', to: 'users#signup'
post 'signup', to: 'users#signup'
Controller
class UsersController < ApplicationController
def signup
if request.get?
#user = User.new
elsif request.post?
#user = User.new(user_params)
if #user.save
redirect_to root_url, notice: 'Signed In'
else
#should just render signup as it's signup action
end
end
end
end

How to route to a belongs_to object

I have a controller method that creates a user. I want it to route to user preferences when the user creates himself. How do I write the path? Here's the method:
def create
#user = User.new(params[:user])
if #user.save
redirect_to profiles_path, :notice => "Signed up!"
else
render "new"
end
end
I'd assume that when the users creates himself you want it to redirect to an edit option of this users preferences. Now, if the users is not created, there cant be any preferences to it. So im thinking that, once the users saves, you should create a record in the 'preferences' model, with the user_id so you know where to redirect to. (This is assuming that the #preferences model has a user_id field).
Something like:
if #user.save
#preferences.new
#preferences.user_id = #user.id
#preferences.save
redirect_to edit_preferences_path(:user_id=> #user.id), :notice => "Signed up!"
else
whatever
end
Hope it helps.

Rails: attributes not being saved even though I called #user.save

I'm running this function, and I KNOW that it gets called because the redirect_to is working. But for some reason, #user isn't! If it helps, #user is devise based.
def make_feed_preference
#user = current_user
##user.feed_preference = params[:preference]
#user.feed_preference = "time"
#user.name = "Shoo Nabarrr"
#user.karma = 666
#user.save
redirect_to '/posts'
end
I fixed it myself. I had to create a new class attached to users in order to get it to work. Lol.
Do you have any validations on this user? They are probably blocking this save. The redirect_to will be called regardless of whether or not the save passes or fails.
I would recommend doing it like this instead:
if #user.save
redirect_to '/posts'
else
render :feed_preference
end
Where :feed_preference is the form where users enter their feed preferences.
There are cases where I want to be sure to update a flag or other field on a record even if the record has validation problems. (However, I would never do that with unvalidated user input.) You can do that thusly:
def make_feed_preference
case params[:preference]
when 'time', 'trending_value', 'followers'
current_user.update_attribute 'feed_preference', params[:preference]
flash[:notice] = 'Your feed preference has been updated.'
else
flash[:notice] = 'Unknown feed preference.'
end
redirect_to '/posts'
end

Rails create action is redirecting to index when it should be rendering the new action

If I submit a new user form with errors, it redirects to the index page and then renders the new page on top of it. In the controller I specify that it should just render the new action so that the user can see/fix their errors and resubmit. Is there something obvious that I am missing?
Here's the create action in my controller code:
def create
#user = User.new(params[:user])
#user.role = "owner"
if #user.save
flash[:notice] = "Registration successful!"
else
flash.now[:notice] = "You have errors!"
render :new
end
end
I think you want to say
redirect_to :action => 'new'

Resources