在rails路由中,嵌套资源导致白屏

时间:2013-11-22 19:12:18

标签: ruby-on-rails model-view-controller ruby-on-rails-4 rails-routing nested-resources

我有两个模型:Schedule和Project。计划belongs_To项目和项目has_one计划。计划和项目的路线嵌套如下:

get 'projects/current', to: 'projects#show_current', as: :current_freelancer_projects
resources :projects do
  resources :schedules
end 

当我运行rake路线时,它表示制定新时间表的路径是:

 new_project_schedule GET      /projects/:project_id/schedules/new(.:format)

问题是,当我在页面上包含指向new_project_Schedule的链接时,该页面在safari中变为白色。在Firefox中,页面将加载,但是当我单击表单的提交按钮时,我收到错误:

First argument in form cannot contain nil or be empty

如果我注释掉表单的链接,我就不会看到白屏。这是链接:

<% @projects.each do |project| %>
    <tr>
        <td><%= project.title %></td>
        <td><%= project.employer.email %></td>
        <td>date</td>
        <td>rating</td>
        <td>bid</td>
        <td>tags</td>
        <td><%= link_to 'Create Schedule', new_project_schedule_path(project.id) %></td>
    </tr>
<% end %>

我知道@projects是定义的,因为所有其他td单元都在工作。我该如何解决这个问题?

更多信息:

以下是显示所有项目的页面控制器,并且包含指向每个创建计划页面的链接:

def show_current
    @projects = current_user.projects
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

时刻表#新:

def new #This controller is what the link above goes to
    @schedule = Schedule.new
    @project = Project.find(params[:project_id])
    @schedule.tasks.build #tasks is a model that belongs_to schedule
end

项目#show_current:

def show_current #In this action view, @projects is looped through and for each project, a link to schedules#new is displayed,
    @projects = current_user.projects
end

新计划视图:

<%= form_for [@project, @schedule] do |f| %>
    <%= f.fields_for :tasks do |builder| %>
            <%= render 'task_fields', :f => builder %>
    <% end %>
    <p><%= link_to_add_fields "Add task", f, :tasks %>
    <p><%= f.submit "Submit" %></p>
<% end %>

更新:

如果我将link_to更改为:

<%= link_to 'Create Schedule', new_project_schedule_path(project.id, Schedule.where(project_id: project.id))

我不再获得白色屏幕,但网址混乱了:

http://localhost:3000/projects/24/schedules/new.%23%3CActiveRecord::Relation::ActiveRecord_Relation_Schedule:0x007fa1d6422a78%3E

我知道网址不对,所以有些东西告诉我我实际上没有解决问题。

更新:

当我为项目#show_current的视图页面更改任何内容时,该页面将暂时再次运行。但是,如果我刷新几次或转到另一页然后返回,它会再次变白。如果我在页面上更改某些内容,它将暂时再次运行,然后最终再次变为白色。所以我看到它的方式,问题必须是项目#show_current控制器或路由。我将一些路由代码添加到上面的路由中,因为现在看来它可能是相关的。话虽这么说,我已经尝试改变路线代码,但没有任何作用。

1 个答案:

答案 0 :(得分:-1)

这个问题以及我遇到的其他几个问题与嵌套资源无关。实际上,问题与缓存有关。禁用缓存修复了问题。