Comments (14)
Note: In the cell view I need to call current_account.
from cells.
Neither this solves the problem: render :locals => {:current_account => current_account}
from cells.
hey grimen,
it's important to understand that controller != cell. if you define a helper_method in a controller, it is only available in the controller's views, not in the cell views- otherwise we would break proper encapsulation.
when using #helper_method in a cell this method must also exist there. note that you could also use #helper to include files in a cell.
from cells.
Right, but a cell is rather a "child" of a controller as a controller render a view that render one or many cells. It's actually also a bit of a problem that cells dont' support controller helpers, as all? authentication solutions defines helpers in controller. This code-duplication make cells bittersweet I must say. I would suggest looking on controller helpers as a "parent".
from cells.
not sure what you mean with "cells don't support controller helpers"...? are you sure you tried #helper in a cell? that should work and is tested thoroughly. any helper that works in a controller should work in a cell view as well.
did you experience problems here?
i speak against some magic helper inclusion where cells would "inherit" helpers from controllers- cells should never know anything about there parent controller to assure clean encapsulation.
imagine such a cell being plugged into another controller B but relying on a mysterious helper method from controller A. this would cause a name error which would be hard to trace, apart from evil dependencies. what do you think, grimen?
from cells.
Yes, but helper won't work because I got loads of helpers that belongs to - and is defined in - controllers, but is used as helpers as well with "helper_method :my_method_1, :my_method_2, ... A pattern used in most plugins I'm aware of, e.g. Devise, Facebooker, etc. As I see it, even if Cells is excellent as a philosophy and plugin; Cells needs to support these globally available controller helpers somehow, otherwise it'll be more pain than gain to use Cells. Btw, helper_methods are usually declared for ActionController::Base so if these helpers could be included somehow.
from cells.
Update:
Here's how helper_method works under the hood:
http://www.neeraj.name/blog/articles/718-how-helper_method-brings-methods-to-view-level
I tried to access them using ActionController::Base.helpers.{method}, but for some reason this don't work either. Will continue see if I can solve this...
from cells.
hi grimen!
thanks for the link.
go and try the following:
class MyCell < Cell::Base
helper WhatEver
helper_method :method_from_whatever
that should really work (has been doing the job for 4 years now ;-)
does that help?
cheers! nick
from cells.
I tried that already, but as already described above many helpers are not defined in helpers, rather in controller but added to the master template as helpers (helper modules). If you look into the the article I posted above you see that your code suggestions cannot work for helpers defined in controllers. ActionController::Base.helpers.{method} should work, but there seems to be a Rails bug which I filed in the Rails issue tracker. Anyway, that bug is not the reason for why these helpers are not available in cells.
from cells.
Example of controller helpers:
http://github.com/plataformatec/devise/blob/master/lib/devise/controllers/helpers.rb
from cells.
grimen! thanks for your post, i start to grasp what you mean :-)
the problem is that those plugins are for controllers only- no cells in mind! the simplest solution i see would be (in your example)
cell MyCell < Cell::Base
include Devise::Controllers::Helpers
i really don't see any way to automatically distinguish what in a controller is a helper and what not, so we cannot provide a generic mechanism for that in cells. you would have to go by foot here. and plugin developers would have to mix in their code in ActionController::Base and in Cell::Base :-(
from cells.
That was just a sample, so won't change much I'm afraid. OK, well that won't happen (other developers to mixin stuf finto Cell::Base) I'm sure unless Cells gets merged into Rails. Thanks for the follow ups though. I'll let u know if I find a best practice on my own to get around it.
from cells.
maybe there IS a way to handle that! we could extract all helper methods from the controller's MasterHelperModule and mix those methods in the cell's view.
however, i wouldn't do that by default- rather you would have to turn on that evil thing explicitly, like
render_cell :grimen_cell, :shout, :mixin_controller_helpers => true
from cells.
My solution/hack that works (but discussable why):
app/helpers/cells_hacks_helper.rb:
module CellsHacksHelper def method_missing(method, *args) controller.send(method, *args) rescue super(method, *args) end end
app/cells/some_cell.rb:
class SomeCell < Cell::Base helper :cells_hacks # ... end
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.