README.md in inherited_resources-1.4.1 vs README.md in inherited_resources-1.5.0

- old
+ new

@@ -50,13 +50,11 @@ ## HasScope Since Inherited Resources 1.0, has_scope is not part of its core anymore but a gem dependency. Be sure to check the documentation to see how you can use it: -``` -http://github.com/plataformatec/has_scope -``` +- <http://github.com/plataformatec/has_scope> And it can be installed as: ```sh $ gem install has_scope @@ -67,13 +65,11 @@ Since Inherited Resources 1.0, responders are not part of its core anymore, but is set as Inherited Resources dependency and it's used by default by InheritedResources controllers. Be sure to check the documentation to see how it will change your application: -``` -http://github.com/plataformatec/responders -``` +- <http://github.com/plataformatec/responders> And it can be installed with: ```sh $ gem install responders @@ -164,11 +160,11 @@ One reason to use the `inherit_resources` macro would be to ensure that your controller never responds with the html mime-type. `InheritedResources::Base` already responds to `:html`, and the `respond_to` macro is strictly additive. Therefore, if you want to create a controller that, for example, responds ONLY via `:js`, -you will have write it this way: +you will have to write it this way: ```ruby class AccountsController < ApplicationController respond_to :js inherit_resources @@ -264,11 +260,11 @@ end end ``` Since most of the time when you change a create, update or destroy -action you do so because you want to to change its redirect url, a shortcut is +action you do so because you want to change its redirect url, a shortcut is provided. So you can do: ```ruby class ProjectsController < InheritedResources::Base def destroy @@ -313,10 +309,22 @@ ``` Yes, it's that simple! The nice part is since you already set the instance variable `@project`, it will not build a project again. +Same goes for updating the project: + +```ruby +class ProjectsController < InheritedResources::Base + def update + @project = Project.find(params[:id]) + @project.something_special! + update! + end +end +``` + Before we finish this topic, we should talk about one more thing: "success/failure blocks". Let's suppose that when we update our project, in case of failure, we want to redirect to the project url instead of re-rendering the edit template. Our first attempt to do this would be: @@ -636,14 +644,13 @@ ## What about views? Sometimes just DRYing up the controllers is not enough. If you need to DRY up your views, check this Wiki page: -``` https://github.com/josevalim/inherited_resources/wiki/Views-Inheritance -``` + Notice that Rails 3.1 ships with view inheritance built-in. ## Some DSL For those DSL lovers, InheritedResources won't leave you alone. You can overwrite @@ -676,29 +683,43 @@ def permitted_params params.permit(:widget => [:permitted_field, :other_permitted_field]) end ``` +Remember that if your field is sent by client to server as an array, you have to write `:permitted_field => []`, not just `:permitted_field`. + Note that this doesn't work if you use strong_parameters' require method instead of permit, because whereas permit returns the entire sanitized parameter hash, require returns only the sanitized params below the parameter you required. If you need `params.require` you can do it like this: ```ruby def permitted_params - {:widget => params.require(:widget => [:permitted_field, :other_permitted_field])} + {:widget => params.fetch(:widget, {}).permit(:permitted_field, :other_permitted_field)} end ``` Or better yet just override `#build_resource_params` directly: ```ruby def build_resource_params - [params.require(:widget => [:permitted_field, :other_permitted_field])] + [params.fetch(:widget, {}).permit(:permitted_field, :other_permitted_field)] end ``` + + +Instead you can stick to a standard Rails 4 notation (as rails scaffold generates) and write: + +```ruby +def widget_params + params.require(:widget).permit(:permitted_field, :other_permitted_field) +end +``` + +In such case you should remove #permitted_params method because it has greater priority. + ## Bugs and Feedback If you discover any bugs, please describe it in the issues tracker, including Rails and InheritedResources versions.