In rails routes, nested resources is causing white screen

自闭症网瘾萝莉.ら 提交于 2019-12-13 23:55:59

问题


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)

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 %>

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

SOME MORE INFO:

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

UPDATE:

Here are some of the controllers:

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

Schedules#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:

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

New Schedule view:

<%= 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 %>

UPDATE:

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

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

UPDATE:

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.


回答1:


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.



来源:https://stackoverflow.com/questions/20152691/in-rails-routes-nested-resources-is-causing-white-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!