rails 表单路由设置以及rails4.0 create引起的ActiveModel::ForbiddenAttributesError错误
2013-07-07 23:17 张小萌 阅读(927) 评论(0) 编辑 收藏 举报表单路由设置:post '/ads/create‘=>’ads#create'
表单页面路由设置:get'/ads/new'=>'ads#new'
map.connect ':controller/:action/:id' 和 map.connect ':controller/:action/:id.:format' 在rails3里为match ':controller(/:action(/:id(/:format)))',via:[:get,:post]
map.connect 'products/:id', :controller => 'catalog', :action => 'view' 在rails 3里为match 'products/:id', :to => 'catalog#view'
map.logout 'logout', :controller => 'sessions', :action => ''在rails 3里为match 'logout', :to => 'sessions#destroy', :as => 'logout'
map.root :controller => 'welcome', :action => 'show'在rails 3里为root :to => 'welcome#show'
match 'account' => 'account#index' 相当于: match 'account', :to => 'account#index' 和 match 'info' => 'projects#info', :as => 'info'
match 'account/overview' 相当于 match 'account/overview', :to => 'account#overview'
get 'account/overview'相当于 match 'account/overview', :via => 'get'
match 'account/setup', :via => [:get, :post] 支持get\post\put\delete四种HTTP方法
resource用法:
- resources :posts, :except => [:index]
- resources :posts, :only => [:new, :create]
- # edit_post GET /posts/:id/modify(.:format) {:controller=>"posts", :action=>"edit"}
- resources :posts, :path_names => { :edit => 'modify' }
- resources :projects do
- resources :tasks, :people
- end
- resources :products do
- collection do
- get :sold
- post :on_offer, :search
- end
- get :buy, :on => :member
- post :batch, :on => :collection
- end
- resource :session do
- get :create
- end
controller表单页面设置:def new @ad=Ad.new end
controller表单提交设置:def create @ad=Ad.new(params[:ad]) @ad.save end
ActiveModel::ForbiddenAttributesError错误有可能是因为升级到rails 4.0引起的,此时要将controller表单的提交设置在 @ad=Ad.new(params[:ad])这句之前增加params.permit!这句话。