0

I can't update field in database table. Why, when I working in edit template(controller/id/edit), rails redirect me to CREATE action and create a new row in table? It must redirect me to update action and update only one field. Controller:

  def create
 Question.create(:user_id => session[:user_id], :question => params[:question][:question])
 end

  def update
  debugger #not go there
  @update = Question.find(params[:id])
  @update.update_attributes(params[:question_status])
 end

edit template

= form_for Question.new do |w|
  %p
= w.label :question_status, :caption => "question status: "
= w.text_field :question_status
  %p
= w.submit "Update"

routs.rb

match "questions/logout" => "questions#logout"
match "questions/show_all_questions" => "questions#show_all_questions"
match "questions/update" => "questions#update"
resources :questions

get "questions/create" 
get "questions/show"
get "questions/update"
get "questions/destroy"
get "questions/new"
get "questions/edit"
get "questions/index"
Johny Oggy
  • 61
  • 1
  • 2
  • 8
  • 1. Edit form with action `Question.new`? are you sure?. 2. You have use `resources :questions` on `routes.rb`, why you add get "questions/....." on your `routes.rb` I think to use enough `resources :questions`. – rails_id Jun 02 '13 at 17:47
  • when i trying Question.update or Question.edit have this error `wrong number of arguments (0 for 2)` `undefined method `edit'` – Johny Oggy Jun 02 '13 at 17:51

1 Answers1

0

It redirects you to the create method because your call Question.new.

you should have your form like:

= form_for Question do |w|
Flo
  • 540
  • 6
  • 20
  • I have questions: why are you using you using Question? and not the usual @question ? – Flo Jun 02 '13 at 17:58
  • but field not update... in controller I trying this `@update = Question.find(params[:id])# @update.update_attributes(params[:question_status])` – Johny Oggy Jun 02 '13 at 18:06
  • if status is a field in question. try @update.update_attributes(params[:status]) if status is inside the question you can access it with @update.update_attributes(params[:question][:status]) – Flo Jun 02 '13 at 18:10
  • because i should use parameters in controller as a hash. Solved!!! `@update.update_attributes(:question_status => params[:question][:question_status])` thank you again – Johny Oggy Jun 02 '13 at 18:10