Reputation:
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
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
Reputation: 8888
You have lots of serious issues about understanding Rails applications (and web applications in general). Here are some hints:
<% %>
) is evaluated on the server.Upvotes: 2