Passing large amounts of Template Code into a variable? - ruby-on-rails

I'm using the following to populate a series of markers on a Google map in Rails:
marker = GMarker.new(coords, :icon => home, :title => "home", :info_window => "Info Text Goes Here" )
I'm trying to customize the info window beyond the text and trying to pass a lot of info into it, but I'm not sure exactly how to do it beyond making a really long annoying string. What's the best strategy to pass a lot of formatted info in HTML/CSS? Partials of some sort?

Ahh well, I can't yet comment, but Andrew's code should work on the View, but not in your controller.
It all depends on how your are constructing your Google map markers. If you are generating them in your controller, you'll want to create a function that will return the text for you and pass that to your GMarker object.
If you are creating them via Javascript in the view, then you'll want to use a partial to load the information in.
Perhaps describe the problem further.
Anyway, Sorry to clutter up the answer space with a comment on Andrews answer. :D
Cheers!
Dustin

You should be able do something like this from your view:
marker = GMarker.new(coords, :icon => home, :title => "home", :info_window => render(:partial => 'info_window') )
Where you have a partial in the same folder named _info_window.html.erb

For some reason, using (render :partial) as an argument supplied to the Gmarker caused only the partial to be rendered. When I changed it to render_to_string it worked.

Related

Rendering Pagedown Markdown in Rails 4

I have a properly working pagedown editor in a rails 4 application, but I believe I'm missing something simple to render the pagedown data at a later point in my application.
Here's the gem (and initialization) I'm using: https://github.com/hughevans/pagedown-bootstrap-rails
Any ideas on how to render that data with the already-used gems?
EDIT: I think the root of my problem is it's not storing the data as the HTML version, so it doesn't render it when I display the data again. It's missing the converter step when the form gets saved, but I don't see any specific instruction on how to do that, so I assumed it was default part of these gems.
EDIT2: I've since taken the approach to convert the Markdown on each page load, with the following code:
Unfortunately, it doesn't use all the Pagedown Markdown, but it's at least handling new lines properly:
Any ideas?
Thanks!
So the answer to this question is two fold. Either you can convert the MD to HTML and store it in the database, or leave it as MD in the DB and convert it to HTML every time you want to render it. Note that you'll need to convert it back to MD (I'm not sure if this is entirely easy or not) if you want that field to be editable in the original MD.
Since this app doesn't care about performance, I decided to store it as MD and render it.
The results I was getting above stemmed from HAML's whitespace rendering that it does, so I had to use a little HAML filters to work around that.
The HAML ended up looking like:
.wmd-output><
:preserve
#{#object.attribute}
The second challenge was actually pretty straightforward, just not explicitly stated anywhere in the Markdown documentation. So I just wrote some javascript that automatically converts any .wmd-output class into it's proper Markdown on page load:
$(function() {
$('.wmd-output').each(function(i) {
var converter = new Markdown.Converter();
var content = $(this).html();
$(this).html(converter.makeHtml(content));
});
});
I hope this helps other people in the future.
This line of haml referenced should be what you need to render it:
= f.input :description, :as => :pagedown, :input_html => { :preview => true }

Is it a good idea to create a helper method for this type of scenario?

I have this code in my html.erb at many places.
<div id="left-nav">
<%= render :partial => 'tests/tests_left_menu' %>
</div>
Is it a good idea to create helper method for this type of code ?
How to write this code in helper ?
I see a few good strategies to use in your situation. Pick and choose based on your project's specific requirements.
You can just put div#left-nav and its contents into yet another partial like tests/tests_left_menu_with_wrapper. This saves you a couple of lines.
If you can generalize the cases when the entire segment appears, you can move it into a layout. This way, once you declare the layout for a particular action using the ActionController::Base.layout method, you'll be able to skip writing the entire segment altogether.
You can write a helper, but it's not clear what advantage it confers over simply using content_tag. You're probably better off using partials or layouts.
Personally i don't think there's a need to, and i think it's more like because you are not using other tools like haml to help reduce the number of lines in an erb files
the same code can be achieved in haml in just 1 line:
#left-nav= render :partial => 'tests/tests_left_menu'
hope this helps =)
I suppose if you have that code in many places I'd move the the div into the partial. If you need the flexibility to have tests_left_menu outside of the div I'd still pick two partials over a helper in this scenario. Avoid writing html in Ruby when you can :)

gmail-like popup checkbox list in rails?

I need to build something like what gmail does for it's labels... It has a button that when pressed pops up a scrolling list displaying the labels with checkboxes for selection.
I'd like to hear about approaches to do the popup and how to place it right under the button.
Also, I'd like to be able to observe the checkbox select/deselect events and take action, so advice on that part would also be appreciated... otherwise, I guess I'll have to put a form with a submit button and handle the new selections when the user submits.
If the checkbox list is static, you can do all this directly in the rendered action. Otherwise, two approaches are possible:
Use button_to_remote to retrieve an action displaying the popup and also serving the necessary js;
Use button_to_function to retrieve some XML or json (at your option) from an action, with the necessary labels and values for checkboxes, then render the popup.
The first may be easier to do if you're not familiar with all this, while the second is way more efficient, as only data is passed through the asynchronous call, and not markup nor javascript.
About your last question, if (un)checking the checkbox must result in a server side action, prototype_helper provides a convenient observe_field function, to be used like this:
<%= check_box "foo", "bar" %>
<%= observe_field "foo_bar", :url => {:action => :some_action, :controller => :some_controller} %>
If the (un)checking can be managed on client side, you can simply use:
<%= check_box "foo", "bar", { :onclick => "someFunctionToDoWhatINeed(someArg);"} %>
Just two notes:
JavascriptHelper and PrototypeHelper are just this, helpers: they allow you to do some things with a very simple syntax and are great, as long as they are helping; when they are no more, feel free to drop them and go for plain javascript.
I've used prototype for a while, but then I fell in love with jquery; you may want to take a look at it.
Please edit your question or comment my answer if I didn't understand your question and/or was unhelpful.

Parsing a 'template' in rails (alternatives to gsub)

I have a 'template' system in my CMS rails app. Basically the whole HTML template is stored in a database column and it has key code in the string (like <!--THEME_Body-->) that gets replaced with content generated by the application.
I use an actual layout to render the template. All that is in the layout is:
<%= generate_theme.gsub!('<!--THEME_Body-->', yield) -%>
That helper takes the correct theme and further gsubs other area like Meta data and Breadcrumbs.
I'm just wondering if there's a better way to go about this? Perhaps using content_for or something like that?
That's a pretty good way of going about it, although I would make use of Ruby's sexy syntax to combine all the lines into something a little more syntactically correct - it speeds the script up and it looks a damn sight nicer too!
tags = {
'<!--THEME_Body-->' => yield,
'<!--THEME_Head-->' => yield(:head),
'<!--STYLESHEET-->' => stylesheet_link_tag('application')
}
tags.each { |str, rep| generate_theme.gsub!(str, rep) }
Bear in mind that this code should not go in a view - it should ideally be put in a model, as it's to do with the application's data, but it could also go in a helper somewhere. If it's an instance variable in a model, you could just call
generate_theme.parse
And that code could be executed - it looks a lot better and sticks to the standard MVC convention of cleaning up the view as much as possible.
Jamie

help with rails render action vs routing

I was using some image cropping example that I found online and now I got confused. There is actually no "crop" method in my controller. Instead (following the guide) I put a
render :action => 'cropping', :layout=> "admin"
In my create method. That renders a page the view called cropping.html.erb . It works fine but I have no idea how to link or render that page otherwise, like if I wanted to hit a URL directly or press a button to recrop an image. Should I actually create a crop method in my controller and hook it up via routing if I want to be able to do this, or is there a way within my view to link to the same place that renders the cropping action?
Sorry about the confusion :) It doesn't help that the first version of the tutorial did have a cropping method and he removed it!! Any explanation on why one method is better over the other would be great. Thanks!!
In your case you would normally name the file create.html.erb which is where rails will look for the file by default. Writing code like:
render :action => 'viewname'
usually happens if you want to render one file in one case and a different one in another.
The best way to do this depends on how you intend to be using the cropping template, and the associated controller logic. You'll find it useful to read up on the render documentation before proceeding.
If you're only ever going to use the cropping template one way. With the same controller logic that independent of the referring action (As in; Not part of a form submission). Then you should be defining a new action and route. It's your choice of whether you want to make a named route or just add a new one to the resource definition in routes.rb
Depending on how you define your route you could do a link_to "Cropping", cropping_url
If you're going to be rendering it from multiple controllers each needing different preparation before rendering the template.
render :template => 'path/template_name'
Where path is the relative path from TEMPLATE_ROOT (RAILS_ROOT/app/views unless otherwise defined) and template name is the filename without the trailing .html.erb/.rhtml
If you want to render cropping as a part of another view, you can make it a partial.
To make it a partial just rename the file to '_cropping.html.erb'. Now it can be called from any view with
<%=render :partial => 'path/partial_name' %>
Again, path is the TEMPLATE_ROOT relatave path to your partial. And partial_name is the file name of the partial, after omitting the leading underscore and trailing .html.erb or .rhtml.
Note: In either of the solutions involving a path to a template, the path can be omitted if the calling controller matches the path. Ie: if the template path is 'users/cropping.html.erb' called from the UsersController.

Resources