J.Danely
J.Danely

Reputation: 37

Ruby On Rails adding new page after scaffolding

I made a scaffold named b_page and I created a migration for bio I added a biopage.html.erb In controller:

def biopage
@b_pages = BPage.all
end

in routes.rb

resources :b_pages do  
collection do  
  get 'biopage'  
 end 
end 

in bio.html.erb:

<div class="jumbotron">
<h1>Bio of :</h1>
<h2><b><%= @b_page.Bpage_name %></b></h2>
<h3><%= @b_page.user.email %></h3>
</div>
<%= @b_page.bio %>

but i still get the error:

ActiveRecord::RecordNotFound in BPagesController#show Couldn't find BPage with 'id'=biopage highlighting:

      @b_page = BPage.find(params[:id])

Upvotes: 1

Views: 580

Answers (1)

DaniG2k
DaniG2k

Reputation: 4893

First of all, this seems a bit odd to me:

resources :b_pages do  
  collection do  
    get 'biopage'  
  end 
end

as it will result in a route like: /b_pages/biopage. You might want to just do something like:

resources :b_pages, except: :show
get '/biopage/:id', to: 'b_pages#show'

This way, your biopage route will go to the show controller method and you will still have the other b_pages routes to work with.

You are seeing the ActiveRecord::RecordNotFound error message because you have no BPage object to show, so the show method is complaining. Notice how the route I wrote above uses :id - this is because the show action normally takes an id of some record to display on the front end. If you want to use biopage and link it to the show method, you should be returning an object to actually show. Otherwise you should probably create a completely different controller method for biopage that does not interfere with the b_pages resources. Something like this in the routes:

resources :b_pages
get '/biopage/:id', to: 'b_pages#your_method'

and in the controller you'd have

class BPages < ApplicationController
  # index, show, destroy, etc. here

  def your_method
    # Get whatever object you want returned here
  end
end

Upvotes: 1

Related Questions