I am creating a question posting/answering application that utilizes CRUD processing in Ruby on Rails.
When posting the answer to the question, it is assumed that it will be rendered if a validation error occurs.
At that time, I tried to define the initial value of the instance variable, but the following error occurred.
Corresponding source code
ActiveRecord :: RecordNotFound in AnswersController # create Couldn't find Question without an ID
answers_controller.rb class AnswersController</pre> <pre><code>questions_controller.rb class QuestionsController</pre> <pre><code>show.html.erb <p>[Question]<br><% = markdown (@ question.title)%></p> <p>[Details]<br><% = markdown (@ question.detail)%></p> Answer <% @ answers.each do | a |%> <% = markdown (a.content)%> <hr> <% end%> Post answer <% = form_with model: [@question, @answer], local: true do | form |%> <% = form.label: title, "[Answer]"%> <% = form.text_area: content, class: "form-control"%> <% = form.submit "submit", class: "btn btn-primary btn-block"%> <% end%> <% end%> -Omitted-
What I tried
routes.rb Rails.application.routes.draw do devise_for: admin_users, ActiveAdmin :: Devise.config ActiveAdmin.routes (self) devise_for: users root "movies # index" resources: movies resources: aws_texts, only: [: index ,: show] resources: questions do resource: answers, only: [: create,: edit,: update,: destroy] end end
Similar to the show method of questions_controller.rb, to the part to be rendered by the create method of answers_controller.rb
@question = Question.all @question = Question.find (params [: id]) @answers = @ question.answers flash.now [: alert] = "Failed to post answer" render "questions/show"
Supplementary information (FW/tool version, etc.)
I tried to define an instance variable like this.
Answer # 1
@question = Question.find (params [: question_id]
Answer # 2
The error message is as follows. Since id is not specified when creating a new one (id is assigned when it is saved in the database),
params [: id]Nothing passes to.
@question = Question.newI think it is appropriate to create an empty one with (in the previous line)
@question = Question.allIs completely meaningless as it will be overwritten on the next line).
- ruby on rails - i want to activate the on option of validation successfully in update
- ruby - validation does not start
- ruby on rails - about routing errors in devise
- ruby - about integration test errors
- ruby - validation cannot be granted
- ruby on rails - i want to keep the selected image when a validation error occurs in an article post
- dealing with errors (bind: address already in use) when building a django development environment
- ruby - about validation of rails application record and active model
- ruby - i want to solve the page transition to a different url ("users/sign_up" → "/users") when a validation
- ruby on rails 6 - about validation (number of characters) of rails tweet posting
- ruby - when i try to update a record, it gets stuck in validation
- ruby - validation doesn't work with rails update
- ruby - custom validation of nested parameters in rails
- ruby - about errors related to migration files in rails development
- about errors when building ruby on rails environment
- i want to eliminate validation due to belongs_to has_many in ruby on rails
- you can do redirect_to with ruby on rails, but you get nomethoderror when rendering
- password confirmation validation doesn't work in ruby on rails
- ruby on rails 6 - when dealing with nested paths in the rails form_with method
- html - about boot strap free introduction
- ruby - about customizing pagination with rails6 kaminari
- ruby - i want to display the edit button and delete button on the index page
- ruby - i don't feel that the sql issued by rails has a different format from the sql that i usually write
- ruby - nomethoderror at post time with tag function implementation
- ruby - rails6 active_strage cannot be installed
- ruby - data including rails hyphen becomes # and is not displayed
- ruby - i want to display new posts i made on the top page only for myself
- ruby - [rails6] i get an error on heroku when i write
- ruby - n + 1 problem in api mode