strong parameters permit all attributes for nested attributes

The only situation I have encountered where permitting arbitrary keys in a nested params hash seems reasonable to me is when writing to a serialized column. I’ve managed to handle it like this: class Post serialize :options, JSON end class PostsController < ApplicationController … def post_params all_options = params.require(:post)[:options].try(:permit!) params.require(:post).permit(:title).merge(:options => all_options) end end try … Read more

Difference between Render and Render Partial and Yield

render & render partial: render ‘some_view’ is a shorthand for render partial: ‘some_view’. render file: ‘view’ will look for a file view.html.erb and NOT _view.html.erb (.erb or any other renderer you use) render can pass local variables for the partial if you do not use collections or layouts, like render ‘some/path/to/my/partial’, custom_var: ‘Hello’ Passing local … Read more

Ruby on Rails Best practices – Big Controller vs Small Controller

This is more of a long-form comment, since it explains the origin of your dilemma, but provides no solutions. The problem actually is caused by misinterpretation of MVC, which RoR popularizes. It is the combination of two factors, which are causing this implosion of controller: On the one side you have anemic model, because, instead … Read more

How to get a Date from date_select or select_date in Rails?

Using date_select gives you 3 separate key/value pairs for the day, month, and year respectively. So you can pass them into Date.new as parameters to create a new Date object. An example date_select returned params for an Event model: “event”=> {“name”=>”Birthday”, “date(1i)”=>”2012”, “date(2i)”=>”11”, “date(3i)”=>”28”}, Then to create the new Date object: event = params[:event] date … Read more

Using send_file to download a file from Amazon S3?

You can also use send_data. I like this option because you have better control. You are not sending users to s3, which might be confusing to some users. I would just add a download method to the AttachmentsController def download data = open(“https://s3.amazonaws.com/PATTH TO YOUR FILE”) send_data data.read, filename: “NAME YOU WANT.pdf”, type: “application/pdf”, disposition: … Read more