Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions app/controllers/startups_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class StartupsController < ApplicationController
before_action :set_startup, only: [ :show, :like ]
before_action :set_startup, only: [ :show, :like, :edit, :update, :destroy ]
skip_before_action :authenticate, only: [ :index, :show, :search ]

def index
Expand All @@ -16,11 +16,49 @@ def new
@startup = Startup.new
end

def edit
unless @startup.user == Current.user
redirect_to root_path, alert: "You can only edit your own startups."
end
end


def update
unless @startup.user == Current.user
redirect_to root_path, alert: "You can only update your own startups."
return
end

if @startup.update(startup_params.except(:role_names))
submitted_titles = (startup_params[:role_names] || "").split(",").map(&:strip).uniq
@startup.roles.where.not(title: submitted_titles).destroy_all

existing_titles = @startup.roles.pluck(:title)
(submitted_titles - existing_titles).each do |title|
@startup.roles.create(title: title, status: :open)
end
redirect_to @startup, notice: "Startup was successfully updated."
else
flash.now[:alert] = "There was an error updating the startup."
render :edit, status: :unprocessable_entity
end
end

def destroy
unless @startup.user == Current.user
redirect_to root_path, alert: "You can only delete your own startups."
return
end

@startup.destroy
redirect_to startups_path, notice: "Startup was successfully deleted."
end

def create
@startup = Startup.new(startup_params.except(:role_names))
@startup.user = Current.user
if params[:startup][:role_names].present?
role_titles = params[:startup][:role_names].split(",")
if startup_params[:role_names].present?
role_titles = startup_params[:role_names].split(",")
role_titles.each do |title|
@startup.roles.build(title: title.strip, status: :open) # or your default status
end
Expand Down
6 changes: 6 additions & 0 deletions app/views/startups/_actions.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<% if Current.user && (Current.user == startup.user) %>
<div class="flex gap-5 items-center my-2 justify-end">
<%= link_to 'Edit Startup', edit_startup_path(startup), class: button_classes.render %>
<%= link_to 'Delete Startup', startup, method: :delete, data: {turbo_method: :delete, turbo_confirm: "Are you sure to delete #{startup.name}?"}, class: "bg-red-400 text-red-400 hover:bg-red-400 #{button_classes.render} " %>
</div>
<% end %>
47 changes: 47 additions & 0 deletions app/views/startups/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<div class="container mx-auto p-6 lg:px-8">
<div class="md:flex md:items-center md:justify-between">
<div class="min-w-0 flex-1">
<h2 class="text-2xl/7 font-bold sm:truncate sm:text-3xl sm:tracking-tight">Post your startup</h2>
<p class="mt-2 text-base/6 text-black">Showcase your dream here and get others excited to work with you.</p>
</div>
</div>
<%= form_with(model: @startup) do |form| %>
<% if @startup.errors.any? %>
<div class="mb-6 rounded-sm border-2 border-red-500 p-4 text-black">
<h3 class="font-semibold">Please fix the following errors:</h3>
<ul class="mt-2 list-disc list-inside">
<% @startup.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="mt-8 space-y-6">
<div>
<%= form.label :name, class: "block text-sm font-medium" %>
<%= form.text_field :name, class: input_classes.render %>
</div>
<div>
<%= form.label :description, class: "block text-sm font-medium" %>
<%= form.text_area :description, rows: 5, class: input_classes.render %>
</div>
<div>
<%= form.label :tag_list, "Tags" %>
<div class="flex items-center">
<%= form.text_field :tag_list, value: @startup.tag_list.join(", "), placeholder: "Get some AI buzzwords in here to build hype...", class: "tag-input #{input_classes.render}", data: { controller: "tag", action: "keydown->tag#preventEnter" } %>
</div>
</div>
<div>
<%= form.label :role_names, "Collaborators" %>
<div class="flex items-center">
<%= form.text_field :role_names, value: @startup.roles.map(&:title).join(','), placeholder: "Need a tech guru? Designer?", class: "tag-input #{input_classes.render}", data: { controller: "tag", action: "keydown->tag#preventEnter" } %> </div>
</div>
<div>
<%= form.label :url, "Website (optional)", class: "block text-sm font-medium" %>
<%= form.url_field :url, class: input_classes.render, placeholder: "https://your-startup-website.com" %>
</div>
<div>
<%= form.submit "Edit Startup", class: button_classes.render %>
</div>
<% end %>
</div>
1 change: 1 addition & 0 deletions app/views/startups/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<div class="container mx-auto px-6 py-8 lg:px-8">
<%= render 'actions', startup: @startup %>
<div class="bg-white border border-black p-6 rounded-xs">
<div class="flex justify-between items-center mb-6">
<div>
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
get "/auth/:provider/callback", to: "sessions/omniauth#create"
post "/auth/:provider/callback", to: "sessions/omniauth#create"
get "up" => "rails/health#show", as: :rails_health_check
resources :startups, only: [ :index, :new, :create, :show ] do
resources :startups do
member do
post :like
end
Expand Down
Loading