简体   繁体   English

如何修复Ruby on Rails中的控制器错误并启动并运行Paperclip

[英]How to fix controller error in Ruby on Rails and get Paperclip up and running

I have been trying for quite a while to get Paperclip up and running on my website and have followed the step-by-step process outlined on github multiple times and it still won't work. 我一直在尝试了好一段时间才能得到回形针起来,在我的网站上运行,并且已经按照概述GitHub上多次的一步一步的过程,它仍然是行不通的。 I really need to get this up and running as soon as possible. 我真的需要尽快启动并运行它。 When I run the code through localhost, I get the message "undefined method `id' for nil:NilClass." 通过本地主机运行代码时,收到消息“ nil:NilClass的未定义方法'id'”。 This is located in line 20 (commented below). 它位于第20行(在下面进行了注释)。 Why doesn't it appear as an error for the identical line under the owners method? 为什么在owner方法下,同一行没有显示为错误?

class SurveysController < ApplicationController
    def new
        @survey = Survey.new
    end

    def create
    end

    def survey_params
        params.require(:survey).permit(:name, :email, :password)
    end

    def owners
        @survey = Survey.new(survey_params)
        @survey.user_id=current_user.id
        @survey.save
    end

    def seeker
        @survey = Survey.new
        @survey.user_id=current_user.id   # line with the error (line 20)
        @survey.save
    end

    private

        def survey_params
            params.require(:survey).permit(:first_name, :last_name, :email, :looking_for, :moving_to, :gender, :coed, :age, :roommate_type, :housing_type, :roommates_estimate, :roommates_amount, :roommates_group, :roommates_names, :max_rent, :move_in, :move_out, :bedrooms, :amenities, :apartment_pet, :roommate_pet, :hometown, :school, :company, :terms, :avatar, :wake_up, :bedtime, :smoke, :smokeoften, :smokesocially, :smokequit, :drink, :drinkoften, :drinksocially, :drinkquit, :drugs, :drugsoften, :drugssocially, :drugsquit, :interest, :sexualactivity, :sexprivacy, :roommatesexprivacy, :overnight, :overnightoften, :roommateovernight, :realty, :availability, :rent, :address, :otherroom, :age_min, :age_max, :age_mode, :pad_photo, :user_status, :sociability, :tidiness, :question, :noise, :political, :religion, :user_id)
        end

        def idcheck
        end
end

What am I doing wrong and how can I get Paperclip up and running? 我在做什么错,我该如何启动和运行Paperclip? I would really appreciate any kind of assistance I can get with this because I have a pretty close deadline. 我真的很感谢我能提供的任何帮助,因为我的截止日期非常接近。

Where are you setting current_user ? 您在哪里设置current_user

Generally this is set via a "sign_in" method or some such, eg: 通常,这是通过“ sign_in”方法或类似方法设置的,例如:

app/helpers/sessions_helper.rb app / helpers / sessions_helper.rb

module SessionsHelper
  def sign_in(user)
    # "permanent" = expires: 20.years.from_now.utc
    cookies.permanent[:remember_token] = user.remember_token
    self.current_user = user
  end

  def current_user=(user)
    @current_user = user
  end

  def current_user
    return unless cookies[:remember_token]
    @current_user ||= User.find_by(remember_token: cookies[:remember_token])
  end
end

Your specific methods likely vary, but that general pattern is often assumed to be in place in your project. 您的特定方法可能会有所不同,但是通常会在您的项目中采用一般模式。 Wherever you handle login flow, the current user should be set to some variable that you can refer to via your controllers. 无论在哪里处理登录流程,都应将当前用户设置为可以通过控制器引用的某个变量。 You need to make sure you're setting and using that variable. 您需要确保正在设置和使用该变量。

Additionally, you should have a before_filter in the controller to ensure only logged-in users can access these actions. 此外,您应该在控制器中有一个before_filter ,以确保只有登录用户才能访问这些操作。

do you also use Devise? 您还使用Devise吗?

if yes, 如是,

please add 请加

before_filter :authenticate_user!, only: [:seeker]

after

class SurveysController < ApplicationController

then Devise will ask you login before you do the seeker action. 然后Devise会在执行搜索程序操作之前要求您登录。

If you don't use devise, would you mind post your Gemfile or let me know where did you get this current_suer 如果您不使用devise,您可以介意发布Gemfile还是让我知道您从哪里获得了current_suer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM