MaxLeoTony
MaxLeoTony

Reputation: 67

Rails: Can't render a page that is routed?

So I routed a page called "classrooms#new". But it won't show up when I click the button that is routed to it. Here are my routes for the classroom: (Routes.rb)

get 'classrooms/index'
get 'classrooms/new'

Here is the button it's linked to: (Nav_link_for_auth.html.erb)

<li><%= link_to 'Create a Class', 'classrooms#new' %></li> 

The code in for this is in app/views/classrooms/new.html.erb

<section class="newclass_form">
    <%= render 'layouts/newclass_form' %>
</section>

Form URL:

<%= form_for :classroom, html: { multipart: true }, url: classrooms_new_path do |f| %>
<h2>Create a Class!</h2>
#additional code here

Here is what happens when I click to see it: enter image description here

There is a route for it, so why won't it show up? Thanks so much!

Upvotes: 0

Views: 42

Answers (1)

jvillian
jvillian

Reputation: 20263

I believe you want:

  <li><%= link_to 'Create a Class', 'classrooms/new' %></li>

If you look at the url in your browser screen shot, you'll see that

  <li><%= link_to 'Create a Class', 'classrooms#new' %></li>

yields:

  https://kidznotes-app-cynthiarios.c9users.io/classrooms#new

but you want:

  https://kidznotes-app-cynthiarios.c9users.io/classrooms/new

Also, I would suggest changing:

  get 'classrooms/index'
  get 'classrooms/new'

to:

  resources :classrooms

Then I would do:

  <li><%= link_to 'Create a Class', new_classroom_path %></li>

To link to Classrooms#index, you would do something like:

  link_to 'Classrooms', classrooms_path

Also, I think this is wrong:

<%= form_for :classroom, html: { multipart: true }, url: classrooms_new_path do |f| %>
  <h2>Create a Class!</h2>
  #additional code here

It will try to submit to classrooms_new_path, and you don't have that defined as a route. Instead, try something like:

<%= form_for :classroom, html: { multipart: true } do |f| %>
  <h2>Create a Class!</h2>
  #additional code here

Upvotes: 1

Related Questions