vendredi 31 juillet 2015

rails 4 refresh only specific div with ajax

this is my second question on stackoverfloy. If i did a mistake, i'm sorry, i'm really new to here. So please be gentle.

I just learned RoR and i want to build a rails app for my GF's birthday. I have 2 models and 2 controllers. One of for image and the other one for the text.

I did randomize they in my images controller's index function to show in index.html And placed a button for refreshing the page so image and text randomize again.

But i want to add background music to this. So i need to use ajax for button to keep music from restarting over again. I did a research and tried many solutions. But i cant succeed.

Here is my controller:

class ImagesController < ApplicationController


    def index
      @randimg = Image.order("RANDOM()").first
      @randtext = Text.order("RANDOM()").first

      respond_to do |format|
        format.js 
        format.html
      end

Here is my images index.html.erb:

<!DOCTYPE html>
<html lang="tr">
<head>
  <title>2 Ağustos 1993</title>
  <meta charset="utf-8">
  <link href='http://ift.tt/1nio39n' rel='stylesheet' type='text/css'>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <%= javascript_include_tag "application" %>
</head>

<body>
  <div class="container">
    <%= render 'text' %>
    <%= render 'image' %>
  </div>
  <center>
    <%= link_to "İyi ki doğdun bebeğim, seni çok seviyorum",
              {:controller => 'images', :action => 'index'},
              :remote => true,
              class:'btn btn-info' %>
  </center>

  <audio autoplay="true" loop="true" preload="">
 <source src="bg.mp3" type="audio/mpeg"></source>
 Your browser does not support the audio element.
</audio>
</body>
</html>

And as far as i know i need to specify index.js.erb to use ajax, here it is;

$("#container").html("<%= j render :partial => 'image' %>")

I know my app is not the best or cleanest. This is my first app on rails. Thanks in advance.

PS: English is not my native language, i'm sorry if i type something mistaken.

Aucun commentaire:

Enregistrer un commentaire