panda error - file not getting uploaded - ruby-on-rails-3

I am trying to embed a video in my app and am using the gem panda '1.6.0' for this. I followed the pandastream integrate with rails guide and have the following code:
controllers:
-------------
class BoothVideosController < ApplicationController
before_filter :init_booth_video
def show
#original_video = #video.panda_video
#h264_encoding = #original_video.encodings["h264"]
render :template => 'booth_video/booth_video', :layout =>
'sponsored_group_manage_sub_menu'
end
def new
#video = BoothVideo.new
render :layout => 'sponsored_group_manage_sub_menu'
end
def create
#video = BoothVideo.create!(params[:video])
redirect_to :action => :show, :id => #video.id
end
def init_booth_video
#group = Group.find(params[:group_id])
#video=#group.booth_video
end
end
class PandaController < ApplicationController
def authorize_upload
payload = JSON.parse(params['payload'])
upload = Panda.post('/videos/upload.json', {
file_name: payload['filename'],
file_size: payload['filesize'],
profiles: "h264",
})
render :json => {:upload_url => upload['location']}
end
end
model:
------
class BoothVideo < ActiveRecord::Base
belongs_to :group
validates_presence_of :panda_video_id
def panda_video
#panda_video ||= Panda::Video.find(panda_video_id)
end
end
View
-----
<% content_for(:page_title, "Create a New Booth Video") -%>
<% form_for(:booth_video,:url => group_booth_video_path, :html => {:id =>
"new_booth_video", :multipart => true}) do |f| %>
<h5>New Booth Video</h5>
<fieldset>
<input type="hidden" name="panda_video_id"/>
<label for="title">Title</label><br />
<%= f.text_field :title, :size => 32, :placeholder => "Title for the new booth
video" %><br /><br />
<div class='progress'>
<span id="progress-bar" class='bar'></span>
</div>
<div id="browse">Choose file</div>
<%=f.submit "Upload video" %>
<% end %>
I have also included the js for panda uploader in my application.js file.
I am able to see my file uploader but when I actually browse and select a file, it does not actually get loaded - also no progress is indicated in the progress bar.
Why is the video not getting uploaded?
The create action mentions params[:video] in the tutorial but there is no param in the
'new' form for the uploaded video-how will it recognize this?
Please help! Thanks in advance...

Related

Show validation errors in view for custom Devise login form

At the moment, I'm building a Rails platform using Spree Commerce where I need two Devise login forms.
The default Devise login forms are already implemented. For the second (custom) Devise form, I created the following view:
global_login.html.erb
<div class="inner">
<h2 class="large-margin">
Login
</h2>
<%= form_for(Spree::User.new, :url => spree_login_path) do |f| %>
<%= f.error_messages %>
<%= f.email_field :email, {:value => params[:email], :placeholder => 'Email address', :class => 'input'} %>
<%= f.password_field :password, {:placeholder => 'Password', :class => 'input'} %>
<%= f.submit 'Login', :class => 'submit' %>
<% end %>
</div>
This view does display a Devise login form. However, every time I submit the form, I'm being redirected to the default Devise view where the validation errors are shown.
Does somebody know a solution to this problem?
Cheers!
I found a workaround for this issue. I added a param to one of the forms named "inside_cart". When the request is send to the #create method, I check if the param is present.
I overwrote the Spree::UserSessionsController#create method as follows:
Spree::UserSessionsController.class_eval do
layout false
def create
authenticate_spree_user!
if spree_user_signed_in?
respond_to do |format|
format.html {
flash[:success] = Spree.t(:logged_in_succesfully)
redirect_back_or_default(after_sign_in_path_for(spree_current_user))
}
format.js {
user = resource.record
render :json => {:ship_address => user.ship_address, :bill_address => user.bill_address}.to_json
}
end
else
if params[:spree_user] && params[:spree_user][:inside_cart]
flash.now[:error] = t('devise.failure.invalid')
render :new, layout: false
else
flash.now[:error] = t('devise.failure.invalid')
render 'spree/users/global_login', layout: 'spree/layouts/spree_application'
end
end
end
end

Do I need to reload the comment object or the partial that contains the comment in case of using Ajax in Rails 3?

