Felipe Cerda
Felipe Cerda

Reputation: 490

ActiveRecord::RecordNotFound - Couldn't find Keynote without an ID

I'm building an application that has a Keynote model and a Story model (as well as a User model that I implemented with Devise). Keynotes have many Stories and a Story belongs to a Keynote.

I'm having problems creating new stories and I get the following error:

ActiveRecord::RecordNotFound in StoriesController#create

Couldn't find Keynote without an ID

The error happens on line 17 of stories_controller.rb which is

@keynote = Keynote.find(params[:keynote_id])

in the create method.

This is part of my stories_controller.rb

class StoriesController < ApplicationController

  before_filter :authenticate_user!, :except => [:show, :index]

  def index
    @keynote = Keynote.find(params[:keynote_id])
    @stories = @keynote.stories
  end

  def new
    @keynote = Keynote.find(params[:keynote_id])
    @story = @keynote.stories.build
  end

  def create
    if user_signed_in?
      @keynote = Keynote.find(params[:keynote_id])
      @story = @keynote.current_user.stories.build(params[:story])
      if @story.save
        flash[:notice] = 'Question submission succeeded'
        redirect_to keynote_stories_path
      else
        render :action => 'new'
      end
    end
  end

This is my keynotes_controller.rb

class KeynotesController < ApplicationController
  def index
    @keynotes = Keynote.find :all, :order => 'id ASC'
  end

  def new
    @keynote = Keynote.new
  end

  def show
    @keynote = Keynote.find(params[:id])
  end

  def create
    @keynote = Keynote.new(params[:keynote])
    if @keynote.save
      flash[:notice] = 'Keynote submission succeeded'
      redirect_to keynotes_path
    else
      render :action => 'new'
    end
  end

end

Any help would be really appreciated.

Edit: These are the Parameters when I try to create a new Story.

{"utf8"=>"✓", "authenticity_token"=>"76odSpcfpTlnePxr+WBt36fVdiLD2z+Gnkxt/Eu1/TU=", "story"=>{"name"=>"as"}, "commit"=>"Send"}

It looks like the ID for the Keynote is not being passed.

This is the view for StoriesController#new

<%= error_messages_for 'story' %>

<%= form_for @story do |f| %>
    <p>
        Question:<br />
        <%= f.text_field :name %>
    </p>
    <p>
        <%= submit_tag "Ask" %>
    </p>
<% end %>

This is what I have in my routes.rb file:

  get "keynotes/index"
  get "users/show"
  devise_for :users
  get "votes/create"
  get "stories/index"

  resources :keynotes do
    resources :stories
  end

  resources :stories do
    get 'bin', :on => :collection
    resources :votes
  end

  resources :users

  root :to => 'keynotes#index'

Upvotes: 1

Views: 1344

Answers (1)

Zabba
Zabba

Reputation: 65467

I think this should do the trick:

<%= form_for @story do |f| %>
    <%= hidden_field_tag 'keynote_id', @keynote.id  %>
    .
    rest of the form stuff here
    .
<% end %>

PS. Not sure if you will get the keynote_id in params[:keynote_id] or params[:story][:keynote_id] .. check out both.

NB: I think there would be a easier way to do it too, using fields_for or something similar, but I don't have access to a Rails setup at the moment to test that out.

Upvotes: 1

Related Questions