【问题标题】:Can't change root route rails无法更改根路由轨道
【发布时间】:2016-08-09 22:25:55
【问题描述】:

我正在 Rails 中创建一个简单的待办事项列表应用程序,我遇到了下一个问题:

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

附:我需要显示为根页面,因为我无法在索引操作中使用此表单

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

以及rake routes生成的路由

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

【问题讨论】:

  • 为什么要更改根目录?您的意思是要更改根 route 吗? Rails 不是静态服务器。根目录不会影响呈现哪些文件。无论如何,如果您希望项目显示模板在 / 上显示,请使用此路线:root 'projects#show' 但是您的 show 方法需要一个不会自动提供的 project_id 参数。
  • 嗯,我的 rutes.rb 中有一个错字 - 我已经修复了它。但是 / 仍然将我重定向到项目#index,我在这个表单中遇到错误(因为我不能在索引操作中使用带有此类参数的表单。据我所知)

标签: ruby-on-rails ruby


【解决方案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 添加到“新任务”中,然后触发 JS 响应以在模态中呈现表单。

如果您觉得这很新鲜,请查看https://launchschool.com/blog/the-detailed-guide-on-how-ajax-works-with-ruby-on-rails

这将引导您在 Rails 应用程序中使用 AJAX。

希望这会有所帮助!

【讨论】:

  • 该应用程序必须看起来像一个包含项目及其任务的页面,所以我无法创建指向其他页面的链接
  • 您想在根页面显示所有项目及其任务?
  • 是的,用户登录后 - 他应该查看所有项目及其所有任务,并能够通过表单将任务添加到项目中
  • 你不懂我的回答。目前您的show 操作需要@project,但如果您将其设为root,则会收到错误No route matches missing required keys: [:id]。我会更新我的答案。
  • 那么我应该怎么做才能让页面看起来像 - prnt.sc/c3qawe ?创建一些主页,其中包含将用户重定向到show 页面的按钮?
猜你喜欢
  • 1970-01-01
  • 2010-11-22
  • 1970-01-01
  • 1970-01-01
  • 2018-12-06
  • 2019-07-17
  • 1970-01-01
  • 2013-11-26
  • 2013-08-08
相关资源
最近更新 更多