无法更改根路由轨道

时间:2016-08-09 22:25:55

标签: ruby-on-rails ruby

我在rails中创建一个简单的Todo列表应用程序,我遇到了下一个问题:

我有一个belongs_to项目的任务 我已经创建了一个模板 - projects/show.html.erb 而且我无法将其设置为我的应用的root page,正如我从调试器中看到的那样 - root始终是projects#index操作

P.S。我需要show作为根页面,因为我无法在索引操作中使用此表单

 <%= form_for [@project, @task],remote: true do |f| %>
          <%= f.input :body,class: 'form-control' %>
          <%= f.submit 'Add task', class: 'btn' %>
        <% end %>

投影控制器

 class ProjectsController < ApplicationController
     before_action :load_project, only: [:show, :edit, :update, :destroy]
     before_action :authenticate_user!

     def index
        @projects = current_user.projects unless current_user.nil?
      end

      def show
        @task = @project.tasks.new
      end

      def new
        @project = current_user.projects.new
      end

      def edit
      end

      def create
        @project = current_user.projects.create(project_params)

        if @project.save
          redirect_to root_path
        else
          render :new
        end
      end

      def update
        if @project.update(project_params)
          redirect_to @project
        else
          render :edit
        end
      end

      def destroy
        @project.destroy
        redirect_to projects_path
      end

      private

    def load_project
      begin
        @project = Project.find(params[:id]) #raises an exception if project not found
      rescue ActiveRecord::RecordNotFound
        redirect_to projects_path
      end
    end

      def project_params
        params.require(:project).permit(:name, :user_id)
      end

end

的routes.rb

Rails.application.routes.draw do
  devise_for :users, :controllers => { :omniauth_callbacks => "callbacks" }
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  root 'projects#index'


  resources :projects do
    resources :tasks
  end




end

佣金路线

生成的路线
todo$ rake routes
                          Prefix Verb     URI Pattern                                    Controller#Action
                new_user_session GET      /users/sign_in(.:format)                       devise/sessions#new
                    user_session POST     /users/sign_in(.:format)                       devise/sessions#create
            destroy_user_session DELETE   /users/sign_out(.:format)                      devise/sessions#destroy
user_facebook_omniauth_authorize GET|POST /users/auth/facebook(.:format)                 callbacks#passthru
 user_facebook_omniauth_callback GET|POST /users/auth/facebook/callback(.:format)        callbacks#facebook
                   user_password POST     /users/password(.:format)                      devise/passwords#create
               new_user_password GET      /users/password/new(.:format)                  devise/passwords#new
              edit_user_password GET      /users/password/edit(.:format)                 devise/passwords#edit
                                 PATCH    /users/password(.:format)                      devise/passwords#update
                                 PUT      /users/password(.:format)                      devise/passwords#update
        cancel_user_registration GET      /users/cancel(.:format)                        devise/registrations#cancel
               user_registration POST     /users(.:format)                               devise/registrations#create
           new_user_registration GET      /users/sign_up(.:format)                       devise/registrations#new
          edit_user_registration GET      /users/edit(.:format)                          devise/registrations#edit
                                 PATCH    /users(.:format)                               devise/registrations#update
                                 PUT      /users(.:format)                               devise/registrations#update
                                 DELETE   /users(.:format)                               devise/registrations#destroy
                            root GET      /                                              home#index
                   project_tasks GET      /projects/:project_id/tasks(.:format)          tasks#index
                                 POST     /projects/:project_id/tasks(.:format)          tasks#create
                new_project_task GET      /projects/:project_id/tasks/new(.:format)      tasks#new
               edit_project_task GET      /projects/:project_id/tasks/:id/edit(.:format) tasks#edit
                    project_task GET      /projects/:project_id/tasks/:id(.:format)      tasks#show
                                 PATCH    /projects/:project_id/tasks/:id(.:format)      tasks#update
                                 PUT      /projects/:project_id/tasks/:id(.:format)      tasks#update
                                 DELETE   /projects/:project_id/tasks/:id(.:format)      tasks#destroy
                        projects GET      /projects(.:format)                            projects#index
                                 POST     /projects(.:format)                            projects#create
                     new_project GET      /projects/new(.:format)                        projects#new
                    edit_project GET      /projects/:id/edit(.:format)                   projects#edit
                         project GET      /projects/:id(.:format)                        projects#show
                                 PATCH    /projects/:id(.:format)                        projects#update
                                 PUT      /projects/:id(.:format)                        projects#update
                                 DELETE   /projects/:id(.:format)                        projects#destroy

1 个答案:

答案 0 :(得分:1)

为什么要将索引显示在页面上?那你怎么能这样做? show操作需要将id传递给控制器​​,以便可以向用户显示特定项目。当用户请求id时,如何传递/

这有意义吗?

我建议您将根留给index页面。在索引视图中,将每个项目链接到其显示页面。没有必要在索引页面中使用嵌套表单。

或者,如果您要在根页面中列出所有项目和任务,请修改index视图以循环执行每个项目的任务。

@projects.each do |project|
  # display project's information
  project.tasks.each do |task|
    # display the task information
    # display a new task button
  end
end

但是你不能像你问的那样显示嵌套表格。因为表单需要@project@task,您无法在index action中确定。也许你可以将remote: true添加到&#34;新任务&#34;然后触发JS响应以在模式中呈现表单。

如果这听起来不熟悉,请参阅https://launchschool.com/blog/the-detailed-guide-on-how-ajax-works-with-ruby-on-rails

这将在您的rails应用程序中使用AJAX。

希望这有帮助!