This is my app here: http://mighty-brushlands-6367.herokuapp.com/ It displays jokes chosen randomly from the database. But then I want this to be done by AJAX without refresh. So I'm looking for some help to learn how to do this. This is how the button looks in view. I'm using Twitter Bootstrap.
<div><blockquote class="pull-left">
<p id="mycontainer"><%= @l.content %> </p>
<small><%= @l.author %> </small>
<button class="btn btn-mini", "pull=right" type="button">Następny -></button>
</blockquote></div>
</div>
But now I'm quite confused here. Cause I don't really know how to add the code to the button which is inside a bootstrap tag. I check this post Attaching functionality to a button in rails 3 and others on google but they are all dealing with forms and not a button. Any help please? I'm a newbie :D
If it helps my controller looks like this:
class KalendarController < ApplicationController
def next
@l = Joke.find_by(id: rand(1..Joke.count))
#@l = Joke.find_by_id(@l)
end
def show
#random_id = Joke.all.sample
@l = Joke.find_by(id: rand(1..Joke.count))
#@l = Joke.find_by_id(@l)
@t = Time.now.strftime("%A")
@week = Date.today.cweek % 2 == 0 ? "#{trans} " "PARZYSTY" : "NIEPARZYSTY"
end
def trans
@t = Time.now.strftime("%A")
if @t == "Monday"
"Poniedziałek"
elsif @t == "Tuesday"
"Wtorek"
elsif @t == "Wednesday"
"Środa"
elsif @t == "Thursday"
"Czwartek"
elsif @t == "Friday"
"Piątek"
elsif @t == "Saturday"
"Sobota"
elsif @t == "Sunday"
"Niedziela"
end
end
def rnd
(0..4).to_a.sample
end
end
I don't expect to get the answer on this one because I just wanted to walk you through rewriting your code with a little more refinement on the day-of-the-week stuff:
def trans
days = ["Niedziela", "Poniedziałek", "Wtorek", "Środa", "Czwartek", "Piątek", "Sobota"]
@t = Date.today.wday
days[@t]
end
Here is a gist I created showing the pertinent parts of your application to simply use button_to and a js template to update the view
https://gist.github.com/andyh/0c28abb7124fb283a924
The only thing missing is setting the classes on the button itself