How to create a custom page in ActiveAdmin gem - ruby-on-rails

Ruby 2.0, Rails 4.0, PSQL 9.3
In the ActiveAdmin documentation there is the example:
ActiveAdmin.register_page "My Page" do
content do
para "Hello World"
end
end
Where do I put this code? The documentation says:
In the above example, a new page will be created at /admin/my_page
with the title “My Page” and the content of “Hello World”.
This implies that such a file would be created automatically somehow? Nevertheless, I created a file named import.rb under app/admin and the Import item in the menu does appear. However, I am not able to use HTML, as this file is .rb and not .erb. I suppose, in order to be able to use html, I need to create a partial and den render it within the content method. But when I look under app/views there is not admin folder (only layouts). Does this mean I need to create the folder admin under app/views? If yes, where should I put my partial - directly under app/views/admin or under a new folder app/views/admin/import?
I am sorry for the menu questions, but the documentation of ActiveAdmin is pretty modest. I would really appreciate if someone could provide a more details explanation of the steps needed for creating and adding content to a new page in ActiveAdmin.

What the documentation meant was that if you create a new custom page app/admin/my_page.rb, this page will be available in the URL /admin/my_page (if you are using the default ActiveAdmin configuration).
Regarding rendering of an ERB or HAML partials for your my_page.rb, you can do it this way:
ActiveAdmin.register_page "My Page" do
content do
render :partial => 'about'
end
end
This will look under the directory app/views/admin/my_page/. If the directories do not exist, create them. Also, you can still specify other directories by referencing the full template path (e.g. shared/sections/about) like you would for a non-ActiveAdmin controller.

Related

Embed web page coded in Ruby on Rails in another web site?

