Render dynamic pages stored in database - ruby-on-rails-3

I want to store pages in the database that are dynamically added/edited/etc. Along with the page title and content I also supply which layout to use and a controller name and view name in order to support the instantiation of models which the view (e.g. text field in the pages table) will use.
I'm having trouble rendering the text from the database and having the layout still be used.
class AboutController < BaseController
def index
#model_data = ...
render_dynamic_page("about", "index")
end
end
class BaseController < ApplicationController
layout "public"
def render_dynamic_page(controller_name, action_name)
page = Page.where("`controller_name` = :controller_name AND `action_name` = :action_name", { :controller_name => controller_name, :action_name => action_name }).first
render :layout => page.layout_name, :text => page.content
end
end
I'm using :text here and I've also tried :inline but both seem to not render the content within the layout.
How can I accomplish this?

Normally if you want to allow users to edit the html pages in a rails application, and store the pages in a DB, the standard way is to use the template language called liquid.
It allows you to define dynamic content, ideal for CMS systems in rails, I think its better you have a look first.
following are some important links
liquid home page
rails cast about liquid
git hub page
and there are lots of resources in SO itself. :)

I was so close - I just had to switch the order of the parameters to the render method:
render :inline => page.content, :layout => (page.layout_name || "public")
I also added a default layout to use public in case a page.layout_name was not specified in the database. The other thing that's worth mentioning is the difference between :text and :inline - only :inline seems to actually "process" the content as if it were an ERB whereas the :text option just literally outputs the raw text.
Note : I ended up creating an ERB for each page using ERB.new(page.content), caching the list of dynamic ERBs and then pulling the pre-rendered ERB from the cache and displaying it using:
render :inline => the_cached_erb.result(binding), :layout => (page.layout_name || "public")
There is still some weird behavior going on but I think it will work in the end.

Just FYI and for greater tools variety you may want to check out Mercury editor:
http://jejacks0n.github.io/mercury/
https://github.com/jejacks0n/mercury
http://railscasts.com/episodes/296-mercury-editor

Related

How can I call a controller/view action from a mailer?

In my rails application I've created a business daily report. There is some non-trivial logic for showing it (all kind of customizable parameters that are used for filtering in the model, a controller that calls that model and some non-trivial view for it, for example, some of the columns are row-spanning over several rows).
Now I wish to send this report nightly (with fixed parameters), in addition to the user ability to generate a customize report in my web site. Of course, I wish not to re-write/duplicate my work, including the view.
My question is how can I call the controller action from my mailer so that it will be as if the page was requested by a user (without sending a get request as a browser, which I wish to avoid, of course)?
In answer to your question is if you are generating some sort of pdf report then go with using the wicke_pdf gem does exactly that generates pdfs. To send a report on a nightly basis the best thing for this is to implement some sort of cron job that runs at a particular time which you can do using the whenever gem. You can do something like:
schedule.rb
every :day, :at => '12:00am'
runner User.send_report
end
With this at hand you can see that you call the send_report method sits inside the User model class as shown below:
User.rb
class User < ActiveRecord::Base
def self.send_report
ReportMailer.report_pdf(#user).deliver
end
end
Inside send_report we call the mailer being ReportMailer which is the name of the class for our mailer and the method being report_pdf and pass in the user. BUT remember this is an example I have here I am not sure the exact specified information you want in a report.
Mailer
class ReportMailer< ActionMailer::Base
default :from => DEFAULT_FROM
def report_pdf(user)
#user = user
mail(:subject => "Overtime", :to => user.email) do |format|
format.text # renders report.text.erb for body of email
format.pdf do
attachments["report.pdf"] = WickedPdf.new.pdf_from_string(
render_to_string(:pdf => "report",:template => 'report/index.pdf.erb',
:layouts => "pdf.html"))
end
end
end
end
Inside the mailer there are a variety of things going on but the most important part is inside the format.pdf block that uses a variety of wicked_pdf methods (this is assuming that you are using wicked_pdf btw. Inside the block you create a new WickedPDF pdf object and render it to a string. Then provide it with the name of the report, the template and the layout. It is important that you create a template. This usually will where the report will be displaying from. The file type is a .pdf.erb this means that when this view or report is generated in the view the embedded ruby tags are being parsed in and the output is going to be a pdf format.
UserController
def report
#user = User.scoped
if params[:format] == 'pdf'
#Do some stuff here
User.send_report(#users)
end
respond_to do |format|
format.html
format.pdf do
render :pdf => "#{Date.today.strftime('%B')} Report",
:header => {:html => {:template => 'layouts/pdf.html.erb'}}
end
end
end
The key thing you asked that I picked up on.
how can I call the controller action from my mailer
In the controller simply collate a scope of Users, then check the format is a pdf, providing it is do some stuff. Then it will run the method send_report which I earlier highlighted in the user model class (Btw in your words this is the controller calling the model). Then inside the respond block for this there is a format.pdf so that you can generate the pdf. Once again note that you need a template for the core design of the pdf, which is similar to how rails generates an application.html.erb in the layouts. However here we have a pdf.html.erb defined. So that this can be called anywhere again in your application should you want to generate another pdf in your application somewhere else.
Think I've provided a substantial amount of information to set you off in the right direction.

Rails 3 form actions and methods

I have made a resource.
resources :dashboards
I have a partial file which contains a form and I want to use this partial (as the form elements won't change) to update and create. So here is what I have:
Controller
class DashboardsController < ApplicationController
def new
#dashboard = Dashboard.new
end
end
View
/dashboards/new.html.erb
<%= render :partial => "form", :locals => { :dashboard => #dashboard } %>
Partial Form
/dashboards/_form.html.erb
<%= form_for(#dashboard) do |form| %>
.....
<% end %>
Ruby Guide
The Ruby Guide states:
The Article model is directly available to users of the application, so — following the best practices for developing with Rails — you should declare it a resource. When dealing with RESTful resources, calls to form_for can get significantly easier if you rely on record identification. In short, you can just pass the model instance and have Rails figure out model name and the rest. For example:
## Creating a new article
# long-style:
form_for(#article, :url => articles_path)
# same thing, short-style (record identification gets used):
form_for(#article)
## Editing an existing article
# long-style:
form_for(#article, :url => article_path(#article), :html => { :method => "put" })
# short-style:
form_for(#article)
Result
I thought I have followed the Rails Guide correctly. Because I made #dashboard a resource. I could just pass it into the form and have it handle the action, method and the rest. Instead I'm getting this:
<form accept-charset="UTF-8" action="/dashboards" class="new_dashboard" id="new_dashboard_" method="post">
According to the docs. Shouldn't the action of my form now be "/dashboards/new" because we are on the new action? And should it be passing an extra field declaring the method to be put when I use the same code in the /edit action??
My result is always the same no matter what. The form never changes.
What am I doing wrong?
EDIT
Here is my router info from rake routes
GET /dashboards(.:format) dashboards#index
POST /dashboards(.:format) dashboards#create
GET /dashboards/new(.:format) dashboards#new
GET /dashboards/:id/edit(.:format) dashboards#edit
GET /dashboards/:id(.:format) dashboards#show
PUT /dashboards/:id(.:format) dashboards#update
DELETE /dashboards/:id(.:format) dashboards#destroy
You are correct that you should be able to "pass #dashboard into the form and have it handle the action, method and the rest." The issue here is what new is in the context of RESTful actions.
When you declare a set of resources with resources :dashboards, you are creating a set of routes which map requests to controller actions:
GET /dashboards index
GET /dashboards/new new
POST /dashboards create
GET /dashboards/:id show
GET /dashboards/:id/edit edit
PUT /dashboards/:id update
DELETE /dashboards/:id destroy
You can check this if you run rake routes.
The issue here is that the new action is defined as a GET request to the path /dashboards/new, i.e. this is the route for the form itself. The URL in the action attribute of the actual form is something else: this is where the form will post the data to with a POST request, which on the server (rails) side will map to the create controller action.
When you use the form helper with form_for(dashboard), a form is created with a route corresponding to what dashboard is: if it is a new record (i.e. it does not yet exist in the database), then the form action will be create (and point to /dashboards), whereas if it already exists it will point to the actual URL for the record (e.g. /dashboards/123). This is what makes the form helpers so useful.
So, to sum up, /dashboards is the correct URL, not for the new action but for the create action, which the form helper uses because dashboard is a new record. new is the route to the page where the form resides, i.e. /dashboards/new.
Hope that makes sense.
p.s. as a side note, you shouldn't be accessing #dashboard in the partial if you are passing it in as a local (:locals => { :dashboard => #dashboard }). Just use dashboard.

How do I create routes for a controller that does nothing with models in Rails?

Still pretty new to Rails, so if I'm taking the completely wrong approach, please feel free to straiten me out.
How do I make routes.rb aware that there's a root controller full of actions that don't manipulate models, while preserving the route helper methods? I'd like it to respond to requests like these:
http://localhost/download
http://localhost/share
With route helpers like
download_app_path
share_path
but without static named routes like these:
match '/download' => 'site#download', :as => :download_app
match '/share' => 'site#share', :as => :share
from a SiteController that doesn't create, show, or otherwise manipulates models from my app.
I've tried using an approach like this, but it works without generating the route helpers ( naturally )
match '/:action', :controller => 'site'
I could theoretically do without the route helpers, but I think they're a bit easier to read than passing hashes of url options to link_to or form methods.
Is there a way to accomplish a more resourceful root controller, or is what I'm trying to do unconventional for Rails?
Edit
For clarity, here's what this SiteController class looks like:
class SiteController < ApplicationController
def download
#custom_options = { .. }
end
def share
#custom_options = { .. }
end
def about
end
end
Its purpose is to allow me to collect pages that don't interact with resources ( such as Users or Friendships ) into a single controller and maintain them all in one place. I'm trying to set this controller up as the application root controller - so all paths from this controller will be directly off the app host ( myapp.com/download )
Thanks in advance!
routes and resources are not tied to models. it's just a RESTful convention. if you just want to use the index actions, in your example download and share could be done like
resouce :download, only: [:index]
resouce :share, only: [:index]
see all the examples in the guides http://guides.rubyonrails.org/routing.html
if you want to add the download and share functionality to some "resource" like, say a picture, then you would do something like:
resources :pictures do
get 'download', :on => :member
get 'share', :on => :member
end
a resource always has and endpoint /pictures for example. so if you want to have paths directly to your host, then you need to provide custom matchers in your routes like you did in your examples.

Rails - How to include a hyperlink in a text field

I have a notices model which contains records of notices for each user. Each notice record contains a message which is a text field with the notice message. These messages look like:
"#{current_user.username} liked your photo '#{#photo.name}."
In the example above, I would like the user and the photo to also be hyperlinks to that user and photo.
Here is a snippet from my likes_controller which generates a notice when a new like is created:
class LikesController < ApplicationController
def create
#photo = Photo.find(params[:id])
#like = Like.new(:photo_id => #photo.id, :user_id => current_user.id)
if #like.save
#notice = Notice.new(:user_id => #photo.user_id, :message => "#{current_user.username} liked your photo '#{#photo.name}'
end
Any thoughts on how I can include links in the message; is this even possible? Thanks.
Adding a link to your message is a rendering issue. In my opinion, you're rendering the message too soon, I would render it in the view.
If you change your Notice model so that it contains a the user_id and the like_id, you can render the notice text in the view (which also lets you localize the text later, should it prove necessary).
Rendering in the view lets you use the standard link_to helper to generate your links.

Displaying raw file content as Github

How could I display the raw file content as done in GitHub when displaying the file after clicking a "view raw" link?
E.g. I wanted to diplay *.html file's source but rails takes html in params[:format] and renders in its own way.
How could I achieve this?
Here's how I got this to work, though I am not exactly sure how you would want to use this in your application. Consider this a proof of concept that hopefully helps you achieve your goal.
Let's say you want to render the raw contents of the index action for your products controller if someone requests the "text" format:
def index
#products = Product.all
#raw = render_to_string('products/index.html', :content_type => 'text/html')
respond_to do |format|
format.html
format.text do
render :text => #raw, :content_type => 'text/plain'
end
end
end
This obviously isn't ideal since you're stuffing the "raw" version of the view in a variable even if someone requests the normal html format, but putting it in the format.text block screws up the content type for the partials inside index.html.erb. Again, this is just a proof of concept I came up with.
At any rate, now when you hit:
/products.txt
You will get the raw HTML of the page. And if you hit:
/products
It will render the normal, interpreted HTML in the browser.