Skip to content

Undefined method `default_url_options' when using Component and Phlex::Rails #767

Answered by joeldrapper
LolsonX asked this question in Help
Discussion options

You must be logged in to vote

Oh, I know what it is! We should really call this out in the guides. You can’t use helpers from the initializer, because the initializer happens when the component is created, not when it’s rendered. We don’t have a rendering context at that point.

If we write the controller action like this, it’s more apparent.

component = Articles::NewView.new(
  article: Article.new
)

# the initializer has run at this point, but it doesn’t have a rendering context

render(component)

# now it has a rendering context but it’s too late

The solution is to move that logic out.

class Articles::FormComponent < ApplicationComponent
  attr_reader :article, :action, :method

  def initialize(article:)
    @art…

Replies: 4 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@willcosgrove
Comment options

Answer selected by joeldrapper
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
3 participants