Rails update_attributes always redirects - ruby-on-rails

I'm writing a edit page for a record in my database, I want to redirect if the update was successful and render the edit page again for any errors. Here is the code:
def edit
#list = List.find(params[:id])
if #list.update_attributes(params[:list])
redirect_to(root_path)
else
render('edit')
end
end
The redirect fires as soon as I launch the edit page, before any changes are made or the submit button is clicked.
Any ideas greatly appreciated.

Your edit action should look like this:
def edit
#list = List.find(params[:id])
end
It renders the edit view. The form should point (and probably is) to the update action that should look like so:
def update
#list = List.find(params[:id])
if #list.update_attributes(params[:list])
redirect_to(root_path)
else
render :edit
end
end

Related

redirect_to the previous page but changing the language

What am I trying to do? To redirect the user in the previous page with the website translated in new language selected
My previous script was (language_controller):
class LanguagesController < ApplicationController
def edit
#language = Language.find_by(locale: I18n.locale)
render layout: false
end
def update
#language = Language.find(update_params[:id])
if user_signed_in?
setting = current_user.setting || current_user.build_setting
setting.language = #language.id
setting.save
end
redirect_to root_path(locale: #language.locale)
end
private
def update_params
params.require(:language).permit(:id)
end
end
As you can see, there is redirect_to root_path that redirect the user in the home page. Even if he is changing the language in an article-page
So I edit it and I added in edit session[:return_to] ||= request.referer
and in my update action I replaced redirect_to root_path(locale: #language.locale)
with redirect_to session.delete(:return_to)
What is happening? If the user is logged, it works very good and it redirects the user in the previous page.
If the user is not logged he is redirected in previous page BUT now the language is the same. Probably because I removed (locale: #language.locale), right?
I also try to edit redirect_to in this way: redirect_to session.delete(:return_to, locale: #language.locale)
but it said that I can not add two elements there.
How to solve?

rails 5 redirect_back not working

i have a admin edit page and form in here. when i submit the form, it is going to the update action and updates the admin, there is no problem. After the update, i want redirect to the index page in the same controller. But it gets redirected to the edit form again. I tried a couple of things but in vain and gets redirected to edit page. i tried too much things but it is always going to edit page.
Controller (admins_controller.rb)
class Admin::AdminsController < ApplicationController
def index
#admins = Admin.all
end
def edit
#admin = Admin.find(params[:id])
end
def update
#admin = Admin.find(params[:id])
if #admin.update(admin_params)
redirect_back fallback_location: admin_admins_path
else
render 'edit'
end
end
private
def admin_params
params.require(:admin).permit(:id, :username, :password)
end
end
I'm trying admin_admins_path it does not work.
redirect_back like its name, it redirects to the route which it submitted the request, in this case is the edit page.
you should use redirect_to
redirect_to admin_index_path

Using Administrate gem, how to render edit page via custom update action?

I'm trying to have my custom update action for my user controller render the edit page if they fail to validate, like so:
def update
user = User.find(params[:id])
user.update(user_params)
# zero because checkbox
user_params[:banned] == "0" ? user.remove_role(:banned) : user.add_role(:banned)
if user.banned_note
if user.banned_note.update(content: params[:user][:reason_for_ban])
redirect_to "/admin/users/#{params[:id]}"
else
render "edit.html.erb" # this line
end
end
end
def find_resource(param)
User.find!(id: param)
end
I want to render the edit page with a flash message listing the errors, but what I have doesn't work. I'm not sure how to route to the correct action, since there's no actual view or edit action (which are auto generated by the Administrate gem).
Pandy, I have not used this gem but looking the source code and this comment of this issue in GitHub, Have you try the following?
def update
user = User.find(params[:id])
user.update(user_params)
# zero because checkbox
user_params[:banned] == "0" ? user.remove_role(:banned) : user.add_role(:banned)
if user.banned_note
if user.banned_note.update(content: params[:user][:reason_for_ban])
redirect_to "/admin/users/#{params[:id]}"
else
#This two lines
flash.now[:notice] = "Something"
render :new, locals: {
page: Administrate::Page::Form.new(dashboard, resource),
}
end
end
end
def find_resource(param)
User.find!(id: param)
end

Keep model with errors when redirecting to edit

When there was an error on updating my model, I was rendering :edit, but this was stripping the /edit from my url because #update is the same as #show with a different request method. To solve this I tried following the advice given here, but this caused me to get an ActionDispatch::Cookies::CookieOverflow error when I try to submit an invalid form. How should I correctly re render the edit page, while keeping both the /edit url and the flash messages? Is it possible to check for validity and show the errors without making a call to update?
Original code:
def edit
end
def update
respond_to do |format|
format.html do
if #model.update(model_params)
redirect_to home_base_url_or_default(model_url(#model)), notice: "Successfully updated."
else
render :edit
end
end
end
end
Failing code:
def edit
if flash[:model]
#model = flash[:model]
end
end
def update
respond_to do |format|
format.html do
if #model.update(model_params)
redirect_to home_base_url_or_default(model_url(#model)), notice: "Successfully updated."
else
flash[:model] = #model
redirect_to :action => :edit
end
end
end
end
Rather than doing a redirect, in this case the problem was solved by doing a render, then controlling the view by setting an instance var in the controller saying if it is the edit page or not. Also by using the update class in the CSS. However, this still has the the url for the show page, but at least the layout is correct.
One way to do it would be to allow the edit action to accept the POST method as well. Use request.method to check whether it is a POST or GET, then perform your render or redirect accordingly.

Can't overwrite the active admin default redirect path

I am using active admin in my application. In my controller, I have an action update with redirect_to function. But while updating, it threw me an error.
Render and/or redirect were called multiple times in this action. Please note that you may only call render or redirect, and at most once per action. Also note that neither redirect nor render terminates execution of the action, so if you want to exit an action after redirecting, you need to do something like redirect_to(...) and return.
def update
#user = User.find(params[:id])
#user.update
mailers.notify(#user).deliver
redirect_to user_path(#user)
end
I tried
only redirect_to
redirect_to() and return
but nothing works.
before_filter :only =>[:create,:update] do
if self.action_name.to_sym == :create
#user = User.new(params[:user])
else
#user = User.find(params[:id])
end
I fix it by adding this to my update action .This works for me fine.
def update
update!do |format|
format.html { redirect_to_user_path(#user)}
end
What is your purpose? Why you use mailers.notify(#user).deliver inside the update action?
Move the notification to after_update filter in your User model, smth like this:
after_update :send_notifications
def send_notifications
Mailer.notify(self).deliver
end
Change the update method to smth like this:
def update
super do |format|
redirect_to user_path(#user), notice: "Your notice message" and return if resource.valid?
end
end

Resources