Permtting the Multiple Params Without Using Mutiple Condition

We can permit different type of model attributes without using the multiple condition

we normally using below code to permit attributes with multiple condition

  if params[:model]=="user"
  User.create(user_params)
 elsif params[:model]=="test"
  Test.create(test_params)
 elsif params[:model]=="post"
  Post.create(post_params)
 elsif params[:model]=="comment"
  Comment.create(comment_params) 
 elsif params[:model]=="reply"
  Reply.create(reply_params)
 end
 

here my code customize the mutiple  condition in one line
class HomeController < ApplicationController

 def save_data
   params[:model].camelcase.constantize.create(send("#{params[:model]}_params"))
   # if params[:model]=="user"
   #  User.create(user_params)
   # elsif params[:model]=="test"
   #  Test.create(test_params)
   # elsif params[:model]=="post"
   #  Post.create(post_params)
   # elsif params[:model]=="comment"
   #  Comment.create(comment_params)
   # elsif params[:model]=="reply"
   #  Reply.create(reply_params)
   # end
 end

 private 
 def user_params
 end


 def test_params
 end


 def post_params
 end


 def comment_params
 end

 def reply_params
 end

end

No comments:

Post a Comment