I used 'uuidtools' gem in my controller this way:
def create
require 'uuidtools'
game = Game.new
game.permalink = Base64.encode64(UUIDTools::UUID.random_create)[0..8]
game.save
redirect_to :controller => 'home', :action => 'index'
end
I get this error about the requiring of 'uuidtools':
no such file to load -- uuidtools
(I added the gem to my gem file.)
How can I fix this?
Thanks,
Oded
Perhaps restarting the server would also had fixed the problem
Solved it.
What I did is to migrate the use of 'uuidtools' from the controller to the model:
class Game < ActiveRecord::Base
before_save :create_permalink
def create_permalink
self.permalink = Base64.encode64(UUIDTools::UUID.random_create)[0..8]
end
end
Did you run 'bundle install' to install the gem ?
Related
I'm writing a rails application with which a user can upload images. I am deploying with Heroku, and using Carrierwave and S3 to upload and store images. I have followed this heroku guide step-by-step...unfortunately I am still getting an error "undefined method `presigned_post'", and do not know how to resolve it. It seems the S3_BUCKET is not being recognized as an aws object...
Has anyone come across this problem and figured it out? Here's some code for reference:
Pictures controller:
class PicturesController < ApplicationController
before_action :set_s3_direct_post, only: [:new, :create]
def index
#pictures = Picture.all
end
def new
#pictures = Picture.all
#picture = Picture.new
end
def create
#picture = Picture.new(picture_params)
if #picture.save
redirect_to new_picture_path, notice: "You just uploaded a picture!"
else
render "new"
end
end
...
def picture_params
params.require(:picture).permit(:attachment)
end
private
def set_s3_direct_post
#s3_direct_post = S3_BUCKET.presigned_post(key: "uploads/#{SecureRandom.uuid}/${filename}", success_action_status: '201', acl: 'public-read')
end
end
New picture view:
<h1>Upload a new picture</h1>
<br>
<div class="well">
<%= form_for #picture, html: { class: 'directUpload', data: { 'form-data' => (#s3_direct_post.fields), 'url' => #s3_direct_post.url, 'host' => URI.parse(#s3_direct_post.url).host } } do |f| %>
<%= f.file_field :attachment %>
<%= f.submit "Upload", class: "btn btn-default" %>
<% end %>
</div>
And config/environment.rb:
require File.expand_path('../application', __FILE__)
# Initialize the Rails application.
Rails.application.initialize!
# S3
S3_BUCKET='fotoes'
AWS_ACCESS_KEY_ID='secretxxxxxxxx'
AWS_SECRET_ACCESS_KEY='xxxxxxxsecretxxxxxx'
Any thoughts?
#Jillian I suppose your expectation was that S3_BUCKET class should call presigned_post method which is supposed to be defined. However, it seems its not. I took a look at the heroku page containing the tutorial you followed and well you followed every instruction. I suggest you contact heroku about the documentation. However, I would keep looking into it
Thank you all for your help. In the end I found a different walk-through that was much simpler and effective. (The Heroku one was a little complicated and left a lot to go wrong - go figure.)
This solved everything :)
edit:
not everything - had to complete one last step before I got the site running. Run this line in the terminal: $ heroku config:add AWS_ACCESS_KEY=value
and $ heroku config:add AWS_SECRET_KEY=value, where the value is your S3 credentials for each, respectively.
This was the only combo of fog, carrierwave, rails, fog-aws gems that worked (after weeks of fussing with them):
gem 'rails', '4.1.0'
gem 'carrierwave', '~> 0.10.0'
gem 'fog', '1.34.0'
gem 'fog-aws', '0.7.6'
i resolved this by updating my version of aws-sdk
$ bundle update aws-sdk
I took the approach of Railscast episode 153 revised.
My controller is
class AdminsController < ApplicationController
def index
#examples = Example.all
respond_to do |format|
format.html
format.csv { send_data #examples.to_csv }
format.xls { send_data #examples.to_csv }
format.pdf do
pdf = DownloadPdf.new(#examples)
send_data pdf.render, filename: 'generate_table.pdf',
type: 'application/pdf', disposition: "inline"
end
end
end
end
and my download_pdf.rb file is
class DownloadPdf < Prawn::Document#make_table
require 'prawn/table'
def initialize(example)
super()
#examples = example
line_items
end
def line_items
image "#{Rails.root}/app/assets/images/logo.png"
table [[1,2],[3,4]]
end
end
I am using gems
gem 'prawn', :git => "https://github.com/prawnpdf/prawn.git", :ref => '8028ca0cd2'
gem 'prawn-table', '~> 0.1.0'
Thanx in advance for helping.
TL;DR: Update prawn gem by adding this to your Gemfile: gem 'prawn' and running bundle install.
Longer answer: You are using an old version of Prawn - that ref you are using in your Gemfile refers to somewhere in 2013. prawn-table 0.1 is newer and requires a newer version of prawn. More precisely, it's using Prawn's ::FLOAT_PRECISION constant, which was added in this 2014's commit to Prawn.
Please use below on Gemfile
gem 'prawn'
and then remove Gemfile.lock
and then
bundle install
restart server
you can use round(2) like this..
val= 456.7890999999
val.round(2)
and Ans will be 456.79
EDIT: Solved. Solution:
Include
gem 'rabl' and gem 'oj' in your gemfile along with gem rabl-rails
For some reason, my instance variable isn't being passed into the view (I'm using Rabl).
Here's the relevant code:
articles_controller.rb
class ArticlesController < ApplicationController
respond_to :json, :xml
def index
#articles = Article.original.last(100)
end
def after
#articles = Article.where("id > #{params[:id]}")
end
def show
#article = Article.find(params[:id])
end
end
show.json.rabl
object #article
attributes :id, :headline, :source, :link
attributes :similar_articles => :similar
The error:
RuntimeError in Articles#show
Showing /Users/chintanparikh/Dropbox/Projects/Current/article_aggregator/app/views/articles/show.json.rabl where line #2 raised:
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
Any ideas?
The ID you're using to look up the record does not match one within your database.
Check what params[:id] is evaluating to and ensure that you can locate a record using that value.
Found the solution, you need these three lines in your Gemfile:
gem 'rabl-rails'
gem 'rabl'
gem 'oj'
Before, I only had rabl-rails, and I assume that was causing the problem.
I'm having bother with delayed_job using the active_record fork (link).
In the controller:
guide = Rightsguide.new
guide.run(#works, current_user)
in the Rightsguide ruby class:
require 'delayed_job'
require 'delayed/tasks'
require 'prawn'
require 'open-uri'
class Runrightsguide
def run(works, current_user)
pdf = Rightsguidereport.new(works, current_user)
filename = "#{Rails.root}/public/#{Date.today}_rightsguide.pdf"
pdf.render_file(filename)
pdf_file = File.open(filename)
archive = RightsguideArchive.new(:user_id => current_user)
archive.pdf = pdf_file
archive.save!
User.find(current_user).notice "<a href='/rightsguide_archives' target='_blank'>View Rights Guide</a>", :level => :notice, :sticky => true, :title => "AIs generated."
end
end
The above works fine, but when I use one of the delayed_job calls such as handle_asynchronously :run after the run method I get wrong number of arguments (2 for 1).
Hmm. Turns out the #works argument was the problem. It's an ActiveRecord relation. Delayed_job didn't like it. Turning the relation into an array of IDs did the job.
I am trying to override a model of forem gem so that I could use thumbs_up gem for voting purpose.
I did a rails g model Post and trying to inherit the post model of forem by this line of code
class Post < Forem::Post
acts_as_voteable
end
same for the controller
class PostsController < Forem::Postscontroller
def vote_up
begin
current_user.vote_for(#post = Post.find(params[:id]))
render :nothing => true, :status => 200
rescue ActiveRecord::RecordInvalid
render :nothing => true, :status => 404
end
end
end
I keep getting this error
undefined method `vote_up_post_path'
in my route.rb
mount Forem::Engine, :at => "/forums"
resources :posts do
member do
post :vote_up
end
end
I guess I am doing something really stupid out here and I am not overriding the model correctly. I was using this Clarification on how to use "thumbs_up" voting gem with Rails 3 post to set up thumbs_up
Can someone help??
If I'm getting your question correctly, you wanna change the behavior of forem Post in order to support voting using acts_as_votable.
For that to work you need to re-open Forem::Post class in an initializer (e.g. config/initializers/forem.rb) and add to it acts_as_votable line like this:
module Forem
class Post
acts_as_votable
end
end
And the same for Forem::PostsController:
module Forem
class PostsController
def vote_up
begin
current_user.vote_for(#post = Post.find(params[:id]))
render :nothing => true, :status => 200
rescue ActiveRecord::RecordInvalid
render :nothing => true, :status => 404
end
end
end
end
It seems it was a stupid mistake, realized it while having discussion with patrickmcgraw.
forem hides your routes and and you have to mention main_app before the routes, so after writing
main_app.vote_up_post_path instead of vote_up_post_path the page was up again.
Hope it helps someone trying to use forem.