weixin_33693070 2013-12-14 02:09 采纳率: 0%
浏览 17

Rails应用程序上的AJAX按钮

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
  • 写回答

2条回答 默认 最新

  • weixin_33717117 2013-12-14 16:56
    关注

    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
    
    评论

报告相同问题?