Question

I have an issues with using the Inherited Resource Gem on rails 4:

whenever I try to create a new project that is scoped to the user I get this error:

 Failure/Error: visit new_project_path
 ActionView::Template::Error:
   No route matches {:id=>#<Project id: nil, user_id: 32568, name: "", created_at: nil, updated_at: nil>} missing required keys: [:id]

As I understand it the new action shouldn't need an ID the only thing it should need is access to the user which I provide through the begin_of_association_chain method.

Anyone know why this is happening? I'm sure I'm missing something simple?

Controller:

class ProjectsController < InheritedResources::Base respond_to :html, :json

before_filter :authenticate_user!

protected
  def begin_of_association_chain
    #provided by devise
    current_user 
  end

private
  def permitted_params
    {:project => params.fetch(:project, {}).permit(:email, :name, :destination_ids => [])}
  end
end

Rspec Test:

require 'spec_helper'

include Warden::Test::Helpers

describe "the signed in user" do

  before :each do
    @user = FactoryGirl.create(:confirmed_user)
    login_as(@user, scope: :user)
  end

  describe "with new project" do

    before :each do
      @project = FactoryGirl.attributes_for(:project)
      visit new_project_path
      fill_in "project_name", with: @project[:name]
    end

    it "can create" do
      expect { click_button('Create Project') }.to change(Project, :count).by(1)
    end

  end

end

View:

<div class="row">
 <div class="large-12 columns">
  <%= form_for(@project) do |f| %>

    <% if @project.errors.any? %>
      <div data-alert class="alert-box alert"><%= pluralize(@project.errors.count, "error") %> prohibited this source from being saved:</div>
    <% end %>

    <%= f.text_field_block :name, placeholder: "Your project's name", maxlength: 500 %>

    <hr>

    <div class="row">
     <div class="large-5 columns">
        <%= f.submit class: "button tiny" %> <%= link_to 'Back', :back, class: "button secondary tiny" %>
     </div>
    </div>
  <% end %>
 </div>
</div>

Routes:

projects POST     /projects(.:format)           projects#create
new_project GET   /projects/new(.:format)       projects#new
edit_project GET  /projects/:id/edit(.:format)  projects#edit
project GET       /projects/:id(.:format)       projects#show
PATCH             /projects/:id(.:format)       projects#update
PUT               /projects/:id(.:format)       projects#update
DELETE            /projects/:id(.:format)       projects#destroy
Was it helpful?

Solution

I found the answer.

In my code I'm looping through the various projects to create shortcut links

In active resource when you create a new model it uses: current_user.project.build() this added a single unsaved entity to my user which had no id. When I looped through to create project links rails was complaining because it cant create a link to an unsaved entity without an id.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top