Comments (7)
There is no way to pass serialization options, but you don't need to overwrite your whole action, just the required pieces.
And if you have always to pass the same options, you should also consider overwriting your models or creating your own responder.
from inherited_resources.
It's not always the same options, the options depend on parameters passed in by the user. I'm assuming that by overwriting "the required pieces" you mean like this:
def index
index! do |format|
format.xml { render :xml => collection.to_xml(to_xml_parameters) }
end
end
Is that correct? It kind of sucks to have to repeat myself in all my controllers that way; perhaps a responder is a better choice...
The stuff under legacy hurts my brain :-). Do I need to create an ActionController::Responder or an ActionController::MimeResponds::Responder?
from inherited_resources.
If you need to add the same line in all controllers, you can put the logic in a module and include the module in your controllers:
module ToXmlParameters
def index
index! do |format|
format.xml { render :xml => collection.to_xml(to_xml_parameters) }
end
end
def to_xml_parameters
# implement your logic here
end
end
And include this module in your controllers. If you want to go the responders road, I would suggest you to inherit from InheritedResources::Responder and add the logic you want. The ActionController::Responder is the one what you should look to know what to overwrite though.
When you are done, do:
InheritedResources::Base.responder = YourResponder
from inherited_resources.
Thanks for your help. I think the responder route is going to work well for me
from inherited_resources.
I too am finding that I am doing--or am about to do--the following in many controllers.
def index
index! do |format|
format.xml{ render :xml => collection.to_xml( render_options ) }
format.json{ render :json => collection.to_json( render_options ) }
end
end
def show
show! do |format|
format.xml{ render :xml => resource.to_xml( render_options ) }
format.xml{ render :json => resource.to_json( render_options ) }
end
end
mrroach, how did your Respondor solution work out? Care to share a snippet? Jose, these seems a fairly typical scenario. Would you accept a patch to do something about this in inherited_resources itself?
from inherited_resources.
The customization does not belong to Inherited Resources. It should be at the Responders level. Take a look at responders documentation and blog posts available. :)
from inherited_resources.
Jose,
I have attempted this at https://github.com/jpshackelford/resource_inclusion . I'll be interested in your feedback.
John-Mason
from inherited_resources.
Related Issues (20)
- resource_path unable to generate the correct path HOT 2
- how to include nested has_many association
- Drop support for Rails 4.2
- Maintaining has_scope and responders dependencies HOT 3
- Unable to autoload constant Base HOT 2
- update_attributes is deprecated in Rails 6
- Infinite loop if resource is called resource HOT 1
- Dependabot couldn't find a gems.rb for this project
- Dependabot couldn't find a gems.rb for this project
- Method missing crash due to duck typing in UrlHelper HOT 1
- Add an option to provide another superclass other than ApplicationController
- Breaking change in Rails 5.2.4.6 HOT 3
- How to use UrlHelpers outside of ApplicationController?
- `param` is not working with `belongs_to`
- Release a gem that supports rails 7 HOT 2
- Need to support ruby 3.2
- Tidelift badge and link do not work anymore
- `Create` & `Update` should return `status code != 200` for failed requests HOT 4
- Route-aware resource_path?
- Providing optional: true and shallow: true together produces an error
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from inherited_resources.