我使用非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 %>
答案 0 :(得分:1)
向资源添加额外路由非常简单:
自定义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动作实际上没有匹配 - 在十分之九的情况下。
答案 1 :(得分:0)
您需要向控制器添加所需的操作,并将相应的路由添加到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
答案 2 :(得分:0)
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
撰写的内容,其次,您可以查看{{ 3}}
答案 3 :(得分:0)
您可以使用match
match 'new_form' => 'new_form#index', :via => :get
这会将get
个/new_form
请求与index
控制器中的new_form
操作匹配