Forum: Ruby on Rails Passing variables across forms

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Xavier L. (Guest)
on 2006-01-19 22:17
I've been reading my Agile Dev book over and over again, but I can't
wrap my head around how to do this.  There's a question and answer
portion site of my site.  You begin the process by adding a question to
the database.  After that, the program should redirect you straight to
adding an answer for that question.  The problem is, I don't know how to
keep track of the answer's question id.

Here's what I have now:


####################### Controller
  def new
    @question = Question.new
  end

  def create
    @question = Question.new(params[:question])
    if @question.save
      flash[:notice] = 'Question was successfully created.'
      redirect_to :action => 'new_answer', :id => @question
    else
      render :action => 'new'
    end
  end

  def new_answer
    # the new answer must belong to a question... so I wasn't sure
    # whether to call it new answer or add answer.
    @question = Question.find(params[:id])
    @answer = Answer.new
    @question.answers << @answer

  end

  def create_answer
    @answer = Answer.new(params[:question_id])
    if @answer.save
      flash[:notice] = 'Answer was added to the question.'
      redirect_to :action => 'show', :id => @question
    else
      render :action => 'create_answer'
    end
  end
###########################

########################### new_answer.rhtml
<%= start_form_tag :action => 'create_answer' %>
  <%= render_partial 'form_new_answer' %>
  <%= submit_tag "Create" %>
<%= end_form_tag %>
###########################

########################### _form_new_answer.rhtml
<p><label for="answer_answer">Answer to add</label><br/>
<%= text_area 'answer', 'answer', {:cols=>"80", :rows=>"5"}  %></p>
###########################

########################### answer model
class Answer < ActiveRecord::Base
  belongs_to :question
  validates_presence_of :answer, :question_id
end
###########################

########################### question model
class Question < ActiveRecord::Base
  validates_presence_of :question
  has_many :answers
end
############################


Probably too much code for what you need to know...
matthew clark (Guest)
on 2006-01-19 22:52
(Received via mailing list)
make sure that you have a hidden field in your form [:answer][:id], and
then
see the code for the controller below.

matt


>     else
>       render :action => 'new'
>     end
>   end
>
>   def new_answer
>     # the new answer must belong to a question... so I wasn't sure
>     # whether to call it new answer or add answer.
>     question = Question.find(params[:id])


        #there are a few ways to do this.  I think this is most straight
forward
        # there is a sister method to answers_create().  One of them
        # saves the new answer, and the other doesn't, depnding on
        # what you want

    @answer = question.answers_create()
>
>   end
>
>   def create_answer


       # this or  answer.find(params[:answer][:id])  if you saved the
       # new answer in the 'new_answer' method

    @answer = Answer.new(params[:answer])
>     if @answer.save
>       flash[:notice] = 'Answer was added to the question.'


         # you don't have a 'show' method in this controller

      redirect_to :action => 'show', :id => @question
Xavier L. (Guest)
on 2006-01-20 02:03
How do you make a hidden field?
Ben R. (Guest)
on 2006-01-20 02:39
(Received via mailing list)
Xavier L. (Guest)
on 2006-01-20 06:04
Thanks a bunch, all my searches for it were turning up the hidden text
field.
Xavier L. (Guest)
on 2006-01-20 06:07
Xavier L. wrote:
> Thanks a bunch, all my searches for it were turning up the hidden text
> field.

Oh ho ho, it just hit me...
Xavier L. (Guest)
on 2006-01-20 07:52
Thanks for the tips, but I've hit a bit of a wall now.
Rails crashes right after I try to add my first answer. I think it's
passing along the values just right, because it says the parameters are

Parameters: {"answer"=>{"answer"=>"Testing 123", "question_id"=>"10"},
"commit"=>"Create"}

The error it gives is

undefined method `stringify_keys!' for "10":String

####### CONTROLLER

  def new_answer
    # the new answer must belong to a question... so I wasn't sure
    # whether to call it new answer or add answer.
    @answer = Question.find(params[:id]).answers.create
  end

  def create_answer
    @answer = Answer.new(params[:answer][:question_id])
    if @answer.save
      flash[:notice] = 'Answer was added to the question.'
      redirect_to :action => 'list'
    else
      redirect_to :action => 'list'
    end
  end
######
Xavier L. (Guest)
on 2006-01-20 23:06
No ideas on what's up?
Kevin O. (Guest)
on 2006-01-20 23:17
Xavier L. wrote:
> Parameters: {"answer"=>{"answer"=>"Testing 123", "question_id"=>"10"},
> "commit"=>"Create"}
>
> The error it gives is
>
> undefined method `stringify_keys!' for "10":String
>
>   def create_answer
>     @answer = Answer.new(params[:answer][:question_id])
>     if @answer.save
>       flash[:notice] = 'Answer was added to the question.'
>       redirect_to :action => 'list'
>     else
>       redirect_to :action => 'list'
>     end
>   end
> ######

based on the params, this line would resolve to this....

     @answer = Answer.new(10)

This is probably where your trouble begins...You probably want this..


     @answer = Answer.new(params[:answer])

_Kevin
This topic is locked and can not be replied to.