在Rails路线中,嵌套资源导致白屏 [英] In rails routes, nested resources is causing white screen

查看:75
本文介绍了在Rails路线中,嵌套资源导致白屏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型:Schedule和Project.计划属地_计划到项目和项目has_one计划. Schedule和Project的路由是这样嵌套的:

I have two models: Schedule and Project. Schedule belongs_To Project and Project has_one schedule. The routes for schedule and Project are nested like this:

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

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

When I run rake routes, it says the path to make a new schedule is:

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

问题是,当我在页面上包含指向new_project_Schedule的链接时,该页面在野生动物园中会变成白色.在Firefox中,页面将加载,但是当我单击表单的提交"按钮时,出现错误:

The problem is, when I include a link to new_project_Schedule on a page, the page goes white in safari. In Firefox, the page will load, but when I click the submit button of the form, I get the error:

First argument in form cannot contain nil or be empty

如果我注释掉表单的链接,则不会出现白屏.这是链接:

If I comment out the link to the form, I don't get the white screen. Here is the link:

<% @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单元都在工作.我该如何解决?

I know @projects is defined because all of the other td cells are working. How can I fix this?

更多信息:

这是显示所有项目并具有指向每个创建计划页面的链接的页面的控制器:

Here is the controller for the page that displays all the projects and has the links to each create schedule page:

def show_current
    @projects = current_user.projects
end

更新:

以下是一些控制器:

Schedules#create:

Schedules#create:

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

时间表#new:

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

Projects#show_current:

Projects#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更改为此:

If I change the link_to to this:

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

我不再显示白屏,但网址混乱了:

I no longer get the white screen but the url is messed up:

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

我知道url可能不正确,所以有些事情告诉我我实际上还没有解决问题.

I know that url can't be right so something tells me i haven't actually fixed the problem.

更新:

当我更改项目#show_current的视图页面的任何内容时,该页面将暂时恢复工作.但是,如果我刷新几次或转到另一页然后返回,它再次变成白色.如果我更改页面上的某些内容,它将暂时再次起作用,然后最终再次变白.因此,从我的角度来看,问题可能出在projects#show_current控制器或路由上.我在上面的路由中添加了一些路由代码,因为现在看来它可能是相关的.话虽如此,我已经尝试过更改路线代码,但是没有任何效果.

When I change anything for the view page for projects#show_current, the page will work again temporarily. However, if i refresh it a few times or go to another page and then go back, it turns white again. If i change something on the page, it will work again temporarily, and then eventually turns white again. So the way I see it, the problem has to either be with the projects#show_current controller or with the routes. I am adding some of the routes code to the routes above, since now it appears it might be relevant. That being said, I've tried changing the routes code but nothing works.

推荐答案

该问题以及我遇到的其他问题与嵌套资源无关.实际上,问题与缓存有关.禁用缓存可解决此问题.

This problem, along with several others I was having, turned out to have nothing to do with nesting the resources. Actually, the problem had to do with the cache. Disabling the cache fixed the problem.

这篇关于在Rails路线中,嵌套资源导致白屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