Skip to content

Commit

Permalink
Added :address to blogs. Changes made to make :address the slug for b…
Browse files Browse the repository at this point in the history
…logs. app/models/blog.rb validations such that we dont recieve errors.
  • Loading branch information
TimeTravelerFromNow committed Feb 18, 2024
1 parent be8dc23 commit 23ff265
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app/controllers/blogs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ def destroy
private
# Use callbacks to share common setup or constraints between actions.
def set_blog
@blog = Blog.find(params[:id])
@blog = Blog.find_by!(address: params[:address])
end

# Only allow a list of trusted parameters through.
def blog_params
params.require(:blog).permit(:title, :description, :story, :header_image)
params.require(:blog).permit(:address, :title, :description, :story, :header_image)
end
end
7 changes: 7 additions & 0 deletions app/models/blog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,11 @@ class Blog < ApplicationRecord
has_rich_text :story

has_one_attached :header_image

validates :address, presence: true
validates_uniqueness_of :address

def to_param
address
end
end
5 changes: 5 additions & 0 deletions app/views/blogs/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
</div>
<% end %>

<div>
<%= form.label :address, style: "display: block" %>
<%= form.text_field :address %>
</div>

<div>
<%= form.label :title, style: "display: block" %>
<%= form.text_field :title %>
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Rails.application.routes.draw do
resources :blogs
resources :blogs, param: :address
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# Defines the root path route ("/")
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240216021857_add_address_to_blogs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddAddressToBlogs < ActiveRecord::Migration[7.0]
def change
add_column :blogs, :address, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 23ff265

Please sign in to comment.