pillaiindu

pillaiindu

Programming Phoenix error: function Routes.user_path/3 is undefined (module Routes is not available)

Hi Everyoen,

I am reading the book Programming Phoenix and I faced the following error when I wrote the code example up to page 45.

function Routes.user_path/3 is undefined (module Routes is not available)

I did may things, searched StackOverflow etc, but didn’t find any answer.

Somebody please help me!

Thank you.

Marked As Solved

Also Liked

peerreynders

peerreynders

In 1.4

https://github.com/phoenixframework/phoenix/blob/master/installer/templates/phx_single/lib/app_name_web.ex#L26

and

https://github.com/phoenixframework/phoenix/blob/master/installer/templates/phx_single/lib/app_name_web.ex#L43

vs. 1.3

phoenix/installer/templates/phx_single/lib/app_name_web.ex at v1.3 · phoenixframework/phoenix · GitHub

and

phoenix/installer/templates/phx_single/lib/app_name_web.ex at v1.3 · phoenixframework/phoenix · GitHub

So you are going to encounter this with all the helpers defined by Phoenix.Router.Helpers like page_path, page_url,user_path, user_url, session_path, session_url, etc.

For other differences check the Change Log 1.4.0-dev.

And p.38 would have probably been the perfect time to replace Brunch with Webpack.

More or less …

In rumbl/assets:

$ rm brunch-config.js
$ npm un babel-brunch clean-css-brunch uglify-js-brunch brunch
$ npm i -D webpack@^4.4.0 webpack-cli@^2.0.10 babel-core@^6.26.0 babel-loader@^7.1.3 babel-preset-env@^1.6.1 copy-webpack-plugin@^4.5.0 css-loader@^0.28.10 mini-css-extract-plugin@^0.4.0 optimize-css-assets-webpack-plugin@^1.2.4 uglifyjs-webpack-plugin@^1.2.4

Update scripts in rumbl/assets/package.json:

{
  "repository": {},
  "license": "MIT",
  "scripts": {
    "deploy": "webpack --mode production",
    "watch": "webpack --mode development -watch"
  },
  "dependencies": {
    "phoenix": "file:../deps/phoenix",
    "phoenix_html": "file:../deps/phoenix_html"
  },
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.4",
    "babel-preset-env": "^1.7.0",
    "copy-webpack-plugin": "^4.5.1",
    "css-loader": "^0.28.11",
    "mini-css-extract-plugin": "^0.4.0",
    "optimize-css-assets-webpack-plugin": "^1.3.2",
    "uglifyjs-webpack-plugin": "^1.2.5",
    "webpack": "^4.9.1",
    "webpack-cli": "^2.1.4"
  }
}

Create rumbl/assets/webpack.config.js:

const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = (env, options) => ({
  optimization: {
    minimizer: [
      new UglifyJsPlugin({ cache: true, parallel: true, sourceMap: false }),
      new OptimizeCSSAssetsPlugin({})
    ]
  },
  entry: './js/app.js',
  output: {
    filename: 'app.js',
    path: path.resolve(__dirname, '../priv/static/js')
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader'
        }
      },
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader']
      }
    ]
  },
  plugins: [
    new MiniCssExtractPlugin({ filename: '../css/app.css' }),
    new CopyWebpackPlugin([{ from: 'static/', to: '../' }])
  ]
});

Create rumbl/assets/.babelrc:

{
    "presets": [
        "env"
    ]
}

Replace rumbl/assets/js/app.js:

// We need to import the CSS so that webpack will load it.
// The MiniCssExtractPlugin is used to separate it out into
// its own CSS file.
import css from "../css/app.css"

// webpack automatically bundles all modules in your
// entry points. Those entry points can be configured
// in "webpack.config.js".
//
// Import dependencies
//
import "phoenix_html"

// Import local files
//
// Local files can be imported directly using relative
// paths "./socket" or full ones "web/static/js/socket".

// import socket from "./socket"

Replace rumbl/assets/css/app.css

/* This file is for your main application css. */

@import "./phoenix.css";

Replace rumbl/assets/css/phoenix.css with contents from
https://raw.githubusercontent.com/phoenixframework/phoenix/master/installer/templates/phx_assets/phoenix.css

Modify rumbl/config/dev.exs:

  watchers: [node: ["node_modules/webpack/bin/webpack.js", "--mode", "development", "--watch-stdin",
                    cd: Path.expand("../assets", __DIR__)]]

Replace rumbl/lib/rumbl_web/templates/layout/app.html.eex:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, shrink-to-fit=no, user-scalable=no"/>
    <title>Hello Rumbl!</title>
    <link rel="stylesheet" href="<%= static_path(@conn, "/css/app.css") %>">
  </head>

  <body>
    <header>
      <section class="container">
        <nav role="navigation">
          <ul>
            <li><a href="http://www.phoenixframework.org/docs">Get Started</a></li>
          </ul>
        </nav>
        <a href="http://phoenixframework.org/" class="phx-logo">
          <img src="<%= static_path(@conn, "/images/phoenix.png") %>" alt="Phoenix Framework Logo"/>
        </a>
      </section>
    </header>
    <main role="main" class="container">
      <p class="alert alert-info" role="alert"><%= get_flash(@conn, :info) %></p>
      <p class="alert alert-danger" role="alert"><%= get_flash(@conn, :error) %></p>
      <%= render @view_module, @view_template, assigns %>
    </main>
    <script src="<%= static_path(@conn, "/js/app.js") %>"></script>
  </body>
</html>
kokolegorille

kokolegorille

Try to remove Routes.

It might be a typo

Update: Or a new feature of version 1.4 :slight_smile:

alexandrubagu

alexandrubagu

If you take a look at this video, the Routes.<some>_path will be used in phoenix 1.4

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40042 209
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement