以下是我的控制器代码
class AdminController < ApplicationController
def new
DashboardUser.create(
:USER_NAME => params[:user_name],
:PASSWORD => params[:password],
:LAST_NAME => params[:last_name],
:FIRST_NAME => params[:first_name],
:MIDDLE_NAME => params[:middle_name],
:PHONE => params[:phone]
)
end
private
def dashboard_params
params.require(:dashboard_user).permit(:id, :user_name, :password, :last_name, :first_name, :middle_name , :phone)
end
end
下面是我的型号代码
class DashboardUser < ActiveRecord::Base
self.table_name = "dashboard_user"
end
下面是我的html.erb代码
<%= form_tag("/admin/user_creation", :method=>"post") do %>
<%= text_field_tag(:user_name) %>
<%= password_field_tag(:password) %>
<%= text_field_tag(:last_name) %>
<%= text_field_tag(:first_name) %>
<%= text_field_tag(:middle_name) %>
<%= text_field_tag(:phone) %>
<%= submit_tag("Add User", :class=>"btn") %>
<% end %>
所以当我点击添加用户时,控制器不会将参数保存到数据库中。
我有什么办法可以将值传递给数据库吗?
我是rails的新手,请帮助谢谢:D
routes.rb代码
Rails.application.routes.draw do
resources :admin do
post 'user_creation', on: :collection
end
resources :admin, :only => [:show, :new, :user_creation]
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root 'admin#user_creation'
# Example of regular route:
# get 'products/:id' => 'catalog#view'
# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end
# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable
# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
end
这是我的路线.rb代码 我提交表格后试图进入同一页面
错误跟踪
AbstractController::ActionNotFound (The action 'show' could not be found for AdminController):
actionpack (4.1.6) lib/abstract_controller/base.rb:131:in `process'
actionview (4.1.6) lib/action_view/rendering.rb:30:in `process'
actionpack (4.1.6) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.1.6) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.1.6) lib/action_controller/metal.rb:232:in `block in action'
actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:82:in `call'
actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:50:in `call'
actionpack (4.1.6) lib/action_dispatch/journey/router.rb:73:in `block in call'
actionpack (4.1.6) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.6) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:678:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.6) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.6) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.6) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.6) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.6) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.6) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.6) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.6) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.6) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.6) lib/rails/engine.rb:514:in `call'
railties (4.1.6) lib/rails/application.rb:144:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/home/vadmin/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/home/vadmin/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/home/vadmin/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
答案 0 :(得分:5)
您的整个过程是错误的,您正在将表单提交给新操作及其get
方法。这样做
class AdminController < ApplicationController
def new
@dashboard_user = DashboardUser.new
end
def user_creation
@dashboard_user = DashboardUser.new(dashboard_params)
@dashboard_user.save!
end
private
def dashboard_params
params.require(:dashboard_user).permit(:id, :user_name, :password, :last_name, :first_name, :middle_name , :phone)
end
end
在您看来,它应该是admin/new.html.erb
<%= form_for @dashboard_user, url: "/admin/user_creation", method: "post" do |f| %>
<%= f.text_field(:user_name) %>
<%= f.password_field(:password) %>
<%= f.text_field(:last_name) %>
<%= f.text_field(:first_name) %>
<%= f.text_field(:middle_name) %>
<%= f.text_field(:phone) %>
<%= f.submit "Add User", :class=>"btn" %>
<% end %>
并且您的路线user_creation
应为post
行动
Rails.application.routes.draw do
resources :admin do
post 'user_creation', on: :collection
end
root 'admin#new'
end
答案 1 :(得分:-1)
您需要使用资源来创建记录。所以,这是你可以改变的东西。
class AdminController < ApplicationController
def new
@dashboard_user = DashboardUser.new
DashboardUser.create(dashboard_params)
end
private
def dashboard_params
params.require(:dashboard_user).permit(:id, :user_name, :password, :last_name, :first_name, :middle_name , :phone)
end
end
<%= form_for(@dashboard_user, url: some_path, :method=>"post") do |f| %>
<%= f.text_field(:user_name) %>
<%= f.text_field(:middle_name) %>
<%= f.submit("Add User", :class=>"btn") %>
<% end %>
应该是这样的......这对你更有意义吗?