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

报告相同问题?

悬赏问题

  • ¥30 求一下解题思路,完全不懂。
  • ¥30 关于#硬件工程#的问题:求一下解题思路
  • ¥15 运筹学对偶单纯行法构造扩充问题
  • ¥20 XP系统的老电脑一开机就提示找不到rundll.exe,付费求解
  • ¥15 milvus查询出来的score怎么转换成0-1之间的相似性
  • ¥15 多ip服务器站群如何搭建l2tp服务器
  • ¥15 lvgl V9移植到linux开发板
  • ¥15 VB.net中在窗体中创建一个button控件来关闭窗体,但是提示错误,我该怎么办
  • ¥15 网上下载好的程序但是arduinoIDE编程报错,运行不了,哪里出错了,能具体给改一下吗
  • ¥15 Sharepoint JS开发 付费技术指导