Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

view variants support #2578

Open
hksk opened this issue May 4, 2024 · 3 comments
Open

view variants support #2578

hksk opened this issue May 4, 2024 · 3 comments
Labels
bug breakages in functionality that is implemented

Comments

@hksk
Copy link

hksk commented May 4, 2024

  • What were you trying to do?
    add a view variant, example:
$ ls -lah app/views/admin/courses
drwxr-xr-x  7 casa  staff   224B May  4 02:17 .
drwxr-xr-x  4 casa  staff   128B May  4 01:00 ..
-rw-r--r--  1 casa  staff   2.0K May  4 02:38 show+student.html.erb
-rw-r--r--  1 casa  staff   2.0K May  4 02:38 show.html.erb
  • What did you end up with (logs, or, even better, example apps are great!)?
    currently in the main controller, cant pass a variant, seems variant: :value not works
# main controller
    def show
      render locals: {
        page: Administrate::Page::Show.new(dashboard, requested_resource)
      }
    end

so I implemented something like this in my generated controller

# generated controller
    def show 
      variant = ""
      if current_user.is_student
        variant = "+student"
      end
      page = Administrate::Page::Show
      render "show#{variant}", locals: {
          page: Administrate::Page::Show.new(dashboard, requested_resource)
      }
    end

I guess we can add some like

# main controller
    def show
     variant = ""
     if !request.variant.nil? 
       variant = "+#{request.variant.to_s}"
     end
      render render "show#{variant}"locals: {
        page: Administrate::Page::Show.new(dashboard, requested_resource)
      }
    end
# generated controller
    def show
      request.variant = :student
      super
    end
@hksk hksk added the bug breakages in functionality that is implemented label May 4, 2024
@nickcharlton
Copy link
Member

I'm not familiar with view variants. Is this a Rails feature we're not supporting? If so, could you link to the docs so we can plan out how to support it?

@Thrizian
Copy link

I'm not familiar with view variants. Is this a Rails feature we're not supporting? If so, could you link to the docs so we can plan out how to support it?

https://guides.rubyonrails.org/layouts_and_rendering.html#the-variants-option

@goosys
Copy link
Contributor

goosys commented Oct 30, 2024

@Thrizian
I didn’t know about variant —this was really helpful to learn!

@hksk
I tried it out briefly, and it worked without needing any additional modifications.
Here’s an example of how it’s used:

touch app/views/admin/courses/show.html+student.erb
module Admin
  class CoursesController < Admin::ApplicationController
    before_action :with_variant, only: %i[show]
    private def with_variant
      if SOME_CONDITION
        request.variant = :student
      end
    end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug breakages in functionality that is implemented
Projects
None yet
Development

No branches or pull requests

4 participants