When using filepicker-rails, where should I define my avatar_url method within my rails app? - ruby-on-rails-3.1

I've installed the filepicker-rails gem and I am able to upload files to the filepicker.io server. When I try to display the image for different users profile, I get the error:
undefined method 'avatar_url' for nil:NilClass
Under my users directory, in the show.html.erb file I have:
<%= filepicker_image_tag #user.avatar_url, w: 160, h: 160, fit: 'clip' %>
I have the following under my User.rb file:
def avatar_url(user)
user.avatar_url
end
Any ideas why this doesn't work?

Basically you'll need a column in your DB called avatar_url. Write a migration that will give you a avatar_url column as just a regular string column. Since you're using Rails, ActiveRecord will provide the avatar_url method for you.
Edit: In your controller you're likely not looking up your user correctly which is resulting in a method call on a NilClass in your view.

Related

Unable to route in ruby on rails as I encounter specific error

I'm using rails 5.0 and I get the
PostsController#index is missing a template for this request format and variant. request.formats: ["text/html"] request.variant: []
NOTE! For XHR/Ajax or API requests while trying to input my url/posts.
my routing.rb file is below:
Rails.application.routes.draw do
get 'posts/index' => 'posts#index'
resources: posts
end
my posts_controller.rb looks like below:
class PostsController < ApplicationController
def index
end
end
I want to know specifically what I'm doing wrong. I've just started to learning Ruby on Rails.
Template is missing. You need to create a file ie index.html.erb under directory /app/views/posts/index.html
GET profiles/employees- Should display all the employees
GET profiles/employees/:id should display a specfic employee
GET profiles/donors - Should display all the donors
GET profiles/donors/:id - Should display a specfic donor
routes in ruby on rails behave in the same way as href tag in rails
we use get for link and if we want toi perform any action on button then w e have to use button.
if you have any doubt let me know in detail.
for more details refer this
http://guides.rubyonrails.org/routing.html
if we write only localhost:3000/employees then bydefault our application will redirect to index page.
The issue is template missing you are missing the template file index.html.erb
I have notice that on your snapshot you named the template file as index.html.erb.txt
remove the .txt and rename to index.html.erb Then it should work
Also you don't need to declare the routes as
get 'posts/index' => 'posts#index'
Just declare the resources: posts is enough. You may check the routes using the rake routes command on your rails console
Rename the file index.html.erb.txt to index.html.erb

ruby on rails undefined method `errors' for nil:NilClass

currently working on my project.and
i'm trying to create an existing article page but i keep getting this message
undefined method `errors' for nil: Nil Class. currently working on my project
Edit existing article
<% if#article.errors.any? %>
<h>The following errors prevented the article from getting created</h>
<ul>
<%#article.errors.full_messages.each do |msg| %
#article doesn't exist. It's probably expected that your controller is setting this variable, and you haven't done that (or it's failing to get a value for some other reason).
Note that the places errors exists in your code snippet are method calls (x.y implies y is a method on the object x in Ruby). This is how to identify this problem in the future.

Rails3: Functional tests fail with NoMethodError: undefined method `user' for nil:NilClass

I'm using Devise (v2.1.2) with Omniauth for user verification. I'm working on a functional test for a controller that takes a JSON object as the POST body and thus using the technique from this question to set the raw POST body. This works fine for development, but when I run tests I get an exception on a method that's completely unauthenticated:
NoMethodError: undefined method `user' for nil:NilClass
Example test:
test "should be able to create an item" do
m = FactoryGirl.attributes_for(:item)
raw_post :create, {}, m.to_json
assert_response :success
end
None of my models have a user method, and nothing in this controller uses authentication, so I was pretty confused. A full stack trace shows that the error comes from the first line of this function in Devise:
def sign_out_all_scopes(lock=true)
users = Devise.mappings.keys.map { |s| warden.user(:scope => s, :run_callbacks => false) }
warden.raw_session.inspect
warden.logout
expire_devise_cached_variables!
warden.clear_strategies_cache!
warden.lock! if lock
users.any?
end
So it looks like in my functional tests (and only in my functional tests) the warden object is nil.
Why is this function being called on an unauthenticated request?
Why doesn't the warden object exist here?
What can I do to fix it?
No idea, Devise is doing its own thing.
See 1.
Include Devise::TestHelpers.
The Devise documentation says that you need to include the helpers in order to use them. It does not say that if you don't include the helpers your functional tests will fail, including those that don't use any authentication, but that's what happens.
(Note the JSON handling here, which I originally thought was the problem, ended up being just a red herring. Even with standard post or get you will have this problem.)

carrierwave - rails 3.1- undefined method: image_will_change

I get an error that look like this:
undefined method `post_image_will_change!' for #<Post:0xf4e9184>
app/controllers/posts_controller.rb:43:in `new'
app/controllers/posts_controller.rb:43:in `create'
I've included this in my "post" model:
attr_accessible :title, :name, :content, :post_image
mount_uploader :post_image, PostImageUploader
and in _form.html.erb I've added:
:html => { :multipart => true }
I looked CarrierWave Error but that doesn't help me.
Any clues of what generates that error? I've migrated the database and so forth (followed the railscasts guide on carrierwave exactly..)
The OP comments that he fixed it, however there's no answer set so I thought I'd add one for people coming across this in the future, which included myself until I figured it out :)
undefined method `x_will_change!' for # happens if you forget to add a column in your model's db table. If you have a model User and a AvatarUploader, with the uploader mounted as in the Carrierwave docs:
class User < ActiveRecord::Base
mount_uploader :avatar, AvatarUploader
end
Then the error will read
undefined method `avatar_will_change!' for #<User:0x00...>
To fix it (based on this example) add a column in a migration run the following in the console:
rails g migration AddAvatarToUsers avatar:string
This will generate the following migration:
class AddAvatarToUsers < ActiveRecord::Migration
def change
add_column :users, :avatar, :string
end
end
Then migrate to apply the change (again in the console):
rake db:migrate
I suppose that author just forgot to run:
rake db:migrate
ALso, if you met such error inside of your tests then you should run:
rake db:test:prepare
Also, for anyone getting this error on heroku, you need to run
heroku run rake db:migrate
and
heroku restart
in the terminal after adding/removing fields/tables from your database.
Kreek, this is obviously a minor oversight, as most people would have realized by now, you probably meant to run this command, as one should, outside the console, otherwise, one would get the following:
'NameError: undefined local variable or method `migrate' for main:Object'.
I had similar problem but mine was because I was copying and pasting codes and forgot to delete
mount_uploader :picture, PictureUploader
from my model which did not use pictures.
Hope this help others in future who could not figure out what happened

Creating a plist in Rails 3

I'm new to Rails and I'm trying to learn it using Rails 3 (RC).
I have managed to use http://plist.rubyforge.org/ for supporting the output of plists. I would like to check with you guys to see if my approach is the right way to do it. Here goes:
In the gemfile I added gem 'plist'
In config/initializers/mime_types.rb I added Mime::Type.register "application/plist", :plist
In the controller, I added format.plist { render :plist => #product } in show
In the model, I added
def to_plist
attributes.to_plist
end
And finally, in the view file show.plist.erb, I have <%= raw #product.to_plist %>
Accessing for instance /products/2.plist works fine, but being new to Rails, I'm wondering if there are anything I should have done differently.
Looks right to me.
The only suggestion I have is to perhaps mix in the to_plist method into ActiveRecord::Base so that you don't have to define it over and over in each model. Perhaps this method will even support render_with syntax?
I'm about to do something similar myself.

Resources