From 0292e8ca4fd5e955e8717a58ed710a2318f00c99 Mon Sep 17 00:00:00 2001 From: Sofia Date: Mon, 1 Jan 2024 22:47:51 +0300 Subject: [PATCH] Set default challenge in sketch form when navigating from challenge page --- app/controllers/sketches_controller.rb | 1 + app/views/challenges/show.html.erb | 2 +- app/views/sketches/_form.html.erb | 6 +++++- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/app/controllers/sketches_controller.rb b/app/controllers/sketches_controller.rb index 4281e78..ffbceeb 100644 --- a/app/controllers/sketches_controller.rb +++ b/app/controllers/sketches_controller.rb @@ -15,6 +15,7 @@ def show def new @sketch = Sketch.new + @challenge_id = params[:challenge_id] end def create diff --git a/app/views/challenges/show.html.erb b/app/views/challenges/show.html.erb index 9995b2b..e81df2c 100644 --- a/app/views/challenges/show.html.erb +++ b/app/views/challenges/show.html.erb @@ -3,7 +3,7 @@

<%= link_to "Vote", challenge_sketches_challenge_path(@challenge), class: "votes-challenge-button" %>

-

<%= link_to "Participate", new_sketch_path, class: "participate-challenge-button" %>

+

<%= link_to "Participate", new_sketch_path(challenge_id: @challenge.id), class: "participate-challenge-button" %>

diff --git a/app/views/sketches/_form.html.erb b/app/views/sketches/_form.html.erb index ba77a88..61949d4 100644 --- a/app/views/sketches/_form.html.erb +++ b/app/views/sketches/_form.html.erb @@ -18,7 +18,11 @@
<%= form.label :challenge_id %> - <%= form.collection_select :challenge_id, Challenge.all, :id, :title, prompt: true %> + <% if @challenge_id.present? %> + <%= form.collection_select :challenge_id, Challenge.all, :id, :title, { selected: @challenge_id }, promt: true %> + <% else %> + <%= form.collection_select :challenge_id, Challenge.all, :id, :title, prompt: true %> + <% end %>