Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6326319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:04:02+00:00 2026-05-24T17:04:02+00:00

I want to make a blog application in Sinatra and DataMapper, my main application

  • 0

I want to make a blog application in Sinatra and DataMapper, my main application file is like this.

%w[rubygems sinatra data_mapper].each{ |r| require r }

DataMapper.setup(:default , ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/development.db")

class Post
  include DataMapper::Resource

  property :id,     Serial
  property :title,  String
  property :author,  String
  property :body,   Text

  has n, :comments
end

class Comment
  include DataMapper::Resource

  property :id,     Serial
  property :post_id,     Serial
  property :name,   String
  property :body,   Text

  belongs_to :post
end

helpers do
  def admin?
    request.cookies[settings.username] == settings.token
  end

  def protected!
    halt [401, 'Not Authorized'] unless admin?
  end
end


post '/comment/create' do
  comment = Comment.new(:name => params[:name], :body => params[:body])
  if comment.save
    status 201
    redirect '/post/'+post.id.to_s
  else
    status 412
    redirect '/'
  end
end

get '/post/:id' do
  @post = Post.get(params[:id])
  @comments = Comment.new
  erb :post
end

delete '/comment/:id' do
  Post.get(params[:id]).Comment.(params[:id]).destroy
  redirect '/post/'+post.id.to_s
end

DataMapper.auto_upgrade!

Now, my problem is how to set up the instance variable of comments
in the post show file, to make, delete, and show the comments.

Any suggestion and solutions would be welcomed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:04:02+00:00Added an answer on May 24, 2026 at 5:04 pm

    If I understand your question, you already most everything you need. Your post.erb file could look something like:

    <h1><%= @post.title %></h1>
    <%= @post.body %>
    
    <% @post.comments.each do |comment| %>
      <p><%= comment.name %><br /><%= comment.body %></p>
    
      <form action="/comment/<%= comment.id %>" method="post">
        <input type="hidden" name="_method" value="delete" />
        <input type="submit" value="Delete comment">
      </form>
    <% end %>
    

    To create new comments, simply add a POST form with the right fields that points to your /comments/create route.

    The reason I have that form with the “Delete” button in there is because your “delete ‘/comment/:id'” will be looking for the HTTP DELETE method. Unfortunately browsers don’t actually implement that. Looking for a “_method” field in a POST form is how Sinatra/Rails/Rack apps get around that. Then, near the top of your Sinatra app, you have to tell it to look for that “_method” field with:

    use Rack::MethodOverride
    

    Hopefully that’s what you were asking.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make an application which has Tab bar . In each TabBar
I want to see in action howto make blog on Eclipse + Java +
Let's say I want make some of my sources publicly available via my blog
Im starting to learn RoR and i want to make my personal blog in
I want to make an iPhone OS app for a Wordpress blog, and I'm
I want to make a Web application in Haskell (for example, it could be
I'm try to make a little blog application in ASP.NET MVC3 with C#. I
I want to make an application in android where i want to pull the
I want to make a little application. That will have register, save, update, delete,
Let's say I have a blog application. On each Post there are Comments. So,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.