Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何修复RubyonRails中的控制器错误并使回形针启动并运行_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 如何修复RubyonRails中的控制器错误并使回形针启动并运行

Ruby on rails 如何修复RubyonRails中的控制器错误并使回形针启动并运行,ruby-on-rails,ruby,Ruby On Rails,Ruby,我已经尝试了很长一段时间在我的网站上安装和运行回形针,并且多次遵循github上概述的逐步过程,但仍然不起作用。我真的需要尽快启动并运行它。当我通过localhost运行代码时,我得到nil:NilClass的消息undefined method`id'。这位于下面评论的第20行。为什么在owners方法下,同一行不显示为错误 class SurveysController < ApplicationController def new @survey = Surv

我已经尝试了很长一段时间在我的网站上安装和运行回形针,并且多次遵循github上概述的逐步过程,但仍然不起作用。我真的需要尽快启动并运行它。当我通过localhost运行代码时,我得到nil:NilClass的消息undefined method`id'。这位于下面评论的第20行。为什么在owners方法下,同一行不显示为错误

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
我做错了什么?我怎样才能让回形针启动并运行?我真的很感激能在这方面得到任何帮助,因为我有一个非常接近的最后期限。

你在哪里设置当前用户

通常,这是通过sign_in方法或类似方法设置的,例如:

app/helpers/sessions\u helper.rb

您的具体方法可能会有所不同,但通常假定在您的项目中存在这种通用模式。无论您在哪里处理登录流,都应该将当前用户设置为可以通过控制器引用的某个变量。您需要确保正在设置和使用该变量


此外,您应该在控制器中设置before_过滤器,以确保只有登录的用户才能访问这些操作。

您也使用Desive吗

如果是

请加上

before_filter :authenticate_user!, only: [:seeker]
之后

class SurveysController < ApplicationController
然后,Desive将在您执行搜索者操作之前要求您登录


如果您不使用Desive,您是否介意将您的Gem文件发布或让我知道您从何处获得此当前用户

在这种情况下,当前用户似乎为零。您是否有当前用户登录?好的,谢谢。我做了这些更改,但仍然有一个错误。它的意思是param丢失或值为空:survey以及:app/controllers/surveys\u controller.rb:11:in survey\u params'app/controllers/surveys\u controller.rb:15:in owners'在哪里设置?发生了什么事?两个注释:{1}您定义了两个不同的survey_params,所以这很糟糕,{2}听起来您的params哈希不包含:survey键。您应该记录参数以进行进一步调查。此外,此时,您可能应该解决此问题,并且在完成更多调查后,可能会使用清理后的代码提交一个新问题,例如无重复方法,当前调查后的其他详细信息,等-如果你坚持这张罚单,那么你可能会因为话题漂移等而少看一眼。
class SurveysController < ApplicationController