I develop a rails app with exercises (for kids with learning difficulties in math). The interactive part of the exercises is written in javascript. I store each exercise in a database. The javascript contains
<%= asset_path('to_images') %>
I can read the scripts into the controller and write the content to a partial, but I think it would be better to capture the scripts in a variable, like:
#animation = exercise.animation
where any code containing <%= asset_path(...) %> would be replaced with the correct fingerprinted route to the asset.
Here is an example of a code snippet in exercise.animation:
$("#hundred_square td").css({
backgroundImage: 'url(<%= asset_path("exercises/shapes/circles/circle_open_black_48.png") %>)',
backgroundSize: "2vw",
backgroundRepeat: "no-repeat",
backgroundPosition: "center"
});
I have already tried to
class Exercise < ActiveRecord::Base
include ActionView::Helpers::AssetUrlHelper
and
self.animation.gsub(/\<\%\=\s*asset_path\((.+)\)\s*\%\>/) do |match|
address = $1
puts "#{address}"
=> "exercises/shapes/circles/circle_open_black_48.png"
puts "#{asset_path(address)}"
=> /"exercises/shapes/circles/circle_open_black_48.png"
puts "#{ActionController::Base.helpers.asset_path(address)}"
=> /"exercises/shapes/circles/circle_open_black_48.png"
end
do not produce the result I need.
Thanks for your suggestions!
One way to fetch the MD5 fingerprint value is to use Sprockets' find_asset method, passing in a logical path to your asset to get a Sprockets::BundledAsset instance. For example
[1] pry(main)> Rails.application.assets.find_asset('application.js')
=> #<Sprockets::BundledAsset:0x3fe368ab8070 pathname="/Users/deefour/Sites/MyApp/app/assets/javascripts/application.js", mtime=2013-02-03 15:33:57 -0500, digest="ab07585c8c7b5329878b1c51ed68831e">
You can call digest_path on this object to get it's MD5 sum appended to the asset.
[1] pry(main)> Rails.application.assets.find_asset('application.js').digest_path
=> "application-ab07585c8c7b5329878b1c51ed68831e.js"
With this knowledge you can create a helper to return the digest_path for any asset in your application, call this helper from within your .js.erb files or from within your model.
See this answer for more details on this approach.
Related
I have extensively researched this matter both on Stack Overflow and Google but found nothing conclusive. Since I'm completely new to the concept of API usage within Rails I have to ask for some advice.
I have followed the procedure from the github page
I have included the Unsplash helper in application_helper.rb as follows
def show_photo
Unsplash::Photo.find("tAKXap853rY")
end
and simply added
<%= image_tag show_photo %>
in my view.
This returns an object (So connectivity is good)
<img src="/images/#<Unsplash::Photo:0x007fc4b2f953c0>" alt="#
<unsplash::photo:0x007fc4b2f953c0>">
I'm aware that Rails is looking for a picture in the assets/images folder
How do I parse the inbound JSON and render it in my Rails view?
You can access to the urls key within the OpenStruct attributes in the Photo object that includes the raw, full, regular, small and thumb sizes, also as keys.
So, just to test you could use the raw one, like:
<%= image_tag Unsplash::Photo.find('tAKXap853rY')[:urls][:raw] %>
Or I think you could modify your method to accept one parameter which is the size key of the image, like:
module ApplicationHelper
def show_photo(size)
Unsplash::Photo.find("tAKXap853rY")[:urls][size.to_sym]
end
end
Then:
<%= show_photo('raw') %> # 'full', 'regular', etc ...
further to this solution I am trying to display the photographer's name by using the user.name method.
In the console I can get the following :
photo = Unsplash::Photo.find("tAKXap853rY")
photo.user.name
will return
=> "Alejandro Escamilla".
But in RAILS :
def show_photo(size)
#photo =Unsplash::Photo.find("tAKXap853rY")[:urls][size.to_sym]
end
just trying to display the name in my view like:
<%= #photo.user.name %> will return "user undefined method".
The .user.name is accessible in the console but not in rails! What have I missed? Thanks
I've seen a lot of questions around this topic but no definitive solutions. I have a Rails 3.2 app that leverages ERB/slim and Coffeescript/EJS/Backbone. I inherited this codebase so some of these peripherals are a little over my head.
Problem
My venue view has a section that displays tips that were submitted to the venue. The list of tips has a "sort" function facilitated by JavaScript. The associated CoffeeScript file has event listeners for clicks on links for "recent" and "popular". On click, the JS does some work and utilizes Rails scopes to resort the list. I'm building out this tips list to include a little more data, specifically including the Rails helper time_ago_in_words. The original code updated the div containing the tips using a JST/EJS template in the Javascripts/templates asset directory.
These are the specific problems I encounter:
If I add .erb to the file chain (after updating the EJS interpreter to look for a different evaluation and interpolation pattern as to not conflict with ERB), I can evaluate basic Ruby expressions. The partial, however, fails to have access to the same tip variable that the JavaScript references. I know this is because Rails is server side and JS is client side. Consensus seems to be no, but is there a way to get that data to Rails?
While basic Ruby expressions can be evaluated (Time.now, for example), Rails helpers such as time_ago_in_words fail, complaining that the method is undefined for the class, though I am absolutely passing in the proper date object. Can Rails/helpers not be evaluated in this .EJS.ERB chain?
I can circumvent all of these problems if there is a way to reference the original ERB/slim partial used on load after the sort is performed. Is this possible? Right now the Coffeescript file called uses render and JST to call on the EJS template. Is it possible to somehow reference the original partial but with the updated sort?
Here's the relevant code.
##show.html.erb (venue)##
#tips
p.sort.span4
| Sort by:
= link_to 'Recent', '#', class: 'selected', id: 'sort-tips-recent'
| |
= link_to 'Popularity', '#', id: 'sort-tips-popularity'
#tip-list
= render resource.tips.by_recent
##tip_list_view.js.coffee##
class CS.TipListView extends Backbone.View
el: "#tip_module"
events:
"click #sort-tips-recent": "sortByRecent"
"click #sort-tips-popularity": "sortByPopularity"
initialize: ->
console.log("ERROR: View must be initialized with a model") unless #model
#model.bind('reset', #render)
render: =>
tips = #model.map (tip) -> JST['templates/tip'](tip: tip)
#$('#tip-list').html(tips.join("\n"))
sortByRecent: (e) ->
e.preventDefault()
#model.enableSortRecent()
#updateSelectedFilter('recent')
sortByPopularity: (e) ->
e.preventDefault()
#model.enableSortPopularity()
#updateSelectedFilter('popularity')
updateSelectedFilter: (sort) ->
#$('p.sort a').removeClass('selected')
#$("p.sort a#sort-tips-#{sort}").addClass('selected')
##tip.jst.ejs.erb (called from tip_list_view.js.coffee after sort change##
<div class="tip">
<p class="tip-author">
<$= tip.get('user').username $>
</p>
<p class="tip-timestamp">
Eventually, time goes here
</p>
<p class="tip-likes pull-right">
<$= tip.upvoteCountText() $>
</p>
<p id="<$= tip.domId() $>">
<$= tip.get('text') $>
<$ if(tip.get('can_upvote')) { $>
upvote
<$ } $>
</p>
<div>
Definitely at a loss here - any and all help is greatly appreciated. Let me know if there is any other detail or code I can provide for background. Thanks in advance!
With your ejs.erb, Rails is generating the template, but has no idea what you will use it for. Then your Backbone app is fetching the tip from Rails via ajax, and populate the model accordingly. Then it's your backbone app that populates and renders the template. Rails cannot chime in here, at least not in this configuration where backbone expects json and then renders the template. Everything is happening in the front-end.
The only thing you can do is ask Rails to give you the result of your helpers inside the json. I am guessing you have a Tip.rb model and it should implement:
include ActionView::Helpers::DateHelper
...
def as_json(options = {})
{
:name => self.name,
... (all the model attributes that you want to expose)
:date_age => time_ago_in_words self.created_at
}
end
That being said, I would def use a client side solution, also because: http://rails-bestpractices.com/posts/105-not-use-time_ago_in_words
I have a scenario where I'd like to pass back a long message with my JSON. Instead of writing it out with string concatenation I'd rather put together an erb template that I can render into my JSON. Below is the code I'm currently trying:
object #invitation
node(:phone_message) do |invitation|
begin
old_formats = formats
self.formats = [:text] # hack so partials resolve with html not json format
view_renderer.render( self, {:template => "invitation_mailer/rsvp_sms", :object => #invitation})
ensure
self.formats = old_formats
end
end
Everything works as expected the first time this code is run, however, I run into problems the second time I run it because it says there is a missing instance variable (which I assume was generated and cached during the first run).
undefined method
_app_views_invitation_mailer_rsvp_sms_text_erb___2510743827238765954_2192068340
for # (ActionView::Template::Error)
Is there a better way to render erb templates into rabl?
You could try using ERB as standalone, and not going through the view renderer, like so:
object #invitation
node(:phone_message) do |invitation|
begin
template = ERB.new(File.read("path/to/template.erb"))
template.result(binding)
end
end
binding is a method on Object (through the Kernel module) and it returns the binding which holds the current context, which also includes instance variables (#invitation in this case)
Update:
Don't really know if this will help you get any further (and I also realised it's been more than a year since you posted this), but here's another way to render ERB templates in a standalone fashion:
view = ActionView::Base.new(ActionController::Base.view_paths, {})
class << view
include ApplicationHelper
include Rails.application.routes.url_helpers
end
Rails.application.routes.default_url_options = ActionMailer::Base.default_url_options
view.render(:file => "path/to/template.html.erb", :locals => {:local_var => 'content'})
When I have time I should actually try this with Rabl.
I'm trying to use delayed_job to update a remote database via xml
In my lib folder I put a file with a class that should do a render_to_text with template.xml.builder, but I get:
undefined method `render_to_string' for #<SyncJob:0x7faf4e6c0480>...
What am I doing wrong?
ac = ActionController::Base.new()
ac.render_to_string(:partial => '/path/to/your/template', :locals => {:varable => somevarable})
I had problems with a undefined helper method then I used ApplicationController
ApplicationController.new.render_to_string
render_to_string is defined in ActionController::Base. Since the class/module is defined outside the scope of the Rails controllers the function is not available.
You are going to have to manually render the file. I don't know what you are using for your templates (ERB, Haml, etc.). But you are going to have load the template and parse it yourself.
So if ERB, something like this:
require 'erb'
x = 42
template = ERB.new <<-EOF
The value of x is: <%= x %>
EOF
puts template.result(binding)
You will have to open the template file and send the contents to ERB.new, but that an exercise left for you. Here are the docs for ERB.
That's the general idea.
Rails 5
render_to_string and others are now available as class methods on the controller. So you may do the following with whatever controller you prefer: ApplicationController.render_to_string
I specifically needed to assign a dynamic instance variable for the templates based on an object's class so my example looked like:
ApplicationController.render_to_string(
assigns: { :"#{lowercase_class}" => document_object },
inline: '' # or whatever templates you want to use
)
Great blog post by the developer who made the rails PR: https://evilmartians.com/chronicles/new-feature-in-rails-5-render-views-outside-of-actions
You could turn your template.xml.builder into a partial (_template.xml.builder) and then render it by instantiating an ActionView::Base and calling render
av = ActionView::Base.new(Rails::Configuration.new.view_path)
av.extend ApplicationController.master_helper_module
xml = av.render :partial => 'something/template'
I haven't tried it with xml yet, but it works well with html partials.
I have a model called Book, which has_many :photos (file attachments handled by paperclip).
I'm currently building a client which will communicate with my Rails app through JSON, using Paul Dix's Typhoeus gem, which uses libcurl.
POSTing a new Book object was easy enough. To create a new book record with the title "Hello There" I could do something as simple as this:
require 'rubygems'
require 'json'
require 'typhoeus'
class Remote
include Typhoeus
end
p Remote.post("http://localhost:3000/books.json",
{ :params =>
{ :book => { :title => "Hello There" }}})
My problems begin when I attempt to add the photos to this query. Simply POSTing the file attachments through the HTML form creates a query like this:
Parameters: {"commit"=>"Submit", "action"=>"create", "controller"=>"books", "book"=>{"title"=>"Hello There", "photo_attributes"=>[{"image"=>#<File:/var/folders/1V/1V8Kw+LEHUCKonqJ-dp3oE+++TI/-Tmp-/RackMultipart20090917-3026-i6d6b9-0>}]}}
And so my assumption is I'm looking to recreate the same query in the Remote.post call.
I'm thinking that I'm letting the syntax of the array of hashes within a hash get the best of me. I've been attempting to do variations of what I was expecting would work, which would be something like:
p Remote.post("http://localhost:3000/books.json",
{ :params =>
{ :book => { :title => "Hello There",
:photo_attributes => [{ :image => "/path/to/image/here" }] }}})
But this seems to concatenate into a string what I'm trying to make into a hash, and returns (no matter what I do in the :image => "" hash):
NoMethodError (undefined method `stringify_keys!' for "image/path/to/image/here":String):
But I also don't want to waste too much time figuring out what is wrong with my syntax here if this isn't going to work anyway, so I figured I'd come here.
My question is:
Am I on the right track? If I clear up this syntax to post an array of hashes instead of an oddly concatenated string, should that be enough to pass the images into the Book object?
Or am I approaching this wrong?
Actually, you can't post files over xhr, there a security precaution in javascript that prevents it from handling any files at all. The trick to get around this is to post the file to a hidden iframe, and the iframe does a regular post to the server, avoiding the full page refresh. The technique is detailed in several places, possibly try this one (they are using php, but the principle remains the same, and there is a lengthy discussion which is helpful):
Posting files to a hidden iframe