Problem z odczytaniem formularza

Witam. Chcę zrobić w swoim projekcie tworzenie artykułu i podczas tworzenia go możliwość wybrania z listy wyboru kategorię do której ten artykuł ma należeć. W modelach mam wpisane odpowiednio has_many, belongs_to… niestety wywala mi błąd “Couldn’t find Category without an ID”. oto moje pliki :

reviews_controller:

class ReviewsController < ApplicationController

GET /reviews

GET /reviews.xml

layout ‘public’
def index
@reviews = Review.paginate :page => params[:page], :order => ‘created_at DESC’, :per_page => 7
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @reviews }
end
end

GET /reviews/1

GET /reviews/1.xml

def show
@review = Review.find(params[:id])

respond_to do |format|
  format.html # show.html.erb
  format.xml  { render :xml => @review }
end

end

GET /reviews/new

GET /reviews/new.xml

def new
@review = Review.new
@categories = Category.find(:all)
@choice = Category.find(params[:select1])
@choice.review.create(params[:review])

respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml => @review }
end

end

GET /reviews/1/edit

def edit
@review = Review.find(params[:id])
end

POST /reviews

POST /reviews.xml

def create
@review = Review.new(params[:review])

respond_to do |format|
  if @review.save
    flash[:notice] = 'Review was successfully created.'
    format.html { redirect_to(@review) }
    format.xml  { render :xml => @review, :status => :created, :location => @review }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @review.errors, :status => :unprocessable_entity }
  end
end

end

PUT /reviews/1

PUT /reviews/1.xml

def update
@review = Review.find(params[:id])

respond_to do |format|
  if @review.update_attributes(params[:review])
    flash[:notice] = 'Review was successfully updated.'
    format.html { redirect_to(@review) }
    format.xml  { head :ok }
  else
    format.html { render :action => "edit" }
    format.xml  { render :xml => @review.errors, :status => :unprocessable_entity }
  end
end

end

DELETE /reviews/1

DELETE /reviews/1.xml

def destroy
@review = Review.find(params[:id])
@review.destroy

respond_to do |format|
  format.html { redirect_to(reviews_url) }
  format.xml  { head :ok }
end

end
end

widok dla akcji “new”:

Nowa Recenzja

<%= form_tag :action =>'new' %> <% for category in @categories %> <%= category.title %> <% end %> <% end %>

<%= form_tag :action => ‘new’, :id => @choice %>
Tytul:

<%= text_area ‘review’, ‘title’ %>
Treść:
<%= text_area 'review, ‘text’%>

<% end %>

<%= f.submit 'Create' %>

<% end %>

<%= link_to ‘Back’, reviews_path %>

proszęo szybką i fachową pomoc jak to rozwiązać.

Szybko i fachowo: zajrzyj w logi
A bardziej serio: zajrzyj w logi. Będzie tam cały backtrace wyjątku ActiveRecord::RecordNotFound, dowiesz się skąd on został wyrzucony, możesz również dokładnie sprawdzić jakie parametry zostały wysłane z formularza. Ogólnie to akcja “new” wydaje się być problemem bo próbujesz tam szukac kategorii po “select1”, ale pewnie nie wchodzisz na tą akcje reviews/new?select1=N ? Dziwnie też wygląda: @choice.review.create(params[:review])

A czy mógłbyś mi napisać jak powinienem to zrobić?? nie chodzi o gotowca tylko o drogę, którą powinienem pójść żeby to zadziałało

Moim zdaniem powinieneś mieć to całkiem inaczej, a mianowicie.

W kontrolerze akcja new:

def new @review = Review.new @categories = Category.find(:all) #@choice = Category.find(params[:select1]) # PO CO TO BYŁO ??? #@choice.reviews.create(params[:review]) # PO CO TO BYŁO ??? end
akcja create:

@review = Review.new(params[:review]) @review.category = Category.find(params[:select1]) if @review.save flash[:notice] = 'Review was successfully created.' redirect_to(@review) else render :action => "new" end
W widoku new.html.erb:

[code=ruby]

Nowa Recenzja


<%= link_to ‘Back’, reviews_path %>

<% form_for @review do |f| -%>

<%= f.label :title %> <%= f.text_field :title %>

<%= f.label :body %> <%= f.text_area :body %>

<%= select_tag "select1", options_from_collection_for_select(@categories, :id, :name) %>

<%= f.submit 'Create' %>

<% end -%>[/code]

krzyczak, gdy wsadziłem twój kod do mojego projektu ( trochę tam nazwy kolumn pozmieniałem), dodałem sobie artykuł (wybrałem kategorię) i kazałem kategorii w akcji show kategorii wyświetlić wszystkie jej artykuły w ten sposób:

<% for review in @category.review %>
<%= review.title %>

<%= review.text %>
<% end %>

