dimanche 28 avril 2019

Is there any way to combine similar html.erb file in views & actions in controller?

I am building a website by using ruby on rails. There is a country list. By clicking different country names , it will link to webs which show information related to this country. These webs are quite similar. Even more, people only have permission to country which they come from. Now, I create several html.erb files under 'views'->'country' folder, and several actions in 'Country' controller. It works, but I want to make codes more efficient. Is there any way to combine these similar html.erb files into one html.erb file and combine actions to one action in controller? Use 'if...else...'? Any suggestions are welcome and thankful.

enter image description here

   class CountryController < ApplicationController
before_action :authenticate_user!
before_action :check_admin

def index
    @users = User.all
end

def show
    @users = User.all
end

def france
    unless current_user.email == "test@france.com"
        flash[:alert] = "No permission!"
        redirect_to "http://localhost:3000/country.html"    
    end
end

def switzerland
    unless current_user.email == "test@switzerland.com"
        flash[:alert] = "No permission!"
        redirect_to "http://localhost:3000/country.html"    
    end
end

def china
    unless current_user.email == "test@china.com"
        flash[:alert] = "No permission!"
        redirect_to "http://localhost:3000/country.html"    
    end
end

protected
    end

expected result: combine similar html.erb files into one html.erb file and combine actions to one action in controller

Aucun commentaire:

Enregistrer un commentaire