【问题标题】:Convert resource routes in Rails to non-resource routes将 Rails 中的资源路由转换为非资源路由
【发布时间】:2015-07-16 10:42:19
【问题描述】:

我已经使用非 db 支持的模型实现了这个简单的表单。而且我能够执行验证并在其上显示表单字段。但我已经使用资源实现了这一点。现在我想了解如何添加更多动作,除了 CRUD 之外,并为它们定义路由。或者,如果我想停止使用resources,并明确定义操作路径。我应该如何处理?

我的文件:

控制器:new_forms_controller.rb

class NewFormsController < ApplicationController
  def new
   @form = NewForm.new
   flash[:notice] = nil
  end

  def index

  end

  def create
    @form = NewForm.new(params[:new_form])
    if @form.valid?
      flash[:notice] = "Successfully created recommendation."
      render :action => 'show'
    else
      render :action => 'new'
    end
  end

  def show

  end

end

型号:new_form.rb

class NewForm
  include ActiveModel::Validations
  include ActiveModel::Conversion
  include ActiveModel::Naming

  attr_accessor :title, :article, :content, :author

  validates :title, :article, :content, :author, :presence => true
  validates :title, :article => {:minimum => 5 }

  def initialize(attributes = {})
    attributes.each do |name, value|
      send("#{name}=", value)
    end
  end

  def persisted?
    false
  end

end

路线:route.rb

TestBranch::Application.routes.draw do
  resources :new_forms
  root :to => "new_forms#new"
end

新视图

<%= content_for :title, "New Form" %>

<% if flash[:notice] %>
    <p><%= flash[:notice]%></p>
<% end %>

<%= form_for @form do |f| %>

    <% if @form.errors.any? %>
        <div id="error_explanation">
          <h2>
            <%= pluralize(@form.errors.count, "error") %> prohibited
            this article from being saved:
          </h2>
          <ul>
            <% @form.errors.full_messages.each do |msg| %>
            <li><%= msg %></li>
            <% end %>
          </ul>
        </div>
    <% end %>

    <p>
      <%= f.label :titleID %> <br/>
      <%= f.text_field :titleID %><br/>
    </p>
    <p>
      <%= f.label :articleID %><br/>
      <%= f.text_field :articleID %><br/>
    </p>
    <p>
      <%= f.label :content %><br/>
      <%= f.text_field :content %><br/>
    </p>
    <p>
      <%= f.label :author %><br/>
      <%= f.text_field :author %><br/>
    </p>

    <p><%= f.submit "Submit"  %></p>
<% end %>

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 routes


    【解决方案1】:

    resources 基本上可以节省您编写大量代码并让您能够自己编写代码。由于您已要求自己明确定义路径,因此您可以通过以下方式定义它们:

    在 routes.rb 中,

    get '/users' => 'users#index'
    get '/users/:id' => 'users#show'
    get '/users/new' => 'users#new'
    post '/users' => 'users#create'
    get '/users/:id/edit' => 'users#edit'
    put '/users' => 'users#update'
    delete '/users' => 'users#destroy'
    

    这些所有的行基本上等同于resources :users。此外,您还询问了如何添加更多操作,并为它们定义相应的路由:首先,您可以查看我为resources :users 写的内容,其次,您可以查看guides for routes

    【讨论】:

    • 所以如果我添加上述路由,并从 routes.tb 中删除资源 :new_forms,我的应用程序应该可以正常工作吗?但它在第 7 行显示未定义的方法 `new_forms_path' 错误:
    • 您添加的路线是否完全相同?您应该为form 定义它们,例如get '/forms' =&gt; 'forms#index'
    • 哦!我的错!我只是盲目地复制了上面的代码!我将其更改为 new_forms,现在可以正常工作了。谢谢:)
    • 欢迎您!如果这回答了您的问题,您可以接受。通过点击屏幕左侧的“打勾”标记,您可以接受答案。
    • 嘿,所以我在报告控制器下添加了一个动作组织!在 routes.rb 中,我添加了:resources :reports do collection do get 'organise' end end 并使用 organise_reports_path 链接到该位置。但我收到 undefined local variable or method organise_reports_path' ` 错误。
    【解决方案2】:

    为资源添加额外的路由非常简单: 自定义resources 生成的路由非常简单:

    resources :users do
      collection do
        get :search
      end
    
      member do
        get :info
      end
    end
    

    这会给我们:

    GET /users/search => users#search
    GET /users/:id/info => users#info
    

    但是,在添加更多路由之前,请先问问自己,您要实现的目标是否实际上与现有的 CRUD 操作之一不匹配 - 在十分之九的情况下会匹配。

    【讨论】:

      【解决方案3】:

      您需要向控制器添加所需的操作并将相应的路由添加到route.rb。这是一个例子:

      route.rb:

      TestBranch::Application.routes.draw do
      
        resources :new_forms do
          collection do
            get :new_action1    # will produce /new_forms/new_action1
            post :new_action2   # will produce /new_forms/new_action2
            delete :new_action3 # will produce /new_forms/new_action3
          end
      
          member do
            get :new_action4    # will produce /new_forms/:id/new_action4
            put :new_action5    # will produce /new_forms/:id/new_action5
            delete :new_action6 # will produce /new_forms/:id/new_action6
          end
        end
      
        root :to => "new_forms#new"
      end
      

      控制器:new_forms_controller.rb

      class NewFormsController < ApplicationController
        ...
      
        def new_action1
          ...
        end
      
        def new_action2
          ...
        end
      
        def new_action3
          ...
        end
      
        def new_action4
          ...
        end
      
        def new_action5
          ...
        end
      
        def new_action6
          ...
        end
      
        ...
      end
      

      【讨论】:

        【解决方案4】:

        您可以使用match 手动定义路由

        match 'new_form' => 'new_form#index', :via => :get
        

        这会将get 请求与/new_form 匹配到new_form 控制器中的index 操作

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-11-12
          • 2011-01-17
          • 1970-01-01
          • 1970-01-01
          • 2012-02-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多