to wyrzuciło mi taki błąd:

ActiveRecord::StatementInvalid in Categories#show

Showing app/views/categories/show.html.erb where line #5 raised:

Mysql::Error: Unknown column ‘reviews.category_id’ in ‘where clause’: SELECT * FROM reviews WHERE (reviews.category_id = 3)

Extracted source (around line #5):

2:
3:

<%= @category.title %>


4: <%= link_to ‘Back’, categories_path %>
5: <% for review in @category.review %>
6: <%= review.title %>

7: <%= review.text %>
8: <% end %>

RAILS_ROOT: C:/modellarium
Application Trace | Framework Trace | Full Trace

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract_adapter.rb:219:in log' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/mysql_adapter.rb:323:inexecute’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/mysql_adapter.rb:608:in select' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/database_statements.rb:7:inselect_all_without_query_cache’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/query_cache.rb:60:in select_all' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/query_cache.rb:81:incache_sql’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/query_cache.rb:60:in select_all' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:661:infind_by_sql’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1548:in find_every' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:615:infind’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_collection.rb:60:in find' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_collection.rb:400:infind_target’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_collection.rb:354:in load_target' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_proxy.rb:212:inmethod_missing’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_collection.rb:369:in method_missing_without_paginate' C:/Ruby/lib/ruby/gems/1.8/gems/will_paginate-2.3.11/lib/will_paginate/finder.rb:168:inmethod_missing’
C:/modellarium/app/views/categories/show.html.erb:5:in _run_erb_app47views47categories47show46html46erb' C:/modellarium/app/controllers/categories_controller.rb:19:inshow’

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract_adapter.rb:219:in log' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/mysql_adapter.rb:323:inexecute’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/mysql_adapter.rb:608:in select' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/database_statements.rb:7:inselect_all_without_query_cache’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/query_cache.rb:60:in select_all' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/query_cache.rb:81:incache_sql’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/query_cache.rb:60:in select_all' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:661:infind_by_sql’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1548:in find_every' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:615:infind’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_collection.rb:60:in find' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_collection.rb:400:infind_target’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_collection.rb:354:in load_target' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_proxy.rb:212:inmethod_missing’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_collection.rb:369:in method_missing_without_paginate' C:/Ruby/lib/ruby/gems/1.8/gems/will_paginate-2.3.11/lib/will_paginate/finder.rb:168:inmethod_missing’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/renderable.rb:34:in send' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/renderable.rb:34:inrender’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/base.rb:306:in with_template' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/renderable.rb:30:inrender’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/template.rb:205:in render_template' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/base.rb:265:inrender’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/base.rb:348:in _render_with_layout' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/base.rb:262:inrender’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:1250:in render_for_file' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:951:inrender_without_benchmark’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/benchmarking.rb:51:in render' C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/benchmark.rb:17:inms’
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/benchmark.rb:10:in realtime' C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/benchmark.rb:17:inms’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/benchmarking.rb:51:in render' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/mime_responds.rb:135:insend’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/mime_responds.rb:135:in custom' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/mime_responds.rb:179:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/mime_responds.rb:179:in respond' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/mime_responds.rb:173:ineach’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/mime_responds.rb:173:in respond' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/mime_responds.rb:107:inrespond_to’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:1331:in send' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:1331:inperform_action_without_filters’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/filters.rb:617:in call_filters' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/filters.rb:610:inperform_action_without_benchmark’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/benchmarking.rb:68:in perform_action_without_rescue' C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/benchmark.rb:17:inms’
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/benchmark.rb:10:in realtime' C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/benchmark.rb:17:inms’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/benchmarking.rb:68:in perform_action_without_rescue' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/rescue.rb:160:inperform_action_without_flash’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/flash.rb:146:in perform_action' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:532:insend’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:532:in process_without_filters' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/filters.rb:606:inprocess’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:391:in process' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:386:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/routing/route_set.rb:437:in call' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:87:indispatch’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:121:in _call' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:130:inbuild_middleware_stack’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:29:in call' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:29:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/query_cache.rb:34:in cache' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:9:incache’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:28:in call' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:361:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/string_coercion.rb:25:in call' C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/head.rb:9:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/methodoverride.rb:24:in call' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/params_parser.rb:15:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/session/cookie_store.rb:93:in call' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/failsafe.rb:26:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in call' C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:insynchronize’
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in call' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:114:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/reloader.rb:34:in run' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:108:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/rails/rack/static.rb:31:in call' C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:46:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:40:in each' C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:40:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/rails/rack/log_tailer.rb:17:in call' C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/content_length.rb:13:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/handler/webrick.rb:50:in service' C:/Ruby/lib/ruby/1.8/webrick/httpserver.rb:104:inservice’
C:/Ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in run' C:/Ruby/lib/ruby/1.8/webrick/server.rb:173:instart_thread’
C:/Ruby/lib/ruby/1.8/webrick/server.rb:162:in start' C:/Ruby/lib/ruby/1.8/webrick/server.rb:162:instart_thread’
C:/Ruby/lib/ruby/1.8/webrick/server.rb:95:in start' C:/Ruby/lib/ruby/1.8/webrick/server.rb:92:ineach’
C:/Ruby/lib/ruby/1.8/webrick/server.rb:92:in start' C:/Ruby/lib/ruby/1.8/webrick/server.rb:23:instart’
C:/Ruby/lib/ruby/1.8/webrick/server.rb:82:in start' C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/handler/webrick.rb:14:inrun’
C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/commands/server.rb:111
C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in gem_original_require' C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:inrequire’
script/server:3

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract_adapter.rb:219:in log' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/mysql_adapter.rb:323:inexecute’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/mysql_adapter.rb:608:in select' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/database_statements.rb:7:inselect_all_without_query_cache’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/query_cache.rb:60:in select_all' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/query_cache.rb:81:incache_sql’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/query_cache.rb:60:in select_all' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:661:infind_by_sql’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1548:in find_every' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:615:infind’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_collection.rb:60:in find' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_collection.rb:400:infind_target’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_collection.rb:354:in load_target' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_proxy.rb:212:inmethod_missing’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_collection.rb:369:in method_missing_without_paginate' C:/Ruby/lib/ruby/gems/1.8/gems/will_paginate-2.3.11/lib/will_paginate/finder.rb:168:inmethod_missing’
C:/modellarium/app/views/categories/show.html.erb:5:in _run_erb_app47views47categories47show46html46erb' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/renderable.rb:34:insend’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/renderable.rb:34:in render' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/base.rb:306:inwith_template’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/renderable.rb:30:in render' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/template.rb:205:inrender_template’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/base.rb:265:in render' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/base.rb:348:in_render_with_layout’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/base.rb:262:in render' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:1250:inrender_for_file’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:951:in render_without_benchmark' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/benchmarking.rb:51:inrender’
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/benchmark.rb:17:in ms' C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/benchmark.rb:10:inrealtime’
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/benchmark.rb:17:in ms' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/benchmarking.rb:51:inrender’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/mime_responds.rb:135:in send' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/mime_responds.rb:135:incustom’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/mime_responds.rb:179:in call' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/mime_responds.rb:179:inrespond’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/mime_responds.rb:173:in each' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/mime_responds.rb:173:inrespond’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/mime_responds.rb:107:in respond_to' C:/modellarium/app/controllers/categories_controller.rb:19:inshow’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:1331:in send' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:1331:inperform_action_without_filters’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/filters.rb:617:in call_filters' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/filters.rb:610:inperform_action_without_benchmark’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/benchmarking.rb:68:in perform_action_without_rescue' C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/benchmark.rb:17:inms’
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/benchmark.rb:10:in realtime' C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/benchmark.rb:17:inms’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/benchmarking.rb:68:in perform_action_without_rescue' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/rescue.rb:160:inperform_action_without_flash’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/flash.rb:146:in perform_action' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:532:insend’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:532:in process_without_filters' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/filters.rb:606:inprocess’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:391:in process' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:386:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/routing/route_set.rb:437:in call' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:87:indispatch’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:121:in _call' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:130:inbuild_middleware_stack’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:29:in call' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:29:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/query_cache.rb:34:in cache' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:9:incache’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:28:in call' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:361:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/string_coercion.rb:25:in call' C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/head.rb:9:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/methodoverride.rb:24:in call' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/params_parser.rb:15:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/session/cookie_store.rb:93:in call' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/failsafe.rb:26:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in call' C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:insynchronize’
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in call' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:114:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/reloader.rb:34:in run' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:108:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/rails/rack/static.rb:31:in call' C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:46:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:40:in each' C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:40:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/rails/rack/log_tailer.rb:17:in call' C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/content_length.rb:13:incall’
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/handler/webrick.rb:50:in service' C:/Ruby/lib/ruby/1.8/webrick/httpserver.rb:104:inservice’
C:/Ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in run' C:/Ruby/lib/ruby/1.8/webrick/server.rb:173:instart_thread’
C:/Ruby/lib/ruby/1.8/webrick/server.rb:162:in start' C:/Ruby/lib/ruby/1.8/webrick/server.rb:162:instart_thread’
C:/Ruby/lib/ruby/1.8/webrick/server.rb:95:in start' C:/Ruby/lib/ruby/1.8/webrick/server.rb:92:ineach’
C:/Ruby/lib/ruby/1.8/webrick/server.rb:92:in start' C:/Ruby/lib/ruby/1.8/webrick/server.rb:23:instart’
C:/Ruby/lib/ruby/1.8/webrick/server.rb:82:in start' C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/handler/webrick.rb:14:inrun’
C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/commands/server.rb:111
C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in gem_original_require' C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:inrequire’
script/server:3

Request

Parameters:

{“id”=>“3”}

Show session dump


Response

Headers:

{“Content-Type”=>“text/html”,
“Cache-Control”=>“no-cache”}

[quote=jazzmann555]Extracted source (around line #5):

2:
3:

<%= @category.title %>


4: <%= link_to ‘Back’, categories_path %>
5: <% for review in @category.review %>
6: <%= review.title %>

7: <%= review.text %>
8: <% end %>[/quote]
Dokładnie tak jak jest napisane. Błąd jest w lini 5. powinno być
@category.reviews # a nie review

bo przecierz

Category has_many :reviews

Przy takim kodzie działa wszystko ładnie jak powinno…

nie działa nadal. :frowning: ten sam błąd dokładnie (wpisałem tak jak mówiłeś)

http://krzyczak.googlecode.com/files/help.rar

Tu działa. Porównaj co masz inaczej…

z tego co tam pisze w wyjątku to wynika, że nie masz kolumny “category_id” w tabeli reviews, więc ją dodaj…

IT’S ALIVE!!! dzięki krzyczak, to działa. masz u mnie zimnego browca :smiley: wytłumacz mi tylko to:

<%= select_tag "select1", options_from_collection_for_select(@categories, :id, :name) %>

(jak to działa)

i co robi akcja create, a co robi akcja new w scaffold.

Jak dodałem tą kolumnę co mówiłeś to zaczęło działać :smiley:

Moze sie wetne, jak jeszcze nie ma odpowiedzi, a wiec robisz sobie liste wybierana typu select i wypelniasz ja zbiorem par id, name z kolekcji kategorie, czyli da ci to

#name

jakoś tak

Czyli pod zmienna categories masz tablice z bazy danych z kategoriami wszystkimi i z tej tablicy options_from_collection_for_select robi ci te options z danymi, to co sie wyswietla to name, a to co leci do zapisu to id

i tyle

aha, no to luz. A co z akcją create i new?? co robi jedna, co druga??

new ma tworzyć tylko pusty obiekt do którego w widoku tworzysz formularz

create pobiera ten obiekt z formularza i zapisuje go w bazie danych…

A konkretnie w tym wypadku

def new @review = Review.new @categories = Category.find(:all) end
Najpierw tworzysz instancję klasy Review. Obiekt ten jest pusty, czyli we wszystkich polach ma nil.
Oprócz stworzenia instancji obiektu Review, masz tutaj jeszcze pobranie z bazy wszystkich kategorii, które są potrzbne, żeby zapełnić select boxa. To raczej oczywiste.

Natomiast to

def create @review = Review.new(params[:review]) #pobiera z formularza obiekt review @review.category = Category.find(params[:select1]) #ustawia kategorię, którą wybrałeś w formularzu if @review.save #zapisuje i sprawdza czy się powiodło flash[:notice] = 'Review was successfully created.' #jeśli tak to ustawia flash[:notice] redirect_to(@review) #i przekierowuje do tego obiektu else render :action => "new" #jeśli się zapis do bazy nie powiódł (np. z powodu nie przejścia walidacji) to zostajemy w tym widoku w którym byliśmy end end
Mam nadzieję, że wytłumaczyłem… :slight_smile:

Poczytaj przewodniki. Od tego powinien zacząć każdy, kto zaczyna przygodę z Rails.

Prośba do adminów - czy można zrobić jakiś sticky topic, w którym będą zalecenia dla zielonych? Żeby poczytali sobie przewodniki i jakąś książkę do Rubiego albo przynajmniej mój przewodnik, przecież wszystko to jest dostępne w sieci i po polsku, więc nie bardzo widzę sens dla topiców jak powyższy.

EDIT

Miałem sam sprawdzić, zanim piszę farmazony :wink: Jest taki topic. Ale jest już dosyć długi. Czy można by go przejrzeć/uporządkować albo zacząć od nowa. I każda osoba zanim udzieli szczegółowych informacji, krok po kroczku, jak wyświetlić logi i co to jest akcja “new”, a co “create”, niech najpierw się zastanowi, czy nie ma tego w ogólnie dostępnych materiałach wyminionych właśnie w tym (tzn. w tamtym ;)) topicu…

EDIT

Zarówno przewodniki, jak i moje wprowadzenie do Rubiego jest w dziale dokumentacja (link na samej górze). Zatem drodzy zieloni - zajrzyjcie najpierw tam, zanim zadacie podstawowe pytanie dotyczące zarówno języka jak i frameworku Ruby on Rails.

jeśli chodzi o mnie to przeczytałem już co nie co. Krzyczak dzięki, już to teraz rozumiem.

Apohllo, przejrzałem dokumentację, niezła sprawa, dzięki.