Skip to content

Instantly share code, notes, and snippets.

View raysrashmi's full-sized avatar
🏠
Working from home

Rashmi Yadav raysrashmi

🏠
Working from home
View GitHub Profile
class BooksController < ApplicationController
def create
@book = Book.new(params[:book], as : current_user.try(:admin) ? :admin : :user)
respond_to do |format|
if @book.save
format.html { redirect_to @book notice 'Book was successfully created.' }
else
format.html { render action : "new" }
end
end
class Book < ActiveRecord::Base
attr_accessible :name, :author, :public as : :admin
attr_accessible :name, :author as : :user
attr_accessible :name, as : :reporting_user
end
config.active_record.whitelist_attributes = true
gem 'strong_parameters'
class Book < ActiveRecord::Base
include ActiveModel::ForbiddenAttributesProtection
end
class BooksController < ApplicationController
def create
@book = Book.new(book_params)
respond_to do |format|
if @book.save
format.html { redirect_to @book, notice : 'Book was successfully created.' }
else
format.html { render action : "new" }
end
params.require(:book).permit(:name,:author,:public)
params[:book].permit(:name,:author,:chapters_attributes => [:number_of_pages])
def user_params
params.require(:user).permit(:name, :email)
end
params[:user] = {:name => 'Foo',:email => '[email protected]'}
@user = User.new(params[:user])