mercredi 22 avril 2015

Unable to insert data in table using form to submit data

I am unable to insert data in user_preferences table where as I am getting all the attributes in params. Please look into my all files

user_preference.rb

class UserPreference < ActiveRecord::Base
    belongs_to :user
    attr_accessor

    def self.bgcolor_options
      [["Orange", "#FF3300"], ["Green", "#00FF00"], ["Blue", "#0000FF"], ["Pink", "#FF0066"], ["Yellow", "#FFFF00"], ["White", "#FFFFFF"]]
    end
    def self.font_options
        [["Times New Roman", "Times New Roman"], ["Verdana", "Verdana"],["Arial", "Arial"],["sans-serif", "sans-serif"]]
    end
end

user_preferences_controller.rb

class UserPreferencesController < ApplicationController
    def new
        @user_preference = UserPreference.new
    end

    def create
        @user_preference = UserPreference.new(user_pref_params)
        @user_preference.save unless user_signed_in?
        render 'user_preferences/new'
    end
    def edit
    end
    def update
    end

    private
    def user_pref_params
        params.require(:user_preference).permit(:title, :bgcolor, :font, :description)
    end
end

routes.rb

Rails.application.routes.draw do

  resources :user_preferences
  post "/user_preferences/new"
  devise_for :users
  devise_scope :user do
    authenticated :user do
      root :to => 'user_preferences#new', as: :authenticated_root
    end
    unauthenticated :user do
      root :to => 'devise/registrations#new', as: :unauthenticated_root
    end
  end

user_preferences/new.html.erb

<%= form_for @user_preference, :url => { :action => "create" }  do |u|%>
 <div style="background-color:#{current_user.user_preference.bgcolor.nil? ? '#FFFFFF' : current_user.user_preference.bgcolor}">
 <p>
    <%= u.label :title %><br>
    <%= u.text_field :title %>
  </p>

  <p>
    <%= u.label :description %><br>
    <%= u.text_field :description %>
  </p>

  <p> <%= u.label :back_ground_color %><br>
    <%= u.select :bgcolor, options_for_select(UserPreference.bgcolor_options) %>
  </p>

  <p>
    <%= u.label :font %><br>
    <%= u.select :font, options_for_select(UserPreference.font_options) %>

  </p>

 <br >
  <p>
    <%= u.submit %>
  </p>
  <hr >
  <div style="background: #{current_user.user_preferences.bgcolor};"></div>
  <div style="background-color:#{current_user.user_preferences.font.nil? ? 'Arial' : current_user.font}">
    This is the changes made in background
  </div>

</div>

<% end %>

schema.rb

ActiveRecord::Schema.define(version: 20150422034042) do

  create_table "user_preferences", force: :cascade do |t|
    t.text     "title"
    t.string   "font"
    t.text     "description"
    t.string   "bgcolor"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.integer  "user_id"
  end

  create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end

Aucun commentaire:

Enregistrer un commentaire