user12796593
user12796593

Reputation:

How to increase a simple counter

This is my home.html.erb:

<div class="container">
    <% @articles.each do |article| %>
        <div class="bloc-article">
            <div class="article">
                <%= image_tag(article.picture, alt: "Article", width:"230", height:"240")%>
                <div class="align-content-column">  
                    <div class="celsuis"><span class="moins">- </span> <%= article.counter %>°  <span class="plus">+</span></div>
                    <div class="title">
                        <%= article.title %>
                    </div>
                        <h2><%= article.price %> €</h2>
                    <div class="desc-author">
                        <p><%= article.description[0, 200] %>....</p>
                        <div class="author-deal">
                            <h3><%= article.author %></h3>
                            <div class="alert alert-info"><%= link_to "Voir le Deal", article_path(article) %></div>
                        </div>
                    </div> 
                </div>
            </div> 
        </div>        
        <% end %>

</div>

I have a span - and a span +. I want put an onClick method to increase for + and decrease for -.

To do that I have a database with article.counter (auto-generate number with Faker::numb) With Vue.js I can do that easily, but on Ruby on Rails I am totaly lost.

Upvotes: 0

Views: 802

Answers (2)

askprod
askprod

Reputation: 129

Let's say you have a Model "Article" with a column "quantity". You could do something in the lines of this (I haven't tested it, just giving you a guideline)

As long as you use the normal resources in your routes:

In your view:

<%= form_for (@article), method: :put do |f| %>
  <%= f.button "-", name: "update", value: "decrement" %>
  <%= f.button "+", name: "update", value: "increment" %>
<% end %>

In your controller:

def update
  @article = Article.find(whatever you use to find it)

  if params[:update]["increment"]
    @article.quantity += 1
  elsif params[:update]["decrement"]
    @article.quantity -= 1
  end

# repond_to here and so on
# and don't forget to @article.save

end

You could then use jQuery (probably no need for an onClick event) to render the part where you show the count in the view.

Upvotes: 0

Aetherus
Aetherus

Reputation: 8888

You have lots of serious issues about understanding Rails applications (and web applications in general). Here are some hints:

  1. All Ruby code (including those snippets inside <% %>) is evaluated on the server.
  2. All JavaScript code in a Rails application is evaluated on the browsers.
  3. The server and the browsers are different computers, so they don't share memory and thus objects.
  4. A browser can't call Ruby methods as the Ruby code can only be evaluated on the server. A browser can only ask the server to do something via sending HTTP requests.
  5. When a request is finished and the web page is shown on the browser, the server knows the browser no more, and every bit of memory on the server that's associated with the request becomes garbage.

Upvotes: 2

Related Questions