Rails render regular erb partial to active admin layout (arb) - ruby-on-rails

I'm trying to add a view that LOOKS like active admin, but doesn't need to take advantage of active admin's automatic page creations (in fact I can't because I'm not using a model for these views). In my controller, I've added
render "reports/index", layout: "active_admin"
I've seen this done in several other forums. However, I'm getting this error: undefined local variable or method 'view_factory' for :Arbre::Context
which I'm assuming is because I'm not using a full active admin page object or something. Any ideas on how to make this work?

If you don't have a model then use a custom page.

Related

Rails 4 way of editing multple records individually at once

So I'm trying to use a table to store global settings. I want the /SystemSettings/edit.html.erb to be the only view and controller, which almost works. Unfortunately, I need to edit all records at the same time. I will be using an initializer as I don't want user to create or remove settings, only edit. I'm trying to go by this railscast (#198) but bypass the checkbox way as I also have no index/show views. It seems to be working fine, but when I go to update the form I get undefined method 'keys' for nil:NilClass. You can see all the code on the sys_settings branch of my github repo.
Can it that be that simple that you use :system_settings symbol to access parameters in controller and create form for "settings[]" (and not "system_settings[]" in view?
undefined method 'keys' for nil:NilClass
basically says you don't have any values under params[:system_settings]. Review console messages for the rails server, it prints all incoming parameters for each request. Study what comes for the update request, how parameters are named there.

Rails Create and Edit on the same view

I am trying to accomplish Create and Edit on the same view (and same controller) on Rails 3.2 and Ruby 1.9.2. I have a partial and I am using form_for and the standard form helpers.
I submit using Ajax, so my page (form) is not refreshed or redirected. When the user tries to edit the form, naturally, Rails will create an entry instead of updating it. I was thinking of modifying the Controller's create method to detect existing entries, but I am not sure if this is the correct approach. Thanks.
You can pretty easily include the same form in both create and edit pages by storing it in a partial (typically _form.html.erb) and rendering the form in both create and edit.
This is what rails generate scaffold MODEL FIELD:TYPE FIELD1:TYPE1 will give you, as well.

creating a simple html page in ruby

Following the ruby tutorial I am trying to create a simple html page.
I will need a controller, i think for some presentation manipoulation but not DB.
I thought that I would create a model, controller and view but then I see that
rails generate model mainMenu freeData1:string freeData2:string
creates the db script.
In order to achieve a simple managed html page that would not require db, what should I create ?
controler only? what is the best practice?
what methods should I put in it for it to be displayed?
thanks.
Yes, you'll just want a controller for the pages which will also create a views folder for your new controller that you can put HTML/ERB/whatever files in.
Since youre not working with the db, you can probably skip the need of a model.

How to display results globally, rails3

We wanted to include a global announcements module in our rails3 application that's displayed on all pages when a user's logged in to the system.
I've created an announcement controller and, to test, put the following in to display the last active message:
#latest_announcement = Announcement.where(:active => true).order("created_at").last
What I was trying to do was display the result of #latest_announcement on every page (in my application layout) and I tried to put this in my application controller.
However, this didn't really work out for me.
Is there a simple way to do this without having to put the above in every controller?
if you have shared notification across the system and need to put it on the every page, you can do:
put the method inside a module or applications_controller.rb
add before_filter in applications_controller to populate the instance variable
add an extra partial app/viewes/shared/_latest_announcement.hmtl.erb
include the partial into the main layout applications.html.erb

How can a partial detect if it's being rendered by a mailer?

I have a partial that is being shared between a few different views, and a mailer template. This partial should attempt to use the user's session to store some state information if possible.
Determining if the session exists seems to be a bit of a problem. Within the partial, calling defined?(session) always seems to yield true during a mail render (is this a bug?), but attempting to access "session" in any way yields an "undefined method" exception.
As of now, I'm having my mailer use a #for_mailer instance variable to signal this partial to render differently, but this doesn't seem very elegant. Is there some simple way for the partial to figure out whether or not it's being rendered by a mailer, as opposed to being rendered in the context of a web request?
I would also create two partials for this but here is an alternative solution as well.
Assuming that it is coming from a different controller and action, you could check the params[:controller] and params[:action].
If you end up doing this more than a few times, you will probably end up with more code than just rewriting the partial. What do you want to be different between the two presentations?

Resources