Git Product home page Git Product logo

strong_parameters's Introduction

This sample will cover about Strong Paramenter ##What I did

$ rails new strong_parameters
$ cd strong_parameters
$ rails g scaffold Product name:string description:string category_id:integer
$ rails g scaffold Category name:string
# and wrote some code...

##Setup

$ git clone https://github.com/kienbt01359/strong_parameters.git
$ cd strong_parameters
$ rake db:create
$ rake db:migrate
$ rails s

##strong_parameters with single table ###Model app/model/product.rb

class Product < ActiveRecord::Base
end  

###Controller app/controllers/products_controller.rb

before_action :set_product, only: [:show, :edit, :create, :update]              # Rails 4 

before_filter :set_product, only: [:show, :edit, :create, :update] if you're using Rails 3

.
.
.

 private                                                                                                                                                                                                    
 # Use callbacks to share common setup or constraints between actions.                                                                                                                                     
 def set_product                                                                                                                                                                                          
   @product = Product.find(params[:id])                                                                                                                                                                   
 end                                                                                                                                                                                                      
                                                                                                                                                                                                          
 # Never trust parameters from the scary internet, only allow the white list through.                                                                                                                     
 def product_params                                                                                                                                                                                       
   params.require(:product).permit(:name, :description)   #Permit which column you want to add to database, 
                                                          #value is nil when you not permit some column.                                                                                                                                                      
 end 

##strong_parameters with accept_nested_attributes ###Model ####Parent model app/model/category.rb

class Category < ActiveRecord::Base
  has_many :products, dependent: :destroy
  accepts_nested_attributes_for :products 
end

####Children model app/model/product.rb

class Product < ActiveRecord::Base                                                                                                                                                                              
  belongs_to :category                                       
end  

####Controller app/controllers/categoires_controller.rb

before_action :set_category, only: [:show, :edit, :update, :destroy]
def new
  @category = Category.new
  3.times {@category.products.build}         # For example 1 category has 3 products 
end
.
.
.
private
# Use callbacks to share common setup or constraints between actions.
def set_category
  @category = Category.find(params[:id])
end
def category_params
  params.require(:category).permit(:name, products_attributes: [:id,:name])  #Add `id` to use when edit product records
end

####View app/views/categoires/_form.html.erb

.
.
<div class="field">
    <%= f.label :name, "Category Name:" %><br>
    <%= f.text_field :name %>
  </div>
  <%= f.fields_for :products do |pr| %>
    <p>
      <%= pr.label :name, "Product Name:" %>
      <%= pr.text_field :name %>
      <%= pr.hidden_field :id %>        #Using when edit, to identify record is editing. Not create new record
    </p>
  <% end %>
.
.

###References http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html
http://railscasts.com/episodes/196-nested-model-form-part-1?view=asciicast

strong_parameters's People

Watchers

James Cloos avatar Bui Trung Kien avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.