Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.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 rails有一个导致问题的关系-也是白屏_Ruby On Rails_Model View Controller_Database Design_Ruby On Rails 4_Has One - Fatal编程技术网

Ruby on rails rails有一个导致问题的关系-也是白屏

Ruby on rails rails有一个导致问题的关系-也是白屏,ruby-on-rails,model-view-controller,database-design,ruby-on-rails-4,has-one,Ruby On Rails,Model View Controller,Database Design,Ruby On Rails 4,Has One,我有两个模型:计划和项目。计划属于项目,项目有一个计划。有几个问题,但我认为它们都有相同的原因。以下是计划#创建控制器的代码: def create @schedule = Schedule.new(schedule_params) @schedule.project = Project.find(params[:project_id]) if @schedule.project.student_id == current

我有两个模型:计划和项目。计划属于项目,项目有一个计划。有几个问题,但我认为它们都有相同的原因。以下是计划#创建控制器的代码:

def create
            @schedule = Schedule.new(schedule_params)
            @schedule.project  = Project.find(params[:project_id])
            if @schedule.project.student_id == current_user.id
                if @schedule.save && @schedule.freelancer_accepts    
                        flash[:notice] = "Successfully created schedule."
                        redirect_to profile_path(current_user.profile_name)  
                else
                    render :action => 'new', :notice => 'Invalid Schedule'
                end
            else
                render :action => 'new', :notice => 'Schedule is invalid.'
            end
end
以下是问题:

  • 即使这是一种“一对一”的关系,我仍然能够为一个项目创建多个时间表。这导致我将控制器更改为:

    def create
        if !Schedule.where(project_id: params[:project_id]).any?
            @schedule = Schedule.new(schedule_params)
            @schedule.project  = Project.find(params[:project_id])
            if @schedule.project.student_id == current_user.id
                if @schedule.save && @schedule.freelancer_accepts     
                        flash[:notice] = "Successfully created schedule."
                        redirect_to profile_path(current_user.profile_name) 
                else
                    render :action => 'new', :notice => 'Invalid Schedule'
                end
            else
                render :action => 'new', :notice => 'Schedule is invalid.'
            end
        else
            render :action => 'new', :notice => 'A schedule has already been created.'
        end
    end
    
  • 改变是,我基本上将控制器包装在一个if中,该if表示“如果存在计划,请不要创建计划。在我更改代码后,我遇到了以下问题:

  • 在firefox中,当我按下submit按钮创建新的计划时,会出现以下错误:

    First argument in form cannot contain nil or be empty
    
  • 在Safari中,当我按下submit按钮创建一个新的日程表时,页面会变成白色。此外,每次我试图返回显示表单的页面时,页面都会变成白色。这非常奇怪

  • 我该怎么解决这个问题?谢谢

    更新:我的路线和表单视图可能有帮助:

    路线:

    resources :projects do
      resources :schedules
    end
    
    表格第一部分:

    <%= form_for [@project, @schedule] do |f| %>
    
    
    
    结果表明,我遇到的这一问题和其他几个问题实际上并不是由has#one关系引起的。我的缓存存在某种类型的问题。我不确定是什么问题,但通过禁用缓存,我能够解决问题。

    您所说的将
    SchedulesController#create
    更改为与您显示的初始代码相同。有什么区别?我的错误。我只是用我最初打算显示的代码更新了它。您的“无效计划”和“计划无效”渲染最初有效吗?它们仍然有效吗?最初有效。我现在说不出来,因为我无法提交表单。如果您只是更改
    创建
    c回到原来的样子,提交工作正常吗?