I am working on a simple crowdsource platform in Rails 4.1. The application is based on the rails-devise-pundit starter app from RailsApp. After the initial bootstrap I have generated the following two scaffolds:
- project
- pledge
$ rails generate scaffold project title body:text user_id:integer:index
$ rails generate scaffold pledge amount:integer user_id:integer:index project_id:integer:index
I have the following associations:
app/models/user.rb
class User < ActiveRecord::Base
has_many :projects
has_many :pledges
end
app/models/project.rb
class Project < ActiveRecord::Base
belongs_to :user
has_many :pledges
end
app/models/pledge.rb
class Pledge < ActiveRecord::Base
belongs_to :project
belongs_to :user
end
And the following routes.
config/routes.rb
Rails.application.routes.draw do
resources :users
resources :projects do
resources :pledges
end
end
When I try to create a new pledge on a project I get the following error:
NoMethodError at /projects/1/pledges/new
undefined method `pledges_path' for #<#:0x007f994c0596b8> Started GET "/projects/1/pledges/new" for 127.0.0.1 at 2014-07-04 23:18:59 +0200 Processing by PledgesController#new as HTML
Parameters: {"project_id"=>"1"} User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1 Rendered pledges/_form.html.erb (8.6ms) Rendered pledges/new.html.erb within layouts/application (9.3ms) Completed 500 Internal Server Error in 28msNoMethodError - undefined method
pledges_path' for #<#<Class:0x007f994c05a6d0>:0x007f994c0596b8>: actionpack (4.1.1) lib/action_dispatch/routing/polymorphic_routes.rb:142:inpolymorphic_url' actionpack (4.1.1) lib/action_dispatch/routing/polymorphic_routes.rb:148:inpolymorphic_path' actionview (4.1.1) lib/action_view/helpers/form_helper.rb:452:inapply_form_for_options!' actionview (4.1.1) lib/action_view/helpers/form_helper.rb:425:inform_for'_app_views_pledges__form_html_erb___852280298890992063_70148190581820'
app/views/pledges/_form.html.erb:1:in
The _form.html.erb for the pledge-view is just the default generated from the scaffold.
app/view/pledges/_form.html.erb
<%= form_for(@pledge) do |f| %>
<% if @pledge.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@pledge.errors.count, "error") %> prohibited this pledge from being saved:</h2>
<ul>
<% @pledge.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :amount %><br>
<%= f.number_field :amount, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :user_id %><br>
<%= f.number_field :user_id, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :project_id %><br>
<%= f.number_field :project_id, class: "form-control" %>
</div>
<div class="form-group">
<%= f.submit class: "btn btn-primary" %>
</div>
<% end %>
Can anyone tell me what I am doing wrong or help me on how I should approach the simple models with users, projects and pledges to create a simple crowdsourcing platform in rails?
When I created the nested resources in config/routes.rb I also changed all "pledge_path" to "project_pledge_path". Showing pledges under projects seems to work, but not creating a new one.