Rails - How to retrieve audio from cloudinary to heroku using rails - heroku

Im creating a post model with Audio inside.
Im deploy my app into heroku, and store all files to cloudinary.
And audio successfully sent to the cloud and Audio can be play on localhost
But the problem is
I cant retrieve the audio on my rails app(on heroku).
This just looks like a blank play button with no audio
I have tried these codes :
<%= cl_video_tag #post.audio.url, controls: true, style: "width: 100%;" %>
and
<%= cloudinary_url #post.audio.url, :resource_type=>"video %>
and
<audio controls="true" src="<%= cloudinary_url #post.audio.url, :resource_type=>"video" %>" style="width: 100%"></audio>
Thanks you for your help..
This is my post model
has_attached_file :audio, :storage => :cloudinary,
:cloudinary_credentials => Rails.root.join("config/cloudinary.yml"),
:path => ':id/:style/:filename',
:cloudinary_resource_type => :video, :processors => [:transcoder]
And in my post form
<%= f.file_field :audio %>

Related

Rails image upload won't display

add_column :issues, :data, :binary, :limit => 1.megabyte
get '/issues/:id/show_image' => 'issues#show_image', as: 'show_image_issue'
def show_image
#issue = Issue.find(params[:id])
send_data #issue.data, :type => 'image/png', :disposition => 'inline'
end
<%= f.file_field :data, as: :file %>
<%= image_tag show_image_issue_path(#issue) %>
The page loads and no errors, but there's no image.
I recommend for you to use PaperClip or CarrierWave for storing images, storing binary data has many disadvantages.
PaperClip: https://github.com/thoughtbot/paperclip
CarrierWave: https://github.com/carrierwaveuploader/carrierwave
why? read here : Storing Images in DB - Yea or Nay?
if you insist to do this then you can try:-
<img src="<%= #issue.data %>" />

Rails sending multiple params through dropdown box

I have two dropdown boxes. One is for campaign size(small, medium, large) and the other one is for the theme selection.
I want to be able to send params dynamically through onchange event handler
current code looks like this
<div class="col-lg-12 col-padding-helper">
<%= form_tag product_builder_path, method: :get do %>
<div class="col-lg-5 col-padding-helper">
<%= label_tag 'Campaign Size' %></br>
<%= select_tag 'type', options_for_select(['Small (4 Products)', 'Medium (7 Products)', 'Large (10 Products)' ]), multiple: false, :include_blank => true, class: "form-control", data: { remote: true }, onchange: "this.form.submit();" %>
</div>
<% end %>
<%= form_tag(product_builder_path, method: :get, remote: true) do %>
<div class="col-lg-7 col-padding-helper">
<%= label_tag 'Theme' %></br>
<%= select_tag 'theme', options_for_select(['Default BWX Theme', 'Black Friday Theme']), multiple: false, :include_blank => true, class: "form-control", onchange: "this.form.submit();" %>
</div>
<% end %>
</div>
The workflow that I am thinking in my head is that once user selects the campaign size it will submit a form through ajax and have something like this as a url. which then I can display display right type of html template on the webpage.
/product_builder?type=small
and after when they select the theme is it possible to do something like this? and this will change the theme of the type that I have chosen above
/product_builder?type=small&theme=black
so basically it's adding params to current URL
I don't even know if this is possible, any help or guidance would be awesome
Thank you

simple-form not looking up i18n translations

simple-form is simply not looking up i18n translations. I was trying it on the placeholders.
simple_form.en.yml
en:
simple_form:
placeholders:
company:
name: "Company name"
The form:
<%= simple_form_for #company,
url: company_path(#company) do |f| %>
<div class="form-group">
<%= f.text_field :name, class: "form-control" %>
</div>
I've debugged using i18n-debug, and it didn't seem to attempt to look up
en.simple_form.placeholders.company.name
at all. Other non simple-form i18n lookups are triggering just fine.
Did I miss a step to 'turn on' i18n for simple-form? I thought it works right out of the box.
Looks like you are using text_field - a Rails form helper. Simple form supports it but it is not implemented in simple form (i.e. falls back to Rails). Change it to f.input :name and it should work.

Devise with multiple models, bootstrap modal with ajax login

I have 2 kinds of users - nurses and patients, and want to setup login such that there are two buttons on the welcome/landing page which open up a bootstrap modal with the login form for each type of user. I'd like the login form to send an AJAX request so that if there are errors, they are displayed in the modal itself.
I'm using Devise for authentication, and have setup 2 models for nurses and patients. Initially, I setup the modal to load the url for the new_nurse_session_path on clicking the button, modified the default devise login form to send AJAX requests, used a custom SessionsController to handle new sessions and send JSON replies back, and then have JS code which catches the reply. This worked (though with some issues as below) was pretty slow since it was loading the entire page from nurses/sessions/new.html.erb along with the header, navbar, etc.
Questions
While the JS code was catching the AJAX request when the entire sign in page was opened in the modal (as per code below), if I opened the page directly, the AJAX request wasn't processed even though the server was sending the correct JSON back - is there something I'm doing wrong here that might be causing this problem?
To avoid the entire new session page from being loaded in the modal, I followed the instructions here https://github.com/plataformatec/devise/wiki/How-To:-Display-a-custom-sign_in-form-anywhere-in-your-app to put the form in the modal. The issue I'm facing here is how to create the helper so that it defines the resource and Devise mappings according to whether it's a nurse or patient trying to login. If I create separate custom forms and modals for each one, I face the same issue with handling AJAX requests as in Q1. Would really appreciate help on what I might be doing wrong here.
If I'm going down a rabbit hole here, would really appreciate suggestions on implementing the above functionality in a better way! Thanks!
Code used
Login form, nurses/sessions/new.html.erb:
<%= form_for(resource, :as => resource_name, :url => session_path(resource_name),
:html => {:id => "sign_in_nurse"}, :format => :json,
:remote => true) do |f| %>
<div><%= f.label :email %><br />
<%= f.email_field :email, :autofocus => true %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<% if devise_mapping.rememberable? -%>
<div><%= f.check_box :remember_me %> <%= f.label :remember_me %></div>
<% end -%>
<div><%= f.submit "Sign in" %></div>
<% end %>
<%= render "nurses/shared/links" %>
Coffeescript to handle AJAX reply, nurses.js.coffee:
$("form#sign_in_nurse").bind "ajax:success", (e, data, status, xhr) ->
if data.success
window.location.replace(data.redirect)
else
alert('failure!')
SessionsController:
class SessionsController < Devise::SessionsController
def create
resource = warden.authenticate!(:scope => resource_name, :recall => "sessions#failure")
return sign_in_and_redirect(resource_name, resource)
end
def sign_in_and_redirect(resource_or_scope, resource=nil)
scope = Devise::Mapping.find_scope!(resource_or_scope)
resource ||= resource_or_scope
sign_in(scope, resource) unless warden.user(scope) == resource
return render :json => {:success => true, :redirect => stored_location_for(scope) || after_sign_in_path_for(resource)}
end
def failure
return render :json => {:success => false, :errors => ["Login failed."]}
end
end
Code to generate modal:
<li><%= link_to image_tag('i_am_nurse.png', :size => "130x130"),
new_nurse_session_path , :data => { :target => "#ajax-modal", :toggle => "modal"} %></li>
Bootstrap modal div:
<div id="ajax-modal" class="modal hide fade" tabindex="-1">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<div class="modal-body">
<div class="modal-body-content"></div>
<div class="ajax-loader"></div>
</div>
<div class='modal-footer'>
<button type="button" data-dismiss="modal" class="btn">Close</button>
</div>
</div>​

form_tag not rendering in rails 3.2

I am following the tutorial at http://www.noupe.com/ajax/create-a-simple-twitter-app.html
I am using ruby version 1.9.3p194 , rails version 3.2
When I created the view file index.html.erb , and put in the contents as
<b><%= render :partial => "message_form" %></b>
<%= render :partial => #posts %>
and then in the form partial , _message_form.html.erb
In partial _message_form
<% form_tag(:controller => "posts", :action => "create", :method=>"post") do %>
<%= label_tag(:message, "What are you doing?") %><br/>
<%= text_area_tag(:message, nil, :size => "44x6") %><br/>
<%= submit_tag("Update") %>
<% end %>
and load the index.html.erb , I only see the message "In partial _message_form" . the form tags do not render at all.
Need help. Would be glad to provide any additional required information
As far as I remember you need this: <%= form_tag...
UPDATE: There was a fallback to previous behavior (without =), but it doesn't seem to exist in Rails 3.2.6

Resources