Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8819729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:26:09+00:00 2026-06-14T05:26:09+00:00

I have a new form for create question and answers. This is my form:

  • 0

I have a new form for create question and answers. This is my form:

<%= simple_form_for [@question_type, @question], url: path, defaults: { error: false } do |question_form| %>
  <%= render 'shared/error_messages', object: question_form.object %>

  <div class="question_fields well">
    <%= question_form.input :content, input_html: { rows: 4, class: 'span6' } %>
    <%= question_form.input :mark, input_html: { class: 'span1' } %>
    <%= question_form.association :topic %>
  </div>
  <%= question_form.simple_fields_for :answers do |answer_form| %>
    <%= render 'answer', f: answer_form %>
  <% end %>
  <%= question_form.button :submit, class: "new_resource" %>
<% end %>

The question have 3 fields: content, mark, topic.

This is my create action in question controller:

def create
  @question = Question.new(params[:question])
  @question.question_type_id = params[:question_type_id]
  @question.user_id = current_user.id

  if @question.save
    flash[:success] = "Successfully created question."
    redirect_to new_question_type_question_path
  else
    render 'new'
  end
end

My routes:

resources :question_types, only: [:index] do
  resources :questions
end

Now, i want after user submit to create question successful, it will show new form again, but the topic select will display the topic of question was just saved. How can i do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:26:11+00:00Added an answer on June 14, 2026 at 5:26 am

    #1 Solution –

    If I understood your question correctly you can pass question’s topic_id to new action after
    question saved successfully.

    redirect_to new_question_type_question_path(:topic_id => @question.topic_id )
    

    then in new action of questions controller, add topic_id if params[:topic_id] present?

    something like this,

    def new
      ...
      ...
      @topic_id = params[:topic_id] if params[:topic_id].present?
    end
    

    then in new form, display your topic using this @topic_id instance variable. I don’t know too much about simple_form_for, but you can do something like,

    <%= question_form.association :topic, :selected => (@topic_id.present? ? @topic_id : '') %>
    

    OR

    #2 Solution

    To display the topic of last question saved, you just need last question object in new action.
    You don’t need to do above steps of #1 Solution

    def new
      ...
      ...
      @topic_id = current_user.questions.order('created_at ASC').last.topic_id if current_user.questions.present?
    end
    

    In new form do same thing as given in #1 Solution,

    <%= question_form.association :topic, :selected => (@topic_id.present? ? @topic_id : '')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a follow-up question to this one . I created a new form,
I have a strange one. Create a new form. Then add the following function
I have a Create new account page. The form is in page named Register.php
So I have this JS form that creates a new record ( MySQL ,
I have a scenario like this: form = MockRepository.GenerateMock<IAddAddressForm>(); mediator = new AddAddressMediator(form); The
I am new to CSS and have been trying to create a web form
I have a form that save a question and five answers in the database
I have built a nested resources between question and answer model. This is form
First of all I have seen SO many answers to a question like this,
I am new to mobile application development with PhoneGap. I have created a form

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.