Giter VIP home page Giter VIP logo

Comments (8)

apotonick avatar apotonick commented on June 7, 2024

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.

giates avatar giates commented on June 7, 2024

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.

apotonick avatar apotonick commented on June 7, 2024

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.

giates avatar giates commented on June 7, 2024

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.

matisojka avatar matisojka commented on June 7, 2024

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.

apotonick avatar apotonick commented on June 7, 2024

Fantastic example for using cells, draper and flash messages, @mateusz-sojka - we should start screencasting about Cells!

from cells.

matisojka avatar matisojka commented on June 7, 2024

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.

apotonick avatar apotonick commented on June 7, 2024

Discussed here #136

from cells.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.