简体   繁体   中英

Rails 4.1.6 inserting not saving in database from form

Below is my controller code

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

below is my model code

class DashboardUser < ActiveRecord::Base
  self.table_name = "dashboard_user"
end

below is my html.erb code

<%= 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 %>

so what happens is that when i click add user the parameters are not saved into the database by the controller.

Is there anything i could do to pass the values to the database ?

i am new to rails please help thanks :D

routes.rb code

       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

this is my routes .rb code i am trying to get to the same page after submitting the form

error trace

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'

Your whole process is wrong, you are submitting form to new action and its a get method. Do this

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

In your view, it should be 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 %>

and in your routes user_creation should be post action

Rails.application.routes.draw do
  resources :admin do
    post 'user_creation', on: :collection
  end

  root 'admin#new'
end

you need to use your resource to create your record. So, here is something you can change.

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 %>

Should be something like that...does this make more sense to you?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM