mercredi 22 avril 2015

Is this the correct way of defining forms in rails

Is this the correct way of defining form, let me know if I need to provide any more details.

This is the UserPreference forms in new.html.erb

<%= form_for :user_preference, url: user_preferences_path do |u|%>
     <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>

I am getting title and description when I am trying to render in html, the attribute is not getting updated in database.

UserPreference.controller.rb

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

    def create
        @user_preference = UserPreference.new(userp_params)
        @user_preference.save unless user_signed_in?
        render plain: params[:user_preference].inspect
    end
    def edit
    end
    def update
    end

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

Aucun commentaire:

Enregistrer un commentaire