I'm trying to modify railslist to work with Rails 3. I keep getting this error when trying to view /classifieds
TypeError (can't convert nil into
Array)
app/controller/classifieds_controller.rb:in
'index'
def index
#classifieds = Classified.paginate :page => params[:page], :order => "created_at DESC"
render :layout => 'main'
end
You need to use a pre-release version of will_paginate if you're using Rails 3. Put the following in your Gemfile in place of what you had for will_paginate before:
gem "will_paginate", "~> 3.0.pre2"
Related
I am able to upload image files as attachments using Attachment_fu but when I try to edit/ modify images which were already uploaded, it does not work.
In my Controller I have this:
def update
#sponsor_account = SponsorAccount.find( params[:id] )
if params[:showcase_category_image_file] &&
!params[:showcase_category_image_file].blank?
#sponsor_account.showcase_category_image =
ShowcaseCategoryImage.new(:uploaded_data =>
params[:showcase_category_image_file])
***This logs - Now the file name is: ***
Rails.logger.info("Now the file name is: #
{#sponsor_account.showcase_category_image.filename}")
end
#sponsor_account.update_attributes( params[:sponsor_account] )
if #sponsor_account.save
flash[:notice] = "Showcase category #{#sponsor_account.name} was updated!"
else
flash[:errors] = #sponsor_account.errors
end
redirect_to sponsor_accounts_path
end
ShowcaseCategoryImage is defined as follows:
has_attachment :content_type => :image,
:storage => :file_system,
:max_size => 5.megabytes,
:thumbnails => { :large => [350, 100], :medium => [200, 90], :thumb =>
[35,35], :preview => [60,60] }
validates_as_attachment
The view has a file_field_tag as follows:
<%= file_field_tag :showcase_category_image_file%>
and my SponsorAccount model says:
has_one :showcase_category_image, :as => :owner, :dependent => :destroy
validates_presence_of :showcase_category_image, :on => :create
Almost similar code works perfectly ok in 'create' but here in 'update' action where there is already a value, this code is not working.
I am getting the below error msgs:
Completed 500 Internal Server Error in 1089ms
ActionView::Template::Error (undefined method `public_filename' for nil:NilClass):
Obviously this error is in the index action where it tries to list all records and their attachments. Since this attachment is empty after the update, this error is thrown in the redirect_to part.
I am using REE1.8.7 and rails 3.2.9
Please help!
This issue was solved when I added :multipart => true in the 'View'. I am using rails 3.2.9 and the rails api has this to say about the 'file_field_tag':
file_field_tag(name, options = {}) Link
Creates a file upload field. If you are using file uploads then you will also need to set the multipart option for the form tag:
I get this error:
There has been an unexpected error with the application. Please contact the administrator. error code: success
I am using recaptcha with devise, I edited where appropriate as stated by following the steps mentioned on this page
Here is my Gemfile:
source 'https://rubygems.org'
gem 'rails', '3.2.8'
gem 'json'
gem 'execjs'
gem 'therubyracer'
gem 'will_paginate', "3.0.pre4"
group :production do
gem 'pg'
end
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
gem 'jquery-fileupload-rails'
gem "letter_opener"
gem 'jquery-ui-rails'
end
gem 'jquery-rails'
gem "carrierwave"
gem "fastercsv"
gem 'devise', '2.1.2'
gem 'rmagick'
gem 'mailman', :require=> false
gem 'activeadmin'
gem 'kaminari'
gem 'prawn'
gem 'recaptcha', :require => 'recaptcha/rails'
I hope you have followed it exactly what it says on the documentation For instance, to support recaptcha on ruby 1.8.X and 1.9.X, there are different line of code. However, after a short research I found out this website you can use recaptcha without the plugins regardless of your rails version. I hope this helps. :)
From Recaptcha Site (almost).... I'm in Rails 4 with Devise (aparently the hard bundle).. No, is too easy. Procedure apply to any ruby, any rails, and devise (or without devise). Tested in ruby/rails/devise 1.9.3/3.2.x/2.2.1 & 2.0.x/4.0.x/3.2.4 and last one without Devise.
Edit next files:
Gemfile
gem 'ruby-recaptcha'
# bundle install
config/initializers/credentials.rb (any file in this path it's ok)
# Set constants
RCC_PUB = "your_public_key"
RCC_PRIV= "your_private_key"
# re-start server. Try to keep safety this data using ENV vars.
app/views/devise/registrations/new.html.erb (or form you prefer)
# form
<%= simple_form_for(resource, as: resource_name, :url => registration_path(resource_name) do |f| %>
<%= f.input :email, autofocus: true, required: true %>
...
<!-- Add next line -->
<div id="captchadiv"></div>
<%= f.button :submit, "Register", class: 'btn btn-danger' %>
<% end %
At the end of same file (can be in awesome_funcs.js.erb file)
<script type="text/javascript" charset="utf-8">
$(function () {
function showRecaptcha() {
Recaptcha.create("<%= RCC_PUB %>", "captchadiv", {
theme: "white",
lang: 'en'
});
};
showRecaptcha();
});
</script>
And the little trick, app/controllers/application_controller.rb
# On top
include ReCaptcha::AppHelper
before_action :configure_permitted_parameters, if: :devise_controller?
# If already you have this method or any other with before_filter
# Into method
protected
def configure_permitted_parameters
if controller_name == "registrations" && action_name == "create"
# Rails 3: If you prefer, put this into registrations#create method
unless validate_recap(params, User.new.errors) # Or your model
# wrong input is handled here
flash[:error] = "Text from images doesn't match with your input"
redirect_to :back # or other stuff
# As my submit is with Ajax, I do a render instead redirect:
# render :json => {other: "recaptcha_failed"}
# In success callback I evaluate response and Recaptcha.reload() paints a new recaptcha.
end
end
....
# trick: do everything before params sanitizers
end
As we are using Ajax plugin, finally, add in
app/views/layouts/application.html.erb
(just before end of body)
<body>
...
...
<script type="text/javascript" src="http://www.google.com/recaptcha/api/js/recaptcha_ajax.js"></script>
</body>
That's it. You have recaptcha
Advice:
If you use Ajax to do submit, you can use success callback for a Recaptcha.reload() in case images and input doesn't match.
Other, if your form is "remote: true", you can use above behavior after "create" method in create.js.erb file in your views directory.
IMPORTANT: In some projects when form arrives from a link Recaptcha doesn't appear. Try to locate the launcher link into a div or in a parent div add "data-no-turbolink". I had this issue in Rails 4.
I have a static set of help pages that I serve in a Rails 3.2 application using Thoughbots high-voltage gem. I'm just using this in a 'vanilla' way, without serving up the html pages via the controller.
Background
I had originally tried to do this myself adapting Michael Hartl's tutorial around static pages - i.e I have a set of static pages with their own controller, and I was trying to create a sub-directory under the static pages view, but could not get my routing to work, so Google searches revealed Thoughbots High-Voltage gem.
Aspiration
What I would like is a recommendation of what gem or method is best to generated PDF files using Thoughtbots High-Voltage gem.
Has anybody done this?
I want to be able to host this on heroku so if there are any gotacha's I'd like to know about these up front.
My current implementation is a basic Rails 3.2 application with the High-Voltage gem installed and a number of views under the pages sub-directory.
pages/help/users
pages/help/products
pages/help/orders
I have images within my html pages, not sure if this causes complications.
EDIT: Added controller based on answer provided as still having issues with wicked_pdf on Rails 3.2.3, ruby 1.9.3-p125 on Lion
class PagesController < HighVoltage::PagesController
def show
respond_to do |format|
format.html do
super
end
format.pdf do
#render :pdf => "pdf_file" # wicked_pdf syntax here
render :pdf => :id,
:layout => 'application',
#:template => 'help/products/product_tolerance.html.erb',
:template => 'pages/#{:id}.html.erb',
:show_as_html => params[:debug],
:footer => {
:left => "Generated on #now",
:centre => "Centre",
:right => "Page # of page(s)"
}
end
end
end
end
Routes file contains:
match "/pages/*id" => 'pages#show', :as => :page, :via => :get, :format => false
Should the :format be true? In the controller and in high_voltage?
Override the High Voltage pages controller as described here:
https://github.com/thoughtbot/high_voltage#override
Then install either pdfkit or wicked_pdf (html to pdf converters)
and hook them into that controller to create PDF versions:
class PagesController < HighVoltage::PagesController
def show
respond_to do |format|
format.html do
super
end
format.pdf do
render :pdf => "pdf_file" # wicked_pdf syntax here
end
end
end
end
in a current Rails 3.0.9 app of mine I had a few .js.erb templates that were using view_context in them so I could call fields_for on it during a ajax request. This was letting me build some nested attribute form fields via ajax. But upon upgrading to Rails 3.1 I'm getting the follow error:
ActionView::Template::Error (undefined local variable or method `view_context' for #<#:0x1057b9f70>):
Was this removed/deprecated recently? Is there another way I can build nested fields_for inputs without having the parent FormBuilder handy? It seems view_context is still available in the controller, but I was hoping to keep this markup generation in the View layer.
My .js.erb template looked like this
<% meal_item_fields = view_context.fields_for :meal_items, Meal.new.meal_items.new, :child_index => "new_meal_items" do |f|
render :partial => 'meal_items/meal_item_fields', :locals => {:meal_item_form => f}
end
%>
$("#meal-items").append("<%= escape_javascript(meal_item_fields) %>");
According to api docs it is deprecated in >= 3. Source of 3.0.9 returned self for view_context. I think if you were to try without view_context it would just work.
<% meal_item_fields = fields_for :meal_items, Meal.new.meal_items.new, :child_index => "new_meal_items" do |f|
render :partial => 'meal_items/meal_item_fields', :locals => {:meal_item_form => f}
end %>
$("#meal-items").append("<%= escape_javascript(meal_item_fields) %>");
You might want to add helper_method :view_context in your controller.
I followed the installation procedure of geokit-rails3, here is my conf :
Using rails (3.0.4)
Using activerecord (3.0.4)
Using geokit (1.5.0)
Using geokit-rails (1.1.4)
I get this error "undefined method `within' for #" when i try to query with the "within" method. (note that i am new to rails and maybe missing something obvious)
Here is my class definition :
class Snip < ActiveRecord::Base
belongs_to :user
acts_as_mappable :default_units => :kms,
:default_formula => :sphere,
:distance_field_name => :distance,
:lat_column_name => :latitude,
:lng_column_name => :longitude
end
In my controller i have :
#userLocation = GeoKit::LatLng.new(params[:lat],params[:lng])
#snips = Snip.within(params[:distance], :origin => #userLocation)
Here is what i added to my gemfile :
gem 'geokit', '>= 1.5.0'
gem 'geokit-rails', '1.1.4'
Do you have any idea why i get this error ?
Thanks in advance,
Vincent.
Your gemfile asks for geokit-rails, that's the Rails 2 version. You need to replace that with geokit-rails3