I would like to randomly display the record and column specified in the problem model in the view, enter the answer in the text area, and then match the answer column of the previously specified record to flash the correct or incorrect answer.
Currently, you can randomly display the specified records and columns in the view and enter answers in the text area, but when you try to match, you get the error "Couldn't find Problem without an ID".
I am having trouble because the id I tried to match against the id of the randomly displayed record does not match.
Please let me know.
Here is the code.
controller
class ProblemsController<ApplicationController
before_action —Logged_in_user
before_action: correct_user, only: %i [edit update]
def show
@problem=Problem.find (params[:id])
end
def new
@problem=Problem.new
end
def create
@problem=current_user.problems.build(problem_params)
@problem.picture.attach (params[:problem][:picture])
[email protected]
flash[:success] = 'Problem created!'
redirect_to problem_path(@problem)
else
render 'problems/new'
end
end
default
@problem=Problem.find (params[:id])
end
default update
@problem=Problem.find (params[:id])
[email protected](problem_params)
flash[:success] = 'Problem information updated!'
redirect_to@problem
else
render 'edit'
end
end
def destroy
@problem=Problem.find (params[:id])
if current_user.admin?||current_user?(@problem.user)
@problem.destroy
flash[:success] = 'Problem Deleted'
redirect_to request.refer==user_url(@problem.user)?user_url(@problem.user): root_url
else
flash[:danger] = 'You cannot delete a problem with another account'
redirect_to root_url
end
end
def random
@problem=Problem.offset(land(Problem.count)) .take
end
default answer
@problem=Problem.find (params[:id])
[email protected] server==params [:problem] [:answer]
flash.now [:notice] = "hit"
else
flash.now [:notice] = "Out"
end
render —random
end
private
def problem_params
param.require(:problem).permit(:study_type,:title,:exploration_text,:problem_text,:answer,:problem_explanation,:taget_age,:reference,:picture)
end
def correct_user
# Determine if the Current User Has Issues to Update
@problem=current_user.problems.find_by (id:params[:id])
redirect_to root_url [email protected]?
end
end
view
<%provide(:title, "Question")%>
<div class="container">
<div class="row">
<%=form_with model:@problem, url: {controller: 'problems', action: 'answer'} do | f | % >
<%=f.hidden_field: id%>
<div><%[email protected]_type%></div>br>
<div><%[email protected]_text%></div>br>
<div><%[email protected]_text%></div>br>
<%=f.text_field: answer, value:"%>
<%=f.submit "Answer", class: "btn btn-primary" %>
<%=link_to "Next Problem", problems_random_path%>
<%end%>
</div>
routes
get "/problems/random", to: "problems#random"
patch '/problems/random', to: 'problems#answer'
resources —problems
I think the reason is that the @problem id is not included in the path
If you include the id in the path as follows, you do not need the view <%=f.hidden_field:id%>
either
route.rb
resources:problems do
get:random, on::collection
patch:random, to: 'problems#answer', on: :member
end
rails routes Results
Prefix Verb URI Pattern Controller#Action
random_problems GET/problems/random(.:format)problems#random
random_problem PATCH/problems/:id/random(.:format)problems#answer
problems GET/problems(.:format)problems#index
POST/problems(.:format)problems#create
new_problem GET/problems/new(.:format)problems#new
edit_problem GET/problems/:id/edit(.:format)problems#edit
problem GET/problems/:id(.:format)problems#show
PATCH/problems/:id(.:format)problems#update
PUT/problems/:id(.:format)problems#update
DELETE/problems/:id(.:format)problems#destroy
view
<%=form_with model:@problem, url:random_problem_path(@problem), method::patch do|f|%>
Reference
https://railsguides.jp/routing.html
© 2024 OneMinuteCode. All rights reserved.