I have a form so that the user can ask a question (in addition to the user model, there is a question model on the profile page with nested answers). It sends the create_controller.rb action to the user profile page /views/users/show.html.erb. If this is not confirmed, I think that by default for Rails it is necessary to display the form (with incorrect information in the form for user editing). However, since I submit the form for the question model on the user profile page, preliminary preparation does not occur if the validation fails; the user is forced to enter all the information in the form again. Is there a way in this context to get a form on the user display page to fill in the information that was entered before submitting?
questions_controller
def create @question = current_user.questions.build(params[:kestion]) if @question.save redirect_to current_user, :notice => "Successfully created question." else
Update I also changed the end of the creation method
Redirect ( : back ), :notice => "something went wrong.try again"
But I still canβt get the form to fill out, and verification error messages are not displayed, only a flash notification.
Update The show method of the user controller creates a new question (with the user)
def show @user = User.find(params[:id]) @question = Question.new 3.times {@question.answers.build} end
/views/users/show.html.erb
<%= form_for @question do |f| %> <% if @question.errors.any? %> <h2><%= pluralize(@question.errors.count, "error") %> prohibited this question from being saved: </h2> <ul> <% @question.errors.full_messages.each do |msg| %> <li> <%= msg %></li> <% end %> </ul> <% end %> <p> <%= f.label :content, "Question"%> <%= f.text_area :content, :class => 'span4', :rows => 1 %> </p> <p> <%= f.label :link, "QuoraLink" %> <%= f.text_field :link, :class => 'span4', :rows => 1 %> </p> <%= f.fields_for :answers do |builder| %> <p> <%= render 'answer_fields', :f => builder %> </p> <% end %> <p><%= link_to_add_fields "Add Answer", f, :answers %></p> <p><%= f.submit %></p> <% end %>
parts of answers partially displayed from questions, partial
<p class="fields"> <%= f.label :content, "Answer..." %> <%= f.text_field :content, :class => 'span3', :rows => 1%> <%= f.label :correctanswer, "Correct" %> <%= f.check_box :correctanswer, :class => 'span1' %> <%= link_to_remove_fields "remove answer", f %> </p>