Override redirect after create

18 views
Skip to first unread message

Dennis Bulgatz

unread,
Apr 10, 2023, 4:54:39鈥疨M4/10/23
to ActiveScaffold : Ruby on Rails Gem
Hi Sergio,

How can I override redirect after create action?

Seems like there should be an easy way to specify in controller.

User desire: after a record gets created,聽 redirect to the list view, with a search param so they can easily see the record that was just created.

聽This does not work because the redirect is not to a page.

聽 def tdn_params
聽 聽 params.require(:record).permit(
聽 聽 聽 :title,
聽 聽 聽 :ordinal,
聽 聽 聽 :revision_description,
聽 聽 聽 :is_latest
聽 聽 )
聽 end # def tdn_from_params

聽 def create
聽 聽 tdn = Tdn.new(tdn_params)
聽 聽 tdn.discipline_id 聽= params[:record][:discipline]
聽 聽 tdn.clin_id = params[:record][:clin]
聽 聽 tdn.task_id = params[:record][:task]
聽 聽 tdn.organization_id = params[:record][:organization]
聽 聽 if tdn.save
聽 聽 聽 redirect_to tdns_path({'search[tdn_number][opt]': '=', 'search[tdn_number][from]': tdn.tdn_number})
聽 聽 聽 #redirect_to edit_tdn_path(@tdn)
聽 聽 else
聽 聽 聽 flash[:error] = "Could not Create TDN. 聽Latest Rev??"
聽 聽 聽 redirect_to tdns_path
聽 聽 end
聽 end
Thanks

Sergio Cambra

unread,
Apr 11, 2023, 6:37:41鈥疉M4/11/23
to ActiveScaffold : Ruby on Rails Gem, Dennis Bulgatz
Hi Dennis

If you are using the AS create action, it uses create_respond_to_html to
redirect to the listing.

def create_respond_to_html
if successful?
flash[:info] = as_(:created_model, :model =>
ERB::Util.h(@record.to_label))
if (action = active_scaffold_config.create.action_after_create)
redirect_to params_for(:action => action, :id => @record.to_param)
elsif params[:dont_close]
redirect_to params_for(:action => 'new')
else
return_to_main
end
elsif active_scaffold_config.actions.include?(:list) &&
active_scaffold_config.list.always_show_create
list
else
render(:action => 'create')
end
end


You can override it to do a different redirect:

def create_respond_to_html
if successful?
redirect_to ...
else
super
end
end

You can redirect to index action with the id of the created record with
tdns_path(id: @record), or tdns_path(id: tdn.id) if you are using the
overrided create action you posted here. It would use a url like /tdns?id=xxx.
Also, you could add a route to index action with id, so the url would be /
tdns/xxx/list

get '/:controller/:id/list', :action => :index

If you want to use search, I don't see what's wrong with your url, as long as
you have field_search enabled, with tdn_number column.

Regards

Dennis Bulgatz

unread,
Apr 11, 2023, 10:19:08鈥疉M4/11/23
to Sergio Cambra, ActiveScaffold : Ruby on Rails Gem

Hi Sergio,

The AS create actions was using create_respond_to_js so the redirect to an HTML page failed..

So I added to the controller:聽 conf.create.link.page = true

Then the redirect to an HTML page works.

Thanks!

Dennis

Sergio Cambra

unread,
Apr 11, 2023, 11:32:22鈥疉M4/11/23
to ActiveScaffold : Ruby on Rails Gem, Dennis Bulgatz
When conf.create.link.page is false (so link.inline is true), it doesn't
redirect, as submitting the form uses a js request which closes the form and
add a row with the new record created. In that case, when you use middle click
(or right click and open on new page) to open the form in a new tab, tab will
be send with HTML request instead of JS request, and the controller will
create_respond_to_html.

When you set link.page to true, then form is open with HTML request and
submitted with HTML request always.

Regards

Dennis Bulgatz

unread,
Apr 11, 2023, 11:41:20鈥疉M4/11/23
to Sergio Cambra, ActiveScaffold : Ruby on Rails Gem

Hi again..

One note..

You can redirect to index action with the id of the created record with
tdns_path(id: @record), or tdns_path(id: tdn.id) if you are using the
overrided create action you posted here. It would use a url like /tdns?id=xxx.
Also, you could add a route to index action with id, so the url would be /
tdns/xxx/list

get '/:controller/:id/list', :action => :index (list_tdn_path is already available so no need to add this route)

Redirect after create or revise to list_tdn_path is a super nice workflow for users, and is better than the search idea because

  1. Users can see the record just created and edit or create children (associated records)
  2. it gives users an easier way to get back to seeing all records via the application menu (my menu has tdsn_path as the link to tdns)

Thanks again for your help!

Dennis

Dennis Bulgatz

unread,
Apr 11, 2023, 11:48:33鈥疉M4/11/23
to Sergio Cambra, ActiveScaffold : Ruby on Rails Gem

Good to know.. but my users would probably struggle with anything other than a left click..聽 馃槉

Reply all
Reply to author
Forward
0 new messages