default:{action:'method'}在rails路由中做什么?

时间:2015-09-25 11:47:22

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

我遇到了一句话:

rake routes

在铁路路线内。

resources :assesments Prefix Verb URI Pattern Controller#Action assesments GET /assesments(.:format) assesments#index POST /assesments(.:format) assesments#create new_assesment GET /assesments/new(.:format) assesments#new edit_assesment GET /assesments/:id/edit(.:format) assesments#edit assesment GET /assesments/:id(.:format) assesments#show PATCH /assesments/:id(.:format) assesments#update PUT /assesments/:id(.:format) assesments#update DELETE /assesments/:id(.:format) assesments#destroy 打印:

default: { action: :index}

Prefix Verb URI Pattern Controller#Action assesments GET /assesments(.:format) assesments#index {:default=>{:action=>:index}} POST /assesments(.:format) assesments#create {:default=>{:action=>:index}} new_assesment GET /assesments/new(.:format) assesments#new {:default=>{:action=>:index}} edit_assesment GET /assesments/:id/edit(.:format) assesments#edit {:default=>{:action=>:index}} assesment GET /assesments/:id(.:format) assesments#show {:default=>{:action=>:index}} PATCH /assesments/:id(.:format) assesments#update {:default=>{:action=>:index}} PUT /assesments/:id(.:format) assesments#update {:default=>{:action=>:index}} DELETE /assesments/:id(.:format) assesments#destroy {:default=>{:action=>:index}} 打印:

resources :assesments, defaults: {action: :index }

默认操作会发生什么变化?

修改

我打错了。它应该是:

rake routes

Prefix Verb URI Pattern Controller#Action assesments GET /assesments(.:format) assesments#index POST /assesments(.:format) assesments#create new_assesment GET /assesments/new(.:format) assesments#new edit_assesment GET /assesments/:id/edit(.:format) assesments#edit assesment GET /assesments/:id(.:format) assesments#show PATCH /assesments/:id(.:format) assesments#update PUT /assesments/:id(.:format) assesments#update DELETE /assesments/:id(.:format) assesments#destroy 现在看起来正常:

/*Intercepts the form submision*/
$('#myform').submit(function(e) {
  /*sets send to true*/
  var send = true;
  /*foreach required element*/
  $('.required').each(function() {
    /*check if input is valid*/
    if (!$(this).val()) {
      /*if not valid, don't send and mark red*/
      send = false;
      $(this).css('background-color', 'red');
    } else {
      /*if valid, take away mark*/
      $(this).css('background-color', 'none');
    }
  });
  /*if don't send, prevent sending*/
  if (!send) {
    e.preventDefault();
    return false;
  }
});

1 个答案:

答案 0 :(得分:0)

TL; DR

如果我们有

resources :assesments, defaults: { format: 'jpg' }, default: { format: 'xml'}

我们会访问localhost/assessments而不是params哈希看起来像:

{
 "format"=>"jpg", # FROM defaults: {}
 "default"=> { "format"=>"xml" }, # FROM default: {}
 "controller"=>"assesments", WE CAN NOT CHANGE IT. COMES FROM RAILS
 "action"=>"index" # WE CAN NOT CHANGE IT. COMES FROM RAILS
}

---更多

defaults: { anything_key: 'any_value' }会将哈希内容添加到控制器内可访问的params哈希值(但是您无法覆盖控制器提供的值 - 即::action:controller)。

添加defaults: { action: :anything }不会产生任何影响,但添加defaults: { format: 'jpg' }会在路由范围包装的所有控制器中添加{ format: 'jpg' }params哈希。

添加default: { some_key: 'value' }会将此哈希(default: { some_key: 'value' })添加到由路由范围包装的控制器内的每个参数哈希。