Render custom edit partial in a 'Custom Page' in ActiveAdmin - ruby-on-rails

I'm using ActiveAdmin for a project. I have and I have a partial with a custom form. I want to be able to render this custom form when the user clicks in a button so this is what I did:
ActiveAdmin.register_page "Messages" do
menu priority: 5
welcome_message = Template.first
page_action :update, method: :put do
#update code
end
page_action :edit, :method => :get do
render partial:'custom_form', locals: { settings: welcome_message }
end
action_item do
link_to "Edit", admin_welcome_messages_edit_path, :method => :get
end
content do
render text: "HI"
end
end
This works, but the problem is that my form gets rendered without the layout and styles of ActiveAdmin, it just shows my custom_form as a clean html.
If I render my custom_form in the content do ... end it does work, but I need that to show something different.
Any help?? I don't know what else to try, I have reached the first 3 pages of google without success!!

kind of an old question but I was having the same problem and this solved it for me.
render partial: "name_of_your_partial", layout: "active_admin"
wasn't working for me either so I just changed my partial into a template.
render: "name_of_your_template", layout: "active_admin"
and now the activeadmin layout is being displayed.

I did not find an answer for this but I found a nice workaround that looks pretty good.
In the action_itemI have a link to admin_welcome_messages_path that is the main view and if I'm editing add a param there to show the form instead of the body.
Hope it helps somebody!
ActiveAdmin.register_page "Messages" do
menu priority: 5
welcome_message = Template.first
page_action :update, method: :put do
#update code
end
action_item do
link_to "Edit", admin_welcome_messages_path(edit: true), :method => :get
end
content do
if params["edit"] == "true"
render partial:'form', locals: { settings: welcome_message }
else
render partial:'body'
end
end
end

Add layout option to the render call.
render partial:'custom_form', layout: 'active_admin', locals: { settings: welcome_message }

Determine which layout to use.
If we're rendering a standard Active Admin action, we want layout(false)
because these actions are subclasses of the Base page (which implements
all the required layout code)
If we're rendering a custom action, we'll use the active_admin layout so
that users can render any template inside Active Admin.
https://github.com/activeadmin/activeadmin/commit/ce0927661c5991cc857306363c402ef9e283cc42
# this would automatically render the custom_edit.html.erb view
page_action :custom_edit do
end
action_item do
link_to "Custom Edit", admin_welcome_messages_custom_edit_path
end

I realise that this an old question but the following worked for me for my Ruby on Rails application:
Inside your your_custom_page.rb file do the following
content do
render 'partial_name'
end
and place the _partial_name.erb in view/admin/your_custom_page/
Your partial should now render on that active admin page.

Related

Multiple buttons pointing to single Controller Action

