Skip to content

Commit

Permalink
fix create podcasts, turboframing the #show
Browse files Browse the repository at this point in the history
  • Loading branch information
o-200 committed Dec 20, 2024
1 parent 3ad3572 commit 358d97a
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 13 deletions.
20 changes: 15 additions & 5 deletions app/controllers/podcasts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,28 @@ def index
@last_podcast = Podcast.last
end

def load_podcasts
@podcasts = Podcast.take(4)
render partial: "podcasts_list", locals: { podcasts: @podcasts }, layout: false
def load_list
podcasts = get_podcasts

render partial: "podcasts_list", locals: { podcasts: podcasts }
end

def show
@podcast = Podcast.find_by(id: params[:id])
render partial: "show", locals: { podcast: @podcast }
end

def new
@podcast = Podcast.new
end

def create
@podcast = Podcast.new(podcast_params)
@podcast = current_user.podcasts.build(podcast_params)

if @podcast.save
redirect_to @podcast
else
render :new
render :new, notice: @podcast.errors.full_messages
end
end

Expand Down Expand Up @@ -54,6 +56,14 @@ def destroy

private

def get_podcasts
if current_user
return current_user.podcasts if params[:filter] == 'my_podcasts'

Check failure on line 61 in app/controllers/podcasts_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
end

Podcast.take(12)
end

def podcast_params
params.require(:podcast).permit(:title, :description, :user_id, :photo, :audio)
end
Expand Down
19 changes: 14 additions & 5 deletions app/views/layouts/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,20 @@
</ul>
<% if current_user %>
<div class="row justify-content-evenly">
<div class="col-4">
<%= link_to "+", new_podcast_path, class: 'btn btn-outline-success' %>
</div>
<div class="col-4">
<%= button_to "exit", destroy_user_session_path, class: 'btn btn-outline-danger', method: :delete %>
<div class="col-3 dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
Menu
</button>
<ul class="dropdown-menu">
<li>
<%= link_to "My podcasts", load_podcasts_path(filter: 'my_podcasts'),
class: "dropdown-item btn btn-outline-success",
data: { turbo_frame: 'podcasts_list' } %>
</li>
<li><%= link_to "Add new podcast", new_podcast_path, class: 'dropdown-item btn btn-outline-success' %></li>
<li><hr class="dropdown-divider"></li>
<li><%= button_to "Sign Out", destroy_user_session_path, class: 'dropdown-item btn btn-outline-danger', method: :delete %></li>
</ul>
</div>
</div>
<% else %>
Expand Down
4 changes: 3 additions & 1 deletion app/views/podcasts/_podcasts_list.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
<img src="https://mir-s3-cdn-cf.behance.net/project_modules/max_1200/9abc28136491393.61fa8bcfee8ce.png" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">
<%= link_to(podcast.title, podcast, class: "text-decoration-none", data: { turbo: false }) %>
<%= link_to podcast.title, podcast,
class: "text-decoration-none",
data: { turbo_frame: 'podcasts_list' } %>
</h5>
<p class="card-text"><%= podcast.description %></p>
</div>
Expand Down
36 changes: 36 additions & 0 deletions app/views/podcasts/_show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<turbo-frame id="podcast_frame">
<div class="d-flex">
<div class="px-2">
<div class="card d-flex flex-column mb-3">
<% if @podcast.photo.attached? %>
<%= image_tag @podcast.photo, height: 500 %>
<% else %>
<div class="alert alert-warning" role="alert">No Photo Available!</div>
<% end %>

<% if @podcast.audio.attached? %>
<%= audio_tag @podcast.audio, controls: true %>
<% else %>
<div class="alert alert-warning" role="alert">No Audio Available!</div>
<% end %>
</div>
</div>

<div class="p-2 card flex-fill d-flex flex-column mb-3">
<div class="card-body">
<h5 class="card-title fs-1 display-1"><%= @podcast.title %></h5>
<h6 class="card-subtitle mb-2 text-body-secondary fs-6"><%= @podcast.created_at %></h6>
<p class="card-text fs-5 display-2"><%= @podcast.description %></p>
</div>
<div class="card-footer bg-transparent border-success">
<a href="#" class="card-link fs-6">Card link</a>
<a href="#" class="card-link fs-6">Another link</a>
</div>
</div>
</div>

<div class="d-flex flex-column mb-3">
<%= link_to "Редактировать Podcast", edit_podcast_path(@podcast), class: "btn btn-secondary mb-2" %>
<%= link_to "Удалить Podcast", podcast_path(@podcast), method: :delete, data: { confirm: "Вы уверены?" }, class: "btn btn-danger mb-2" %>
</div>
</turbo_frame>
2 changes: 1 addition & 1 deletion app/views/podcasts/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
</div>
<%= link_to "Check it out!", load_podcasts_path,
class: "btn btn-primary btn-lg d-flex align-items-center justify-content-center",
data: { turbo_frame: 'podcasts_list', turbo_method: :post }
data: { turbo_frame: 'podcasts_list' }
%>
</div>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

# Defines the root path route ("/")
root "podcasts#index"
post "load_podcasts", to: "podcasts#load_podcasts"
get "load_podcasts", to: "podcasts#load_list"

resources :podcasts
end

0 comments on commit 358d97a

Please sign in to comment.