How to render js template from module included in controller?
actioncontroller, module, respond-to, ruby-on-rails, templates
Solution
module SocionicsVotesConcern
extend ActiveSupport::Concern
included do
def vote_socionics
respond_to do |format|
format.js { render 'shared/vote_socionics' }
end
end
end
end
Wrap any actions/methods you define in the concern in an `included do` block. This way, anything in the block will be considered as if it was directly written in the includer object (i.e. the controller you're mixing this into)
With this solution, there are no loose ends, no idiosyncracies, no deviations from rails patterns. You will be able to use `respond_to` blocks, and won't have to deal with weird stuff.
Problem
I have an action in a controller concern, which gets included in a controller. This action does not render a js.erb file as specified under a respond_to block. How do I properly get an action in a controller concern to successfully render a js.erb file (or any view, for that matter)? Is it a problem with my routes? The link for the module action ``` = link_to image_tag("upvote.png"), send("vote_socionics_#{votable_name}_path", votable, vote_type: "#{s.type_two_im_raw}"), id: "vote-#{s.type_two_im_raw}", method: :post, remote: true ``` ** The link for the controller action** ``` = link_to "whatever", characters_whatever_path, remote: true ``` controllers/characters_controller.rb ``` class CharactersController < ApplicationController include SocionicsVotesConcern def an_action respond_to do |format| format.js { render 'shared/vote_socionics' } # This renders/executes the file end end ``` controllers/concerns/socionics_votes_concern.rb ``` module SocionicsVotesConcern extend ActiveSupport::Concern def vote_socionics respond_to do |format| format.js { render 'shared/vote_socionics' } # This DOES NOT render/execute the file. Why? end end end ``` views/shared/whatever.js.erb ``` # js code that executes ``` routes.rb ``` concern :socionics_votes do member do post 'vote_socionics' end end resources :universes resources :characters, concerns: :socionics_votes resources :celebrities, concerns: :socionics_votes resources :users, concerns: :socionics_votes ```