I would like to make my web page that I coded with Ruby on Rails as backend embeddable so that users are able to easily share it by copy and pasting some embed code. (much like YouTube embed code, but for a webpage)
Could someone point me to a tutorial or general direction how to go about doing so?
I'm planning to embed my web page in Joomla CMS.
Many thanks.
Pier.
Let's suppose you want to create a Widget for a mobile app store. The widget would allow to embed information of a certain app in any web page.
If we use the script tag, the embeddable code could look like this:
<script src="http://my_appstore.com/apps/1234.js" type="text/javascript"></script>
Where 1234 would be the id of the specific app we would like to embed.
If we use the iframe tag the code to put in other web pages could look like:
<iframe src="http://my_appstore.com/apps/1234" width="500" height="200" frameborder="0"></iframe>
First thing we have to decide is what kind of tag to use. Using and iframe tag is more straight forward but we are limited to use an iframe. Using an iframe is not a bad option but if you distribute this to third party web pages you won't be able to change that afterwards. Instead, it is preferable to use a script tag that will insert an iframe. This tag will also allow you to switch to embedding your content directly into pages if you choose to do so afterwards.
Inserting an iframe means that the proportions of your content have to be fixed and can't change to adapt to different window sizes in the parent window. Embedding your content directly doesn't have this problem but you have to be very careful with CSS and add style to all your elements because otherwise they will inherit the host page styles. Also embedding your content directly and then making AJAX calls will likely produce cross-browser request problems unless you use JSONP.
Let's first create a simple web page with Sinatra that we will use to embed our Rails Widget:
mkdir host_page
cd host_page
With your text editor create host.rb file inside host_page folder:
# host.rb
require 'sinatra'
get '/' do
erb :index
end
Create index.erb and launch host_page:
mkdir views
cat '<script src="http://localhost:3000/apps/1234.js" type="text/javascript"></script>' > views/index.erb
ruby host.rb
Now if we visit http://localhost:4567/ we see nothing but there will be a widget there soon.
Let's now create the rails app that will be embedded. Start with a new folder for your project and do:
rails new widget
cd widget/
rails g controller apps
rm app/assets/javascripts/apps.js.coffee
Add the needed routes:
# config/routes.rb
MyApp::Application.routes.draw do
resources :apps
end
Edit your apps controller:
# app/controllers/apps_controller.rb
class AppsController < ApplicationController
def show
#mobile_app = {
:title => "Piano Tutorial",
:descr => "Learn to play piano with this interactive app",
:rating => "*****"
}
end
end
In that controller we are always returning the same app. In a real situation we would have a model and the controller that would retrieve the appropriate app data from the model id found in params.
Create your javascript view and start the server:
echo 'document.write("<h3><%=#mobile_app[:title]%></h3><p><%=#mobile_app[:descr]%></p><p><em><%=#mobile_app[:rating]%></em><p>");' > app/views/apps/show.js.erb
rails server
And that's it. Go to http://localhost:4567/ and see your widget.
In case you want to use an iframe, replace the contents of your show.js.erb file with this:
document.write("<%=escape_javascript(content_tag(:iframe, '', :src => app_url(params['id'])).html_safe)%>");
Here we are using a content_tag but it could also be done in a way similar the previous one by just using the <iframe> tag as previously.
Obviously if we use an iframe, we are doing two calls, one to render the iframe and the other one to load the contents of that iframe. For this second call we are still missing an html view. Just create the view like that:
# app/views/apps/show.html.erb
<h3><%=#mobile_app[:title]%></h3>
<p><%=#mobile_app[:descr]%></p>
<p><em><%=#mobile_app[:rating]%></em><p>
Now you can point again to http://localhost:4567/ and see your widget inside an iframe.
A bit late, but I stumbled over this Question while searching for a solution by myself. I found a Gem that does exactly what's described. It will make your rails app embeddable like YouTube Videos or Content from other Webpages like Google Maps, Instagram, Twitter… It's called EmbedMe
To use you simply need to change your Routes to define, which Paths need to be embeddable
get 'private', to: 'application#private'
embeddable do
get 'embeddable', to: 'application#embeddable'
end
Gem on Github or Documentation
In case anyone is coming across this now, almost 9 years later...
If you use the JavaScript method, you'll have to allow Cross-Origin requests, like #abessive mentioned in their comment above. I was able to do this by adding this to the top of my controller class:
protect_from_forgery except: :method
where :method is the method that will be called for the embed request.
Here's my controller:
class PagesController < ApplicationController
protect_from_forgery except: :home
def home
render 'index.js'
end
end
And the relevant route in routes.rb:
get "index.js", to: "pages#home"
And I have views/pages/index.js.erb with some JS code that renders the widget.
(I'm using Rails 6.1.4)

Refinery CMS: Adding custom field to custom engine index page

i have generated a custom engine "Pianos" inside of refinery cms. On the index piano page, I'd like to display the content of a related custom field called "pianos_introduction_text".
What would be the correct way to generate and display this custom field?
Thanks for your help!
Our preference however is to create custom page parts in the normal pages section, rather than to use the copywriting engine.
Under the pages admin section you should see that a page has been added for pianos.
If you add a new page part (or even use an existing one), you can then retrieve and render it from the engine's index page like this:
<%= raw #page.content_for(:new_page_part) %>

Adding content to static files(pages)

I have several static files(pages), which are basically copies of my website pages source code, with the content changed.
These files support my website, (keeping the same format) in various ways.
For example the menu part is:-
<body>
<div id="menu">
<ul class="level1" id="root">
etc
etc. until
</ul>
</div>
Unfortunately every month or so my menu bar changes and I have to update each static file manually.
As each of my static files have the same menu.
Is it possible to have one menu file which can be updated and have the static files load them automatically.
I plan to have several more static files. So this would be a great help if someone can suggest how to accomplish this.
Oh yes. Use some javascript magic to load the menu bar upon page load and keep it in menu.html.
One solution may be to use a spider (wget --recursive) to download generated pages directly from your application. One command, and you have the full copy of your site. (just add some useful options, like --convert-links, for example).
The other option may be to write an after_filter in your controller, and write the generated content to a file (not always, but for example when you add a parameter ?refresh_copy=1). Maybe just turning on page caching would be suitable? But the problem will be that you will not be able to trigger the controller action so easily.
If you don't want the whole site copied, just add some specific routes or controllers (/mirrorable/...) and run the spider on them, or just access them manually (to trigger saving the content in the files).
I ended up creating one controller without a model.
rails g controller staticpages
I then created a layout file which imported the individual changes to the layout, via a "yield" tied to a "content_for" in the view files(static files(pages) in the "view of staticpages" (for example abbreviations, aboutthissite etc etc).
The rest of the static file loaded with the usual "yield" in the layout. Works a treat. No more updating the menu bar all done automatically.
To get to the correct static file I created a route using:-
match 'static/:static_page_name'=> 'staticpages#show' (or in rails 2.x:-
map.connect 'static/:static_page_name', :controller=> "staticpages", :action=> "show"
"static_page_name" variable accepted anything after "/static/" in the url and passed it to the controller "staticpages" in which I set up a show action containing:-
def show
#static_page_name = params[:static_page_name]
allowed_pages = %w(abbreviations aboutthissite etc, etc,)
if allowed_pages.include?(#static_page_name)
render #static_page_name
else
redirect_to '/' #redirects to homepage if link does not exists
end
end
I then only had to change the links in the website. (e.g.<%= link_to " About This Site ", '/static/aboutthissite' %>)
and viola! its all working.

Rails + facebox + authlogic - how?

on my web site I want to have login/registration form in modal window done using facebox (jQuery plugin). What is better:
Create view with one method and template that has form and refer facebox to this view.
Create static HTML file in public directory and refer facebox to this static page.
What I want to achieve is:
Easy verification (like "user name already taken", "password confirmation doesn't match password" and stuff like that).
Easy submit and redirect
I'm new to Rails, I just know about forms verification in Django, so for Django I would probably choose option 1, but it might be another thing in Ruby.
If you want the verification to come back to the registration page, you should make it a dynamic page.
The other problem with a static page in the public directory is that your links all become hardcoded, so if you application ever lives off the domain root (i.e. example.com/app) the links in that static file could be wrong.
Additionally, if you ever need to move your images to a different host, you lose the advantages of the image_tag.
Only use static resources if you know things won't change and you need speed. If your dynamic pages are too slow, you can cache them, or you might be doing something wrong.
UPDATE: (to address the first comment)
You can't use the rails functions to build your URLs when you are in the public folder. If you need rails generated URLs in your javascript, trigger them from a rails view page.
Generally, I'll do the following:
In application.html.erb in the head tag:
<%= yield :headScripting %>
Then in the view page that is triggering the javascript:
<% content_for :headScripting do %>
jQuery().ready(function() {
jQuery("#placeholder").load("<%= summary_model_path(#model) %>");
});
<% end %>
That would load the summary text from the model controller action summary. This would probably render :text => "summary" or render :layout => false depending on your needs

Ancillary files for view templates in a Rails application

A very basic question on Rails. I would like to create a Rails application that, besides the "regular" html view, it can generate a set of XML files. I am aware that I can tailor the templating using the "respond_to" command and using ERB with the templates ???.xml.erb. My question is: suppose that the final document consists in several of these XML files (some are template and must be autoedited by the application but some others are "static" and do not need to be changed). In this scenario, which would be the best location in the application folder to put these ancillary files of the templates?
Thanks a lot in advance
Miquel
I hope I'm understanding your question...
If your responses are most static you should continue using the .xml.erb templates, but if you need something more dynamic maybe you could consider builder templates, which have a .builder or .rxml extension.
This is an example of what a builder template might look like:
xml.rss("version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/") do
xml.channel do
xml.title(#feed_title)
xml.link(#url)
xml.description "Basecamp: Recent items"
xml.language "en-us"
xml.ttl "40"
for item in #recent_items
xml.item do
xml.title(item_title(item))
xml.description(item_description(item)) if item_description(item)
xml.pubDate(item_pubDate(item))
xml.guid(#person.firm.account.url + #recent_items.url(item))
xml.link(#person.firm.account.url + #recent_items.url(item))
xml.tag!("dc:creator", item.author_name) if item_has_creator?(item)
end
end
end
end

Resources