Rails + CarrierWave: NoMethodError: undefined meth

2019-02-20 00:45发布

问题:

I'm using Devise and Carrierwave. I genderated users with devise then created a separate user controller where this page is now (users/search)

Carrierwave was working perfectly and displaying images in my show (users/1 etc) however when I have moved things over to the new 'search' page, its broken.

Here's the code in my view.. (note, the view works when I remove the image, so the other code is fine)

<% @males.each do |user| %>
  <%= image_tag @user.image_url(:thumb).to_s %>
  <%= user.firstname %>
<% end %>

I've tried adding in my users_controller:

def search
  @users = User.all
end

As you might be able to tell by the code in my view I am filtering users by gender (which ive defined @males and @females) You can see that in my model below.

class User < ActiveRecord::Base

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :firstname, :lastname, :gender, :image, :remote_image_url, :image_cache, :remove_image

  mount_uploader :image, ImageUploader

  scope :male, -> { where gender: 'male' }
  scope :female, -> { where gender: 'female' }

I've tried adding :image_url to both attr_accessible and mount_uploader and it didn't work, however I know that would not be the problem as I said it works fine in the show.

Any ideas ??

note: I've checked all related questions on here and I still can't find a solution which helps me. Thanks very much

回答1:

<% @males.each do |user| %>
  <%= image_tag @user.image_url(:thumb).to_s %>
  <%= user.firstname %>
<% end %>

I think you have an extra @ sign. Try <%= image_tag user.image_url(:thumb).to_s %>