The better way to pass the foreign_key value to the Rails controller

You may want to consider reading the Rails Guide on nested resources: http://guides.rubyonrails.org/routing.html#nested-resources In a nutshell: routes.rb resources :galleries do resources :pictures do end # Generates the routes: /galleries/:gallery_id/pictures pictures_controller.rb def new @gallery = Gallery.find(params[:gallery_id]) @picture = Picture.new end def create @gallery = Gallery.find(params[:gallery_id]) # gallery_id is passed in the URL @picture = @gallery.build(params[:picture]) if … Read more

file download link in rails

Rails 4: in routes: get “home/download_pdf” in controller (already have pdf): def download_pdf send_file( “#{Rails.root}/public/your_file.pdf”, filename: “your_custom_file_name.pdf”, type: “application/pdf” ) end in controller (need to generate pdf): require “prawn” class ClientsController < ApplicationController def download_pdf client = Client.find(params[:id]) send_data generate_pdf(client), filename: “#{client.name}.pdf”, type: “application/pdf” end private def generate_pdf(client) Prawn::Document.new do text client.name, align: :center text … Read more