Posts

Showing posts with the label ruby-on-rails-5

Using “ApplicationController.new.render_to_string” in a Rails api app (config.api_only = true)

Using “ApplicationController.new.render_to_string” in a Rails api app (config.api_only = true) I have a Rails 5 app build as an api app: # config/application.rb module MyApp class Application < Rails::Application config.api_only = true end end In one of my controller actions I need to render some Ruby flavoured html, and then serve it back in the json response. A standard Rails app do this: # app/controllers/my_controller.rb def my_action html = ApplicationController.new.render_to_string("my_erb_or_haml_file", locals: {foo: "bar"}, layout: false) render :json => {html: html}, :status => :ok end However, for my slimmed api app ApplicationController.new.render_to_string seems to just render " " . This confuses me, because I would expect it to either serve the content or raise an exception. ApplicationController.new.render_to_string " " Any suggestions what route I should take in order to generate Ruby flavoured html? This ques...

Finding posts under a category name keyword with pg_search on Rails 5

Finding posts under a category name keyword with pg_search on Rails 5 I'm using gem pg_search and doing multi-search for Website and Post tables. I am able to search by records title, but I also want to be able to search records based on categories they belong to. pg_search So, for example, when I type "typographic" (category name) to search input, all Website and Post records under that category should be listed, as well as all records that have "Typographic" in the title. I think it can be done with association but I could not make it work 🤷‍♂️ _header.html.erb <%= form_tag search_index_path, method: :get do %> <%= text_field_tag :query, params[:query], placeholder: "Search" %> <% end %> search_controller.rb class SearchController < ApplicationController def index @pg_search_result = PgSearch.multisearch(params[:query]) end end models/post.rb class Post < ApplicationRecord include PgSearch multisearchable :ag...