samedi 18 avril 2015

How to save static page attributes in User model

I have created a controller static_pages with home as index and created a form under its view that named as home.html.erb.



<%= form_for :user do |u| %>

<br >
<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>
<select name="bgcolor" id="bgcolor">
<option value="#FF3300">Orange</option>
<option value="#00FF00">Green</option>
<option value="#0000FF">Blue</option>
<option value="#FF0066">Pink</option>
<option value="#FFFF00">Yellow</option>
<option value="#FFFFFF">White</option>
</select>
</p>

<p>
<%= u.label :font %><br>
<select name="font" id="font">
<option value="Times New Roman">Times new Roman</option>
<option value="Verdana">Verdana</option>
<option value="Arial">Arial</option>
<option value="sans-serif">serif</option>
</select>
</p>

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

<div style="background-color:#{current_user.bgcolor.nil? ? '#FFFFFF' : current_user.bgcolor}">

</div>

<% end %>


I have created this form for user who currently logged in the application and I have used devise gem for this.Now if user fill this form and submit it has to change the background color of page and I am trying to save this in user database. The schema for User is as:



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"
t.text "title"
t.string "bgcolor"
t.text "description"
t.text "font"
end


and I am handling this data in controller as static_pages_controller.rb



class StaticPagesController < ApplicationController
def home
@user = User.new(params.require(:user).permit(:title, :description, :font, :bgcolor))
if @user.save
redirect_to '/static_pages/home'
else
flash[:notice] = "Developers to be blamed !!"
end
end
end


When I am submitting this form I am getting the error as below:



param is missing or the value is empty: user


Notes: routes.rb looks like this



devise_for :users, controllers: { registrations: "users/registrations" }
devise_scope :user do
authenticated :user do
root :to => 'static_pages#home', as: :authenticated_root
end
unauthenticated :user do
root :to => 'devise/registrations#new', as: :unauthenticated_root
end
end


Please guide where I am doing mistake !!! and thanks in advance


Aucun commentaire:

Enregistrer un commentaire