I have been trying to figure out adding comments without reloading the page using Ajax, after reading few different tutorials this is what I came up to so far, and it's not working:
inside user_comments/_comments.html.erb
<div id="comment_form">
<%= simple_form_for [#commentable, #comment], :html => { :multipart => true }, :remote => true do |f| %>
<div class="picture"><%= image_tag current_user.avatar.url(:thumb) %></div>
<%= f.input :content, label: false, :placeholder => "Add Comment", :input_html => { :rows => 4 } %>
<%= f.submit "Add Comment" %>
<% end %>
</div>
Inside the controller:
def create
#users = User.all
#comment = #commentable.user_comments.new(params[:user_comment])
#comment.user_id = current_user[:id]
##commentable.user_comments.create(:user_id => current_user[:id])
if #comment.save
flash[:notice] = "Successfully created comment."
respond_to do |format|
format.html { redirect_to #commentable }
format.js
#format.js #{ render 'create.js.erb' }
end
else
render :new
end
end
and inside the create.js.erb
// Display a Javascript alert
<% if remotipart_submitted? %>
$("#comments_list").append("<%= escape_javascript(render(:partial => 'user_comments/comments')) %>");
<% end %>
I'm using a Gem called: remotipart
I don't know what I'm missing in the process.
in the console I get:
POST http://localhost:3000/assignments/2/user_comments
200 OK
134ms
which means the post goes through, but the comment doesnt get added back to the partial.
Ok after 2 days! I fixed it, here is what I can share and might help:
1- make sure to include the :remote => true to the form that is about to be submitted
2- Check the controller and see what the Create action is being redirected, in my case I changed to this:
def create
#users = User.all
#comment = #commentable.user_comments.new(params[:user_comment])
#comment.user_id = current_user[:id]
##commentable.user_comments.create(:user_id => current_user[:id])
if #comment.save
flash[:notice] = "Successfully created comment."
respond_to do |format|
format.html
format.js {#comments = #commentable.user_comments}
end
else
render :new
end
end
Then make sure the create.js.erb is written properly:
$("#comments_list").empty()
$("#comments_list").append("<%= escape_javascript(render(:partial => 'comments')) %>");
there you go! I hope some creates a proper tutorial for newbies like me :)!

Uploading Images using paperclip in rails 3

I am trying to upload an image in my Rails Application. However, I am unable to do so. I am relatively new to rails and I need some help here.
I have a "Picture" Model shown here -
class Picture < ActiveRecord::Base
attr_accessible :photo, :tag
has_attached_file :photo, :styles => { :medium => "300x300>",
:large => "1000x1000>", :thumb => "100x100>" }
end
I have a "Pictures" Controller shown here -
class PicturesController < ApplicationController
def new
#picture = Picture.new
end
def create
#picture = Picture.create( params[:picture] )
if #picture.save
flash.now[:success] = "Image Uploaded"
redirect_to #picture
else
render 'new'
end
end
def index
end
def show
#picture = Picture.find(params[:id])
send_data #picture.photo, :type => 'image/png', :disposition => 'inline'
end
def imageshow
end
end
Lastly, These are my "new" and "show" views:
"new.html.erb"
<h1>Upload an Image</h1>
<div>
<%= form_for(#picture, :html => {:multipart => true}) do |f| %>
<%= f.file_field :photo %>
<%= f.label :tag %>
<%= f.text_field :tag %>
<%= f.submit "Submit", class: "btn btn-large btn-primary" %>
<% end %>
</div>
"show.html.erb"
<h1>Displaying Uploaded Image</h1>
<div>
<%= image_tag #picture.photo.url %>
<%= image_tag #picture.photo.url(:large) %>
</div>
I am able to reach the Upload page (ie, new.html.erb in Pictures Controller). However, when I upload an image I get the following error:
"This image "http://10.102.119.20:3000/pictures/12" cannot be displayed because it contains errors"
My queries are:
Where does an image get uploaded on the server?
Is there any configuration that needs to be done?
Is there any other issue with my code?
Thanks in advance for any help..!!
Ryan Bates discusses Paperclip in his Railscasts. Check it out. It helped me get started.

Rails autocomplete and acts-as-taggable-on

I hope for help to follow problem.
I have install gem rails3-jquery-autocomplete from here
and gem "acts-as-taggable-on" from here
Here is my history:
my model is
class Article <ActiveRecord::Base
acts_as_taggable_on :tags
attr_accessible :title, :body, :tag_list
my controller is
class ArticlesController < ApplicationController
autocomplete :tag, :name, :class_name => 'ActAsTaggableOn::Tag'
def tag_cloud
#tags ||=Article.tag_counts_on(:tags)
end
def all
#tags = Article.tag_counts_on(:tags).limit(8).order('count desc')
klass = Article
klass = klass.tagged_with(#tag) if (#tag = params[:tag]).present?
#articles = klass.where(:state => '4').paginate(:page => params[:page])
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #articles }
end
end
my route is
resources :articles do
collection do
get 'autocomplete_tag_name'
get 'about'
get 'all'
get 'myarticles'
delete 'destroy'
end
end
my _form.html.erb
<div class="field">
<label for="tag_list">Κατηγορία (Μπορείτε να προσθέσετε διαφορετικές κατηγορίες διαχωρίζοντάς τες με κόμμα)</label><br />
<%= f.autocomplete_field :tag_list, autocomplete_tag_name_articles_path, :"data-delimiter" => ', ' %>
</div>
my index.html.erb
<div class='tag-box'>
<% #tags.sort_by(&:count).reverse.each do |k| %>
<% url_opts = {:action => "all", :controller => "articles"}
link_name = "#{k.name} (#{k.count})" %>
<% if #tag == k.name %>
<%= link_to link_name, url_opts.merge(:tag => nil), :class => "tag current_tag", :title => "Κλικ για εμφάνιση όλων" %>
<% else %>
<%= link_to link_name, url_opts.merge(:tag => k.name), :class => "tag", :title => "Κλικ για εμφάνιση άρθρων στην κατηγορία #{k.name}" %>
<% end %>
<% end %>
</div>
my application.html.erb
<%= stylesheet_link_tag :all %>
<%= javascript_include_tag :defaults %>
<%= javascript_include_tag 'jquery-1.7.2.min.js', 'jquery-ui-1.8.19.custom.min.js', 'autocomplete-rails.js', 'rails.js' %>
<%= stylesheet_link_tag 'jquery-ui-1.8.19.custom.css' %>
and last my Gemfile
gem 'rails', '3.0.11'
gem 'pg'
gem 'jquery-rails', '>= 1.0.12'
gem 'rails3-jquery-autocomplete'
gem 'acts-as-taggable-on', '~> 2.2.2'
in app works the tagging system but don't work the autocomplete.
perhaps my problem is on file _form.html.erb. I have on file new.html.erb the code
<section id="myarticles">
<h2>Νέο άρθρο</h2>
<%= render 'form' %>
</section>
Is about render and autocomplete together and autocomplete don't work?
Finaly the solution for my problem that can help someone with the same situation is simple.
Copy the file jquery-ui-1.8.19.custom.css from public/stylesheets/ui-lightness or from public/stylesheets/smoothness depends from the download theme from jQuery UI-Autocomplete
and paste to public/stylesheets/ and automagicaly disappeared the Routing Error and voila the Autocomplete functioning.
I had follow the instructions from here
Copy the files on the css folder to the public/stylesheets folder on your app. Note that these files may be one level down from the css folder, in a folder called "ui-lightness".
but the right position is on directory public/stylesheets.
Also remove from mycontroller the follow code :class_name => 'ActAsTaggableOn::Tag' and now the code looks like
class ArticlesController < ApplicationController
autocomplete :tag, :name

Rails: devise override create method and rendering errors

I'm new to rails, and developing first application gets hard because you know things, but you don't know how to do them.
I set my registration form in a box which comes out like a popup using "fancy-box" jquery.
When some user fails to sign up, I want the page to remain in the box with validation errors,
and when he signs up correctly I want to redirect him in the home page, but I can't do this:
My controller which overrides devise methods:
def setup
#society = Society.new
end
# GET /resource/sign_up
def new
super
end
# POST /resource
def create
build_resource
if resource.save
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_navigational_format?
sign_in(resource_name, resource)
respond_with resource, :location => after_sign_up_path_for(resource)
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
expire_session_data_after_sign_in!
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
render action: new
end
end
protected
def after_sign_up_path_for(resource)
'/vetrina/index' #my homepage
end
and the form:
<%= simple_form_for #society, :html => { :class => 'form-horizontal' } do |f| %>
<fieldset>
#all the inputs are here
<div class="form-actions">
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to 'Cancel', "#", :class => 'btn' %>
</div>
</fieldset>
Any help?