I have an application using Rails 5 and I have an article resource and I'm implementing "like/unlike" function. I already got that function but I'm having troubles on updating the #artlike variable on the view. These are my files:
show.html.erb:
<div class="pull-right">
<i class="fa fa-diamond"></i> <span id="likes"><%= render partial: "likes", locals: {artlike: #artlike} %></span>
</div>
Controller:
respond_to :js
def like
puts "LikesController:like"
#usuario = current_usuario
#articulo = Articulo.find(params[:id])
#usuario.like!(#articulo)
#artlike = Like.where(articulo_id: #articulo.id).count
end
def unlike
puts "LikesController:unlike"
#usuario = current_usuario
#like = #usuario.likes.find_by_articulo_id(params[:id])
#articulo = Articulo.find(params[:id])
#like.destroy!
#artlike = Like.where(articulo_id: #articulo.id).count
end
_likes.html.erb:
<span id="likes"><%= #artlike %></span>
like.js.erb File
$('span#likes').html('<%= j render partial: "likes", locals: {artlike: #artlike} %>');
How can I do the #artlike value in the view?
This is the ouput in the console log:
Console Log
Related
I am using Ruby on Rails 5.2.3 and Mongoid 7.0
I need to be able to sort multiple models (Item and Text) in one cotroller.
Now only Item or Text is sorted, it is necessary that Position was set in relation to each other.
class UsersController < ApplicationController
def sort
params[:item].each_with_index do |id, index|
Item.where(id: id).update_all(position: index + 1)
end
head :ok
end
def admin
#user_items = #user.user_feed
end
end
admin.html.erb
<div id="items" data-url="<%= sort_users_path %>">
<%= render partial: 'users/user_item', collection: #user_items %>
</div>
_user_item.html.erb
<% if user_item[:title].present? %>
<div id="item_<%= user_item[:id] %>">
<%= user_item[:position] %>
</div>
<% end %>
<% if user_item[:text].present? %>
<div id="item_<%= user_item[:id] %>">
<%= raw user_item[:position] %>
</div>
<% end %>
User.rb
def user_activity
activity_items = []
items.each do |item|
activity_item = {}
activity_item[:id] = item.id
activity_item[:url] = item
activity_item[:title] = item.title
activity_item[:position] = item.position
activity_item[:item_link] = item.url
activity_items << activity_item
end
texts.each do |text|
activity_item = {}
activity_item[:id] = text.id
activity_item[:url] = text
activity_item[:text] = text.text
activity_item[:position] = text.position
activity_items << activity_item
end
activity_items.sort_by! { |activity_item| activity_item[:position] }
activity_items
end
def user_feed
activity_items = user_activity
activity_items.sort_by! { |activity_item| activity_item[:position] }
activity_items
end
Just needed to add Text.where(id: id).update_all(position: index + 1)
def sort
params[:item].each_with_index do |id, index|
Item.where(id: id).update_all(position: index + 1)
Text.where(id: id).update_all(position: index + 1)
end
head :ok
end
I'm currently trying to display the first record. The record is updated when someone decides to buy a wine. How would I get the first record for the specific wine ?
I thought of using .find(1)? but how would implement this into my controller ?
My current controller:
def create
wine = Wine.find(params[:wine_id])
if current_user == wine.user
flash[:alert] = "Du kannst nicht deinen eigenen Wein kaufen!"
else
start_date = Date.parse(reservation_params[:start_date])
#reservation = current_user.reservations.build(reservation_params)
#reservation.wine = wine
#reservation.price = wine.price
#reservation.in_stock = wine.in_stock - #reservation.bottle
#reservation.total = wine.price * #reservation.bottle
wine.update(in_stock: #reservation.in_stock)
# #reservation.save
if #reservation.save
if wine.Reservieren?
flash[:notice] = "Reserviert!"
else
#reservation.Versendet!
flash[:notice] = "Eroflgreich bestellt!"
end
else
flash[:alert] = "Can't make a reservation!"
end
end
redirect_to wine
end
My current view:
<% #wines.each do |wine| %>
<div class="row">
<div class="col-md-3">
<%= link_to wine.wine_name, wine_path(wine) %>
</div>
<div class="col-md-3">
<%= wine.in_stock %> <!-- Display first record -->
</div>
<div class="col-md-3">
<% if !wine.in_stock.blank? %>
Aktiv
<% else %>
Nicht Aktiv
<% end %>
</div>
<div class="col-md-3 right">
<%= link_to "Update", details_wine_path(wine), class: "btn btn-form" %>
</div>
</div><!-- row -->
<hr/>
<% end %>
Maybe you can explain a little better your code, because you receive #wines in your view and it is not coming from this controller action that you posted.
But if you want to take the first register you can basically call wines.first.
You would change from <%= wine.in_stock %> to <%= #wines.first %> for example.
Here is some thread related:
Display first record in rails
I have this project: https://todo-app-back-end-prospeed.c9users.io/
It's basically a reminder web app. I trying to add a function in which the user clicks on the checkbox and the value <Todo id: 24, description: "Publish project", pomodoro_estimate: 1, complete: nil the complete: nill changes to complete: true or false `
<body>
<div class="container2">
<h1 id ="list">Workshop Todo List</h1>
<ul>
<% #todos.each do |todo| %>
<li>
<label class="container">
<input type="checkbox"/ checked="checked">
<%= todo.description %>
<span class="pomodoro-estimate"><%= todo.pomodoro_estimate %> pomodoro</span>
<span class="checkmark"></span>
</label>
</li>
<% end %>
</ul>
<a class="noColor" href="/new/"><input class="add-new-todo-button" type="submit" value="Add todo"/></a>
<h1 id="Pomodoro-technique">What is a pomodoro estimate?</h1>
<p id="Pomodoro-technique-info">
<strong>1 pomodoro = 25 minutes + 5 minutes break.</strong><br>
For more info: https://www.focusboosterapp.com/the-pomodoro-technique
</p>
</div>
</body>
ToDoController:
class TodoController < ApplicationController
def index
#todos = Todo.all
end
def show
# Gets number user typed in URL
todo_id = params[:id]
#Grab the todo with that id from the database
#todo = Todo.find_by_id(todo_id)
end
def new
#
end
def create
t = Todo.new
t.description = params['description']
t.pomodoro_estimate = params['pomodoro_estimate']
t.save
redirect_to "/show/#{t.id}"
end
def destroy
t = Todo.find_by_id(params[:id])
t.destroy
redirect_to "/"
end
def edit
#todo = Todo.find_by_id(params[:id])
end
def update
t = Todo.find_by_id(params['id'])
t.description = params['description']
t.pomodoro_estimate = params['pomodoro-estimate']
t.save
redirect_to "/show/#{t.id}"
end
end
What kind of controller would I need? How does it know when It is clicked?
I would just like to be pointed in the right direction
Your checkbox in your view is not good I think. You need something like:
<%= f.check_box: field, {}, true %>
I added this to my controller:
class HtmlreportController < ApplicationController
def index
#report = Report.new
#report_presenter = ReportPresenter.new(#report_presenter)
end
end
Then added this presenter to /app/presenters
# app/presenters/htmlreport_presenter.rb
class ReportPresenter
def initialize(report)
#report = report
end
def pass_fail(view)
arrs = ['onemon.rb','twomon.rb','threemon.rb','fourmon.rb','fivemon.rb']
arrs.each do |arr|
shortname = File.basename("#{arr}", ".rb")
newest_file = Dir.glob("ScriptResults/#{shortname}/*").max
#reporter = File.readlines("/Users/username/Automation/Code/Reports/MonthlyTracking/#{newest_file}")
if #reporter.grep(/test failed/).any?
view.concat content_tag(:div, 'FAILED', class: 'results_fail')
else
view.concat content_tag(:div, 'PASSED', class: 'results_pass')
end
end
end
end
With this in my view:
<% title "HTML Report" %>
<!-- This is where the HTML Report lies -->
<h1>HTML Report for Marines.com Daily Monitoring</h1>
<div>View the grids below for the following results:</div>
<div id="results">
<div class="results_grid">
<div class="results_title">Xbox</div>
<%= #report_presenter.show_credentials(self) %>
</div>
</div>
But, I get this error when running it: uninitialized constant HtmlreportController::Report for the line #report = Report.new
How do I get it initialized to make it recognize the functions in my presenter into my view?
I have this link in Rails:
<%= link_to "Add to Journal", add_post_journal_path(post), :method => :put %>
However I want transform this link to show a fancybox with the content listing my content to choose. First, I use this code:
<%= link_to "fancy", "#add_post", :class=>"fancybox" %>
but I have errors, because I want pass the actual post to fancybox, so I'm using this code: in add_post.html.erb:
<h1>Escolha o Jornal que deseja adicionar:</h1>
<ul>
<% current_user.journals.each do |journal| %>
<li><%= link_to journal.name,add_post_complete_journal_path(journal),:remote=>true %> </li>
<% end %>
</ul>
and my controller is:
def add_post
#journal_post = JournalsPosts.new
session[:post_add] = params[:id]
end
def add_post_complete
#journal_post = JournalsPosts.create(:post_id => session[:post_add],:journal_id => params[:id])
respond_with #journal_post
end
How can I transform this code to use my content in my fancybox?
Add on your action add_post the next respond with js:
def add_post
#journal_post = JournalsPosts.new
session[:post_add] = params[:id]
respond_to do |format|
format.js
end
end
Add on a file on your views add_post.js.erb with the next content:
$.fancybox('<%= escape_javascript(render(:partial => 'path_to/add_post'))%>',
{
openEffect: "fade",
closeEffect: "fade",
autoSize: true,
minWidth: 480,
scrolling: 'auto',
});
For example, you have add a partial _add_post.html.erb on your views. Now inside this partial you can write your code view:
#code for your view inside partial `add_post.html.erb`
<%= #journal_post %>
<h1>Escolha o Jornal que deseja adicionar:</h1>
<ul>
.
.
Regards!