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

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建