I am making an app where user can print a doc in either PDF or JPG format, using wicked-pdf and imgkit respectively.
I have two buttons, one for PDF and other JPG. Is it possible to have these buttons point to same action in controller which here is 'create'.
my buttons are as-
<%= button_to "Print Bill[PDF]", :action => "create" %>
<%= button_to "Print Bill[JPG]", :action => "new" %>
can i make both the actions create?
if yes, how so? How to catch which button is hit and render the respective view.
First of all, it is generally recommended to use route helpers, rather than specify controllers and actions. So your code could be
<%= button_to "Print Bill[PDF]", bill_print_path(#bill, format: :pdf) %>
<%= button_to "Print Bill[JPG]", bill_print_path(#bill, format: :jpeg) %>
and in your controller
def print
# insert here code to find your bill and load it from DB
respond_to |format| do
format.jpeg do
# code to produce the jpeg version of the bill
end
format.pdf do
# code to produce the pdf version of the bill
end
end
end
As a final step I would change button_to to link_to and style your link as a button, but that is more of a personal preference.

Rails - Add action to controller created by scaffold

I'm trying to add an action called rollback to controller.
As I've seen, the only things I should do is writting the new action:
def rollback
puts "ROLLBACK!"
respond_to do |format|
format.html # index.html.erb
format.json { render json: #components }
end
Modify the routes.rb file:
resources :components do
collection do
post :rollback, :as => 'rollback'
end
end
And calling the action from some view:
<%= link_to 'Rollback', rollback_components_path %>
But I get the following error:
Couldn't find Component with id=rollback
app/controllers/components_controller.rb:18:in `show'
That's because instead of going to rollback action, the controller thinks that we are trying to 'show' to component with id 'rollback'.
Something that it seems weird for me is that calling 'new' action rails uses new_component_path (without s, in singular), but if I write rollback_component_path it throws me an error and I cant see the view.
In your routes you require a POST, just clicking a link is by default a GET, so either write
resources :components do
collection do
get :rollback
end
end
and then the link_to will work as expected.
I am assuming the rollback operation is not idempotent, so a POST is semantically better in that case.
If you write your link as follows, then rails will create an inline form for you:
link_to 'Rollback', rollback_components_path, :method => 'post'
Hope this helps.
This will work
routes.rb
resources :components
match "components/rollback" => "components#rollback", :as => :rollback
In views
<%=link_to 'Rollback', rollback_path%>

Reload page or div using RJS

I have a 'like'-counter in a rails project. Whenever one clicks this 'like'-link I want to add one to the like_counter in the database. The logic works well and the database is updated, but I cannot figure out how to set up the Ajax Request correctly so it refreshes the page or the div when completed.
In my view this looks as follows:
<%= link_to_remote 'like', :url => {
:controller => 'projects',
:action => 'like_it',
:id=> #project.id }%>
The controller:
def like_it
#project = Project.find(params[:id])
#project.update_like
render :update do |page|
page.reload
end
end
the update_like method is in the model and just adds one to the counter and saves the project (this part works).
As for the page.reload I Firefox throws an RJS-Error: ReferenceError: Reload is not defined. And the page is not reloaded
What do I do wrong? Is there a more distinguished way to just reload the div containing the counter? Any help is much appreciated!
Try:
render :update do |page|
page << "window.location.reload()"
end

Ruby on Rails form_remote_tag missing template

I'm using form_remote_tag(:url => {:controller => "home", :action => "search"}, :update => "mydiv"). When I click submit on the form "mydiv" is populated with the error "Template is missing. Missing template home/search.erb in view path app/views". I've tried multiple render options in def search, but they all result in the same error.
It looks like the search method is trying to use it's default render even though I'm specifying what I want.
I've tried:
render 'index'
render :text => 'Return this from my method!'
Is my url incorrect? Is it not submitting back to my home controller's search method?
Try
render :action => 'index'
this will use "index.rhtml" or "index.html.erb".
I will try to explain why it said search.erb is not found, lets take create action for a some model, if there is some error in my create action they it will throw missing template create.html.erb file, since you have some error in your create action rails will try to render the create.html.erb in the page. Hope I explained it clearly.
In an ajax action you can't use redirect_to or render options directly.
try using this in your search action
render :update do |page|
page.replace_html "ur_div_id","partial"
end
The form_remote_tag needs prototype to function. Make sure you are including the :defaults for your javascript libraries namely prototype.
<%= javascript_include_tag :defaults %>

Rails conditional ('if') statements based on controller action

There might be a better way to do this, but I'm trying to make an if statement in rails, based on the current action, in a controller (this will be used in a view).
For example, if its the edit page, or the show page, etc. I'd like a different style for something - is there an if statement that can specify this?
(I need an if statement, because its used in a partial, on multiple pages).
Thanks!
Elliot
The params hash that is available in the controller contains :controller and :action keys, which specify the controller and action names of the request.
Therefore you could say
if params[:action] == "foo"
# Show stuff for action 'foo'
elsif params[:action] == "bar"
# Show stuff for action 'bar'
elsif ...
# etc.
end
It's not good practice IMO to have partials asking what the current controller and action names are. Think "tell, don't ask" (http://www.pragprog.com/articles/tell-dont-ask). That is, rather than having the partial ask it's caller about its state, tell the partial what you want it to do.
One way to do this is by passing variables to the partial through the locals option:
<%= render :partial => "/common/toolbar", :locals => {:edit => true} %>
Then in the partial:
<% if defined?(edit) && edit %>
... stuff appropriate to edit mode
<% end %>
You can do it this way:
class ApplicationController < ActionController::Base
layout :set_layout
def set_layout
case params[:action]
when "foo"
"foo_layout"
when "bar"
"bar_layout"
...
else
"default_layout"
end
end
...
end
hope it helps =)
You can use layouts for partials too:
<%= render :partial => 'some_partial', :layout => 'wrap_with_stuff' %>
If you want to work out what layout to use dynamically I'd put that in a helper. So you'd end up with
# In your view
<%= render :partial => 'some_partial', :layout => layout_for_my_partial %>
# In your helper
def layout_for_my_partial
params[:action] == 'show' ? 'show_wrapper' : 'everything_else_wrapper'
end
This will only work in some circumstances, but might be what you're trying to do.
See more here.
http://ryandaigle.com/articles/2007/8/3/what-s-new-in-edge-rails-partials-get-layouts

Resources