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

About the author

Being the CEO and Founder of ClecoTech International, Mr. Ashish Prajapati is dedicated towards his aim of mentoring young startups into a full-fledged businesses. He is helping startups from America, Europe, India, and various other countries through proper guidance and the use of latest technologies to develop their innovation and ideas into definite realities.

Comments

Leave a Reply

Your email address will not be published.