Home > Enterprise >  Rails link do not work in Trailbalzer::cell
Rails link do not work in Trailbalzer::cell

Time:01-13

I am using cell to render Navigation Bar for current_user devise. But went i try to render page i get an error. I am using rails 7 and ruby 3.0.2

Error

undefined method `new_user_session_path' for #<Navigation::Cell::Show:0x00007fac566e3570 @model=#<User id: 1, email: "[email protected]", first_name: "Otto", last_name: "Otto", phone: nil, birthday: nil, gender: nil, city: nil, street: nil, house: nil, apartment: nil, created_at: "2023-01-07 18:10:10.925669000  0000", updated_at: "2023-01-07 18:30:10.207258000  0000">, @options={}>

My application.html.erb

<body>
    <%= Navigation::Cell::Show.(current_user) %>

    <%= yield %>
</body>

Gemfile

gem 'trailblazer-cells', '~> 0.0.3'
gem 'trailblazer-rails', '~> 2.4', '>= 2.4.3'
gem 'cells-erb', '~> 0.1.0'

Navigation::Cell:Show (app/concepts/navigation/cell/show.rb)

class Navigation::Cell::Show < Trailblazer::Cell
  include Devise::Controllers::Helpers
  include ActionView::Helpers::UrlHelper
  include Cell::Erb

  def show
    render
  end

  private

  def username
    "#{model.first_name} #{model.last_name}"
  end

  def current_user
    model
  end

  def profile_link
    link_to username, new_user_session_path(current_user.id)
  end

  def logout_link
    link_to 'Log Out', destroy_user_session_path, method: :delete
  end

  def login_link
    link_to 'Login', new_user_session_path
  end

  def registration_link
    link_to 'Registration', new_user_registration_path
  end
end

And my view (app/concepts/navigation/view/show.erb)

<nav>
  <div>
    <% if current_user %>
      <h1><%= profile_link %></h1>
      <%= logout_link %>
    <% else %>
      <%= login_link %>
      <%= registration_link %>
    <% end %>
  </div>
</nav>

I try to include include ActionView::Helpers::UrlHelper it didn`t work.

When i add gem gem 'cells-rails' to rails app i have another error that looks like this

error

CodePudding user response:

Your error

undefined method `new_user_session_path'

This method is from routes URL helpers (named route helper)

You need this line additionally:

include Rails.application.routes.url_helpers
  • Related