Comments (8)
Hey, I remember you! You are "RE-trying" cells? Does that mean you didn't like it in the first place?????????????????
You can access flash using parent_controller.flash
. However, the flash object is intentionally hidden from cells as they usually should not access global variables like the flash message :-/
Please report if that works for you!
Nick
from cells.
I solved it.
Into the cell controller I've put:
def flashes
@flash = parent_controller.flash
render
end
and into the view I use @flash instead of flash.
Many thanks for your response.
from cells.
Yeah, the problem is people expect Cells to be like ActionController, which is an all-mighty monster. Nevertheless, Cells are "only" view components, and hence they aren't supposed to do everything.
You could also try
helper_method :parent_controller
to access parent_controller in your view. The same applies for your fix, just wrap it in a def flash
and declare it as helper method.
Good to hear you're still not too encouraged ;-)
from cells.
Now my project is fully using rails cells and it is very modular.
Many thanks for your support and congratulations for the wonderful gem !!!
from cells.
First of all, @apotonick , I want to say thank you for this magnific tool, it's just a little big revolution to my personal way of developing in Rails. I've just started a new project, using cells so I can find my way through it and plan the code structure from beginning.
Now, lets get to @giates question.
First of all, as I started using cells, I wanted to do everything the "old school" style: flash messages, global helpers, some logic in views... Then I realized that what I need is to do stuff different so it fits this new approach.
About flash messages. In my project I use the Decorator patttern by using the draper(https://github.com/jcasimir/draper) gem. It helps to assign "helper" methods in a more OOP way. So I did with the flash messages, which now rely on a concrete class.
app/controllers/foos_controller.rb
def create
@foo = Foo.new(params[:foo])
begin
@foo.save!
flash[:notice] = "Created successfully!"
redirect_to :controller => "foos", :action => "index"
rescue Mongoid::Errors::Validations
render :new
end
end
- notice there is no flash[:error] used here, we'll come to it later!
app/cells/foo_cell.rb
def flash_messages(foo)
@foo = FooDecorator.decorate(foo)
render
end
app/decorators/application_decorator.rb
class ApplicationDecorator < Draper::Base
def errors
model && model.errors || {}
end
def notice
h.flash[:notice] if h.flash
end
- notice that I am using the
ActiveModel
"errors" method to show the errors.
So this way, I just render a cell above my pages so they can show the flash messages:
<%= render_cell :foo, :flash_messages, @foo %>
Maybe it is not the best approach, but it works for me very nicely. Actually, as I am writing this lines, I realize that an application_cell.rb
would be a nice place to put the flash_messages
cell so it can be reused in the whole application :)
from cells.
Fantastic example for using cells, draper and flash messages, @mateusz-sojka - we should start screencasting about Cells!
from cells.
I'm glad to here you like it :)
The idea about screencasting is great! I think it could boost the popularity of the gem and make the voice spread.
BTW: I voted you at Ryan Bates site, I hope you get a screencast :)
from cells.
Discussed here #136
from cells.
Related Issues (20)
- Cells + Airbrake issue - double exceptions raised HOT 2
- Using Cells with Rails engine that has html.erb partials not being picked up HOT 5
- Confusing file structure - views or view? HOT 7
- Template Missing ... again! HOT 21
- how to render erb templates when haml/hamlit and erb extensions installed HOT 3
- Render an array inside view cell HOT 1
- Escaping implementation seems to be a glaring LAYER VIOLATION, methinks.
- Is there some way to do html escaping default with options?
- Publish latest version on Rubygems HOT 10
- should default to file_name.html.erb or .haml HOT 1
- Can't get form_with to work from a cell in Rails 6 HOT 3
- License HOT 1
- Form Input renders as raw text in cell HOT 1
- Feature Request: Support Recyclable Cache Key
- Still maintained? Documentation URLs broken. HOT 9
- Default cache key produces clashes HOT 4
- Options hash is same instance passed to each cell when using collections HOT 1
- Slim v5 breaks cells HOT 9
- Ruby 3.0 ArgumentError: wrong number of arguments error with keyword args for Cell::Testing
- Ruby 3.0 ArgumentError: wrong number of arguments error with Caching
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 cells.