I'm creating a simple CMS where there is a Page model that has a content field that stores the HAML code to be rendered for a page. So I know how to store the HAML and then render it, but I'd also like to be able to use Rails helpers interspersed in the HAML and have that render properly.
So an example HAML file:
%p
Come on over to 3rd Ward! Take a tour with one of our associates this summer and get a
%strong FREE $25 Class Gift Card and ONE FREE DAY of COWORKING.
%h2 How awesome is that?
=render("shared/schedule_tour")
I can render the HAML:
template = Haml::Engine.new(Page.find(params[:id]).content)
template.render
But how do I get it to accept my Rails Helpers?
Related
I'm trying to use the erb template on my angular templates folder, but the functionality is limited, for example,
The <% link_to
is working
but devise methods or even raw('sdmdslkc') pops an error that the method is not found.
for example
<%= link_to('Logout', destroy_user_session_path, :method => :delete) %>
work on views but not in angular templates (says destroy_user_session_path method not found)
Whats missing ? is it fixable?
let me see if i understand, u have some template with some tags that belongs to another language that is not angular, html, or js. if so then it is not fixable because angular sends an ajax to bring that html file and injects it to your page, alot after any server has rendered any part of the page.
my suggestion for a work around is to try and tell angular to take a server page, say it was with asp.net and i would like some server tags i would tell angular that the template is somepage.aspx while making sure that my page evetually returns plain html.
I search around for quite some time but there's are no close solution for this.
For example,
I had generated 2 controllers: Articles, and Calendars.
There have been 2 javascript files also been generated according to these 2 controllers as:
calendars.js and articles.js.
The problem is, if they (2 files) all are included into application.js without any condition. i.e when I open articles/index action, the calendars.js has been imported on page
or when I open calendars/index action (or any action), the articles.js has been imported on page.
What I am looking for is a methodology to separate javascripts file according to the controller that they are belong to?
I also having the same question related to css files.
Thank you and Best regards,
Dat
What you probably want to do is only execute certain pieces of javascript on each controller (action). You can achieve that easily with this gem: https://github.com/intrica/rails_document_ready.
You can add in your index.html.erb file.
For the specific page where you want this js file add in your html file.
<%= javascript_include_tag "articles" %>
I found paloma: https://github.com/kbparagua/paloma
I develop new Rails application for about 2 months with this gem, and it provide quite effective way to manage javascript.
I'm writing a Rails 3.2 app with backbone, and since I only need rails to render one page, I have no need for a controller to back the index page.
Is there a way to render the layout (application.html.erb) without a controller? I imagine it would be a configuration in the routes.rb file?
My first thought was to move it to index.html in the /public directory, but I need to take advantage of erb for javascript includes and CSRF helpers, etc.
I get that you don't need the controller to do anything, but Rails is "opinionated" software; it expects a controller and a view, because that is the way it was designed, and trying to work around that is going to give you a lot of trouble.
Just
create an empty controller class in /app/controllers/main_controller.rb
create an empty view file /app/views/main/index.html.erb
set up a route like :root => 'main#index'
Easy peasy.
So I am storing the whole embed code from youtube into my database and then outputting it like this.
<%= raw(people.video) %>
which outputs the general iframe tag copied from youtube:
<iframe src="foo" width=400 height=380></iframe>
the problem is that it actually displays that tag instead of embedding the video into the page itself. I can get around this just by storing the src in the database.... but this is part of a mini cms system and the site admins would find it much easier just to copy and past the embed code from youtube. Is there someway I can specify for the iframe to actually render instead of just spitting out the html on the page?
You can use the raw method to render the HTML of a string, are you storing it that way? You can also try the RedCloth gem - I was able to get this working like so:
<%= raw RedCloth.new(#page_content.content).to_html %>
Which allowed for my custom CMS to use the Textile markup
http://en.wikipedia.org/wiki/Textile_%28markup_language%29
I'm not 100% on what you're seeing, since raw should render HTML, but you might also try to include the embed tag using a JavaScript.write, since there are some issues related to Flash objects embedded in HTML vs JavaScript.
Let me know if this doesn't work, and if so, can you provide screenshots or copy what you're seeing on the page?
I want to have a different layout for the home page (root_url) than the other pages in my web app. I would like to be able to use <%= render 'layouts/pages' %> for pages that are not the home page. How do I go about doing this?
Majority
The best way to do this is to name the layout for the majority of your app layouts/application - this way, Rails will automatically assume this layout for that majority without you needing to do anything else.
Home
For your home page, you can add this line to the bottom of your controller action:
render :layout => "home"
This will tell Rails not to use application, but instead to point to your home/root page's layout, which in this case would be located at layouts/home.