plataformatec/simple_form — Issue #349: can't make association working
Author: fifoooo
Created: 2011-10-17 20:33:11+00:00
State: open

hi there i'm a big RoR newbie. I try to use simple_form in a search form along with metasearch. WOrks fine until i try to add an association to my form . here is the code :

biens/index.html.erb

Listing biens

<%= simple_form_for @search do |f| %>
<%= f.input :titre_contains, :label => "Titre", :required => false %>
<%= f.input :prix_gte, :label => "Prix entre", :required => false %>
<%= f.input :prix_lte, :label => "et", :required => false %>
<%= f.association :category %>
<%= f.button :submit, "lancer la recherche" %>
<% end %>

<%= render 'index' %>

<%= link_to 'New Bien', new_bien_path %>

model bien.rb

class Bien < ActiveRecord::Base
belongs_to :category
belongs_to :operation

validates :prix, :presence => true
validates :num, :uniqueness => true, :presence => true
validates :titre, :presence => true

end

model category.rb

class Category < ActiveRecord::Base
has_many :biens
validates_presence_of :nom

end

biens_controller.rb

class BiensController < ApplicationController

def index

@search = Bien.search(params[:search])
@biens = @search.all

respond_to do |format|
format.html # index.html.erb
format.json { render json: @biens }
end
end

So really a beginner stuff. I try a lot of things toi make it works like :
- using :categories instead of :category
- add :collection => Category.all()

Anyway it says the same :

Started GET "/biens" for 127.0.0.1 at 2011-10-17 22:21:23 +0200
Processing by BiensController#index as HTML
Bien Load (0.0ms) SELECT "biens".* FROM "biens"
Category Load (0.0ms) SELECT "categories".* FROM "categories"
Rendered biens/index.html.erb within layouts/application (1117.2ms)
Completed 500 Internal Server Error in 1374ms

ActionView::Template::Error (Association :category not found):
4: <%= f.input :titre_contains, :label => "Titre", :required => false %>
5: <%= f.input :prix_gte, :label => "Prix entre", :required => false %>
6: <%= f.input :prix_lte, :label => "et", :required => false %>
7: <%= f.association :category, :collection => Category.all(), :label => 'Type de bien :' %>
8: <%= f.button :submit, "lancer la recherche" %>
9: <% end %>
10:
app/views/biens/index.html.erb:7:in `block in _app_views_biens_index_html_erb___396600003_29987700'
app/views/biens/index.html.erb:3:in `_app_views_biens_index_html_erb___396600003_29987700'
app/controllers/biens_controller.rb:9:in `index'

Rendered C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.0ms)
Rendered C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
Rendered C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (7.8ms)

Thanks if you have time for this.