0

OK, so first question: do I need an index function in my Controller? This would call .all method on my Class? Can I do away with this? Would a show function in the Controller suffice? In which case, the function reads as follows?

def show
    @quotation = Quotation.find(params[:id])
end

And consequently, on the show.html.erb:

<div class="card-category">
<%= @quotations.text %>
<button>
    <%= link_to "More ➜", quotation_path%>
</button>

Thank you!

3 Answers 3

0

You can google your type of database to see the best way to select a random record. For example, in postgreSQL...

@quotation = Quotation.order('random()').take

For something database-independent...

@quotation = Quotation.find(Quotation.pluck(:id).sample)
0
0

Do I need an index function in my Controller?

No, it is not necessary. But don't forget do delete it from routes if you want to disable it.

  resources :quotes, except: :index

This would call .all method on my Class?

You can do whatever you want.

Can I do away with this?

But if you want it to be in common way, it should respond with many resources.

How do I SHOW a randomised sample of my seeds?

I think the best option would be is to have another controller action for this

def random
  # ...
end

And a route to your routes.rb.

  get '/quotes/random', to: 'quotes#random'
0
0

for the short just use:

Quotation.find(Quotation.ids.sample)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.