Rails Undefined Method - NoMethodError -
i vaguely following http://guides.rubyonrails.org/getting_started.html, part 6.
using rails v 4.1.8 sqlite db
i error after adding second model:
nomethoderror in pages#show
showing m:/ict/rails/salacity/app/views/pages/show.html.erb line
24 raised:
undefined method `links' #
extracted source (around line #24)
links
<% @page.links.each |link| %>id: <%= link.child__id %>
rails.root: m:/ict/rails/salacity application trace | framework trace | full trace
app/views/pages/show.html.erb:24:in `_app_views_pages_show_html_erb___283878207_51684456'
this app/views/pages/show.html.erb
<p id="notice"><%= notice %></p> <p> <strong>page:</strong> <%= @page.page_id %> </p> <p> <strong>body:</strong> <%= @page.body %> </p> <p> <strong>branch:</strong> <%= @page.branch_id %> </p> <p> <strong>location:</strong> <%= @page.location_id %> </p> <h2>links</h2> <% @page.links.each |link| %> <p> <strong>id:</strong> <%= link.child__id %> </p> <p> <strong>body:</strong> <%= link.body %> </p> <% end %> <h2>add link:</h2> <%= form_for([@page, @page.links.build]) |f| %> <p> <%= f.label :page_id %><br> <%= f.text_field :page_id %> </p> <p> <%= f.label :child_id %><br> <%= f.text_area :child_id %> </p> <p> <%= f.label :order %><br> <%= f.number_field :order %> </p> <p> <%= f.label :body %><br> <%= f.text_area :body %> </p> <p> <%= f.submit %> </p> <% end %> <%= link_to 'edit', edit_page_path(@page) %> | <%= link_to 'back', pages_path %>
pagescontroller
class pagescontroller < applicationcontroller before_action :set_page, only: [:show, :edit, :update, :destroy] # /pages # /pages.json def index @pages = page.all end # /pages/1 # /pages/1.json def show @page = page.find(show_params) end # /pages/new def new @page = page.new end # /pages/1/edit def edit @page = page.find(params[:id]) end # post /pages # post /pages.json def create @page = page.new(page_params) respond_to |format| if @page.save format.html { redirect_to @page, notice: 'page created.' } format.json { render :show, status: :created, location: @page } else format.html { render :new } format.json { render json: @page.errors, status: :unprocessable_entity } end end end # patch/put /pages/1 # patch/put /pages/1.json def update respond_to |format| if @page.update(page_params) format.html { redirect_to @page, notice: 'page updated.' } format.json { render :show, status: :ok, location: @page } else format.html { render :edit } format.json { render json: @page.errors, status: :unprocessable_entity } end end end # delete /pages/1 # delete /pages/1.json def destroy @page = page.find(params[:id]) @page.destroy respond_to |format| format.html { redirect_to pages_url, notice: 'page destroyed.' } format.json { head :no_content } end end private # use callbacks share common setup or constraints between actions. def set_page @page = page.find(params[:id]) end # never trust parameters scary internet, allow white list through. def page_params params.require(:page).permit(:page_id, :body, :branch_id, :location_id) end def show_params params.require(:id) end end
linkscontroller
class linkscontroller < applicationcontroller before_action :set_link, only: [:show, :edit, :update, :destroy] # /links # /links.json def index @links = link.all end # /links/1 # /links/1.json def show end # /links/new def new @link = link.new end # /links/1/edit def edit end # post /links # post /links.json =begin def create @link = link.new(link_params) respond_to |format| if @link.save format.html { redirect_to @link, notice: 'link created.' } format.json { render :show, status: :created, location: @link } else format.html { render :new } format.json { render json: @link.errors, status: :unprocessable_entity } end end end =end def create @page = page.find(params[:page_id]) @link = @page.links.create(link_params) redirect_to page_path(@page) end # patch/put /links/1 # patch/put /links/1.json def update respond_to |format| if @link.update(link_params) format.html { redirect_to @link, notice: 'link updated.' } format.json { render :show, status: :ok, location: @link } else format.html { render :edit } format.json { render json: @link.errors, status: :unprocessable_entity } end end end # delete /links/1 # delete /links/1.json def destroy @link.destroy respond_to |format| format.html { redirect_to links_url, notice: 'link destroyed.' } format.json { head :no_content } end end private # use callbacks share common setup or constraints between actions. def set_link @link = link.find(params[:id]) end # never trust parameters scary internet, allow white list through. def link_params params.require(:link).permit(:page_id, :child_id, :order, :body) end end
class page has_many :links , class link belongs_to :page migrations links table exists in db not sure else look!
thanks in advance
edit: error
activerecord::statementinvalid in pages#show
showing m:/ict/rails/salacity/app/views/pages/show.html.erb line
24 raised:
sqlite3::sqlexception: no such column: links.page_id: select "links".* "links" "links"."page_id" = ?
extracted source (around line #24):
links
<% @page.links.each |link| %>id: <%= link.child__id %>
rails.root: m:/ict/rails/salacity application trace | framework trace | full trace
app/views/pages/show.html.erb:24:in `_app_views_pages_show_html_erb___283878207_50250936'
db/migrate/create_links.rb
class createlinks < activerecord::migration def change create_table :links |t| t.text :page_id t.text :child_id t.integer :order t.text :body t.references :page, foreign_key: true, index: true t.timestamps end end end
and schema.rb
# encoding: utf-8 # file auto-generated current state of database. instead # of editing file, please use migrations feature of active record # incrementally modify database, , regenerate schema definition. # # note schema.rb definition authoritative source # database schema. if need create application database on # system, should using db:schema:load, not running migrations # scratch. latter flawed , unsustainable approach (the more migrations # you'll amass, slower it'll run , greater likelihood issues). # # it's recommended check file version control system. activerecord::schema.define(version: 20150707195306) # not dump table "branches" because of following nomethoderror # undefined method `[]' nil:nilclass # not dump table "keywords" because of following nomethoderror # undefined method `[]' nil:nilclass # not dump table "links" because of following nomethoderror # undefined method `[]' nil:nilclass # not dump table "locations" because of following nomethoderror # undefined method `[]' nil:nilclass # not dump table "pages" because of following nomethoderror # undefined method `[]' nil:nilclass end
change migration file this
db/migrate/timestamp_create_links.rb
class createlinks < activerecord::migration def change create_table :links |t| t.integer :child_id t.integer :order t.text :body t.references :page, index: true t.timestamps end end end
Comments
Post a Comment