5 Replies Latest reply on Jun 13, 2013 3:44 AM by Ankur Srivastava

    Alter in Attributes of Model Class

      Hi All,

       

      I want to know, if I can alter attribute of the Model class after creating the model class. If I can insert more attribute afterward or have to create new model class for that. and if so, How can I proceed on this.

       

       

      Thanks in Advance!!

        • Re: Alter in Attributes of Model Class
          Kutir Mobility

          Hi Ankur Srivastava,

          Rhom currently supports two model types: Property Bag (default) and Fixed Schema

          Property bag doesn’t require specifying attributes. If you use property bag you can alter attribute and insert more attributes after creating the model.

          Fixed Schema has specifying attributes and can't alter and insert more attribute after creating the model.

           

          Thanks

          Surendran S

          Kutir Mobility

            • Re: Alter in Attributes of Model Class
              Mark Nongkhlaw

              Here's my product.rb (Uses Property bag) :

               

              # The model has already been created by the framework, and extends Rhom::RhomObject

              # You can add more methods here

              class Product

                include Rhom::PropertyBag

               

               

                # Uncomment the following line to enable sync with Product.

                enable :sync

               

               

                #add model specifc code here

              end

               

              Here's my Product Controller :

               

              require 'rho/rhocontroller'

              require 'helpers/browser_helper'

               

               

              class ProductController < Rho::RhoController

                include BrowserHelper

               

               

                #GET /Product

                def index

                  @products = Product.find(:all)

                  render

                end

               

               

                # GET /Product/{1}

                def show

                  @product = Product.find(@params['id'])

                  if @product

                    render :action => :show

                  else

                    redirect :action => :index

                  end

                end

               

               

                # GET /Product/new

                def new

                  @product = Product.new

                  render :action => :new

                end

               

               

                # GET /Product/{1}/edit

                def edit

                  @product = Product.find(@params['id'])

                  if @product

                    render :action => :edit

                  else

                    redirect :action => :index

                  end

                end

               

               

                # POST /Product/create

                def create

                  @product = Product.new(@params['product'])

                  @product.save

                  redirect :action => :index

                end

               

               

                # POST /Product/{1}/update

                def update

                  @product = Product.find(@params['id'])

                  @product.update_attributes(@params['product']) if @product

                  redirect :action => :index

                end

               

               

                # POST /Product/{1}/delete

                def delete

                  @product = Product.find(@params['id'])

                  @product.destroy if @product

                  redirect :action => :index

                end

              end

               

              Here's my new.erb :

               

              <div data-role="page">

               

               

                <div data-role="header" data-position="inline">

                  <h1>New <%= @product.title %></h1>

                  <a href="<%= url_for :action => :index %>" class="ui-btn-left" data-icon="back" data-direction="reverse">

                    Cancel

                  </a>

                </div>

               

               

                <div data-role="content">

                  <form method="POST" action="<%= url_for :action => :create %>">

                    <input type="hidden" name="id" value="<%= @product.object %>"/>

               

               

                   

                        <div data-role="fieldcontain">

                          <label for="product[name]" class="fieldLabel">Name</label>

                          <input type="text" id="product[name]" name="product[name]" <%= placeholder("Name") %> />

                        </div>

                   

                        <div data-role="fieldcontain">

                          <label for="product[price]" class="fieldLabel">Price</label>

                          <input type="text" id="product[price]" name="product[price]" <%= placeholder("Price") %> />

                        </div>

                   

                        <div data-role="fieldcontain">

                          <label for="product[quantity]" class="fieldLabel">Quantity</label>

                          <input type="text" id="product[quantity]" name="product[quantity]" <%= placeholder("Quantity") %> />

                        </div>

                   

                        <div data-role="fieldcontain">

                          <label for="product[sku]" class="fieldLabel">Sku</label>

                          <input type="text" id="product[sku]" name="product[sku]" <%= placeholder("Sku") %> />

                        </div>

                   

                        <div data-role="fieldcontain">

                          <label for="product[brand]" class="fieldLabel">Brand</label>

                          <input type="text" id="product[brand]" name="product[brand]" <%= placeholder("Brand") %> />

                        </div>

                   

               

               

                    <input type="submit" value="Create"/>

                  </form>

                </div>

               

               

              </div>

               

              In above, if I am to split, say Name attribute into First_Name and Last_Name, do I just change the ERBs?

            • Re: Alter in Attributes of Model Class

              Thanks All.. for clearing my doubts