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...
--
Posted via http://www.ruby-forum.com/.
make sure that you have a hidden field in your form [:answer][:id], and then see the code for the controller below. matt> ####################### 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])#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> 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... > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060119/d604e37d/attachment.html
How do you make a hidden field? -- Posted via http://www.ruby-forum.com/.
http://api.rubyonrails.com/classes/ActionView/Helpers/FormTagHelper.html#M000408 On 1/19/06, Xavier Lange <cuxrl@eiu.edu> wrote:> > How do you make a hidden field? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Ben Reubenstein benr@x-cr.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060120/0ef27c28/attachment.html
Thanks a bunch, all my searches for it were turning up the hidden text field. -- Posted via http://www.ruby-forum.com/.
Xavier Lange wrote:> Thanks a bunch, all my searches for it were turning up the hidden text > field.Oh ho ho, it just hit me... -- Posted via http://www.ruby-forum.com/.
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
######
--
Posted via http://www.ruby-forum.com/.
No ideas on what''s up? -- Posted via http://www.ruby-forum.com/.
Xavier Lange 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 -- Posted via http://www.ruby-forum.com/.