Tensai
Tensai

Reputation: 43

How can I access data stored in a Rails app, that I created, on a seperate website?

I have recently created a Rails application to handle parent comments for a company I work for. Each comment is given a score (1-10) by a moderator, I am trying to create a window on an html page that will randomly display a comment in the database as long as it has a score of at least 7. The rails app also has User authorization requirements to be able to view the database of comments.

I know this is possible but I only recently learned Ruby on Rails and am rather new to the html world as well. I am extremely willing to learn something like jquery or ajax if that is what is needed to make this work. I just need some direction so I can get started displaying the comments. Thank you for any help!

Upvotes: 1

Views: 97

Answers (2)

AJcodez
AJcodez

Reputation: 34166

Perhaps you could have an action in your CommentsController like this:

def random_high_rated
  @comment = Comment.where(score: 7..10).sample
end

Your route in config/routes would look something like this:

resources :comments do
  collection do
    get '/random_high_rated'
  end
end

And you would access it at:

yoursite.com/comments/random_high_rated

Make a view at app/views/comments/random_high_rated.html.erb

And in it:

<%= @comment %>

EDIT:

Read through this tutorial and you'll be able to write your own code in no time!

http://ruby.railstutorial.org/ruby-on-rails-tutorial-book

Upvotes: 1

daveyfaherty
daveyfaherty

Reputation: 4613

Option 1: Make a unique page in your Rails website (lets call it site no.1), display it in an iframe on site no.2

Option 2: Make a url in site no.1 that gives you JSON, XML, or even just some html, and use an AJAX call from site no.2 to pull it in.

If you've never used AJAX before, you might find using jQuery makes it easier to work with. See the jQuery AJAX Methods for more information. Other libraries might be better, but that's the one I've used.

Sorry if the answer is a bit general, but it's a bit of a general question.

Upvotes: 0

Related Questions