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?
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 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 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
I'm adding search functionality to my ruby on rails app. Based on the Ryan Bates tutorial #111.
For some reason, my render call in my show page is resulting in
Name Error
uninitialized constant Search::Properties
My controller is simple
class SearchesController < ApplicationController
def new
#search = Search.new
end
def create
#search = Search.create(search_params)
redirect_to #search
end
def show
#search = Search.find(params[:id])
end
private
def search_params
params.require(:search).permit(:keywords, :category, :min_price, :max_price, :bedrooms, :bathrooms, :garage)
end
end
And I created a new class in my model
class Search < ActiveRecord::Base
def properties
properties = Properties.all
properties = properties.order(:title)
properties = properties.where(["title LIKE ?","%#{keywords}%"]) if keywords.present?
properties = properties.where([category: category]) if category.present?
properties = properties.where(["price >= ?","%#{min_price}%"]) if min_price.present?
properties = properties.where(["price <= ?","%#{max_price}%"]) if max_price.present?
properties = properties.where(["bedrooms == ?","%#{bedrooms}%"]) if bedrooms.present?
properties = properties.where(["bathrooms == ?","%#{bathrooms}%"]) if bathrooms.present?
properties = properties.where(["garage == ?","%#{garage}%"]) if garage.present?
properties
end
end
And my view
<div class="row property-list-wrapper">
<div class="container">
<div class="col-md-8 property-list-left">
<h1>We've found <%= pluralize(#properties.count, 'property')%> </h1>
<%= render #properties %>
</div>
<div class="col-md-4 property-list-right">
</div>
</div>
</div>
It seems that my render tag is not finding the class in my model. Am I missing something in my controller maybe?
I'm using rails 4.2.7 and ruby 2.2.4p230
UPDATE: The answer bellow is correct. Just wanted to update what I did to solve the problem.
First I had to delete all my previous lines in the rails console.
Then I used the bye bug gem in my lines controller at the bottom of the create method to discover where the next bug occurred. I created a test line that I needed to delete again. so I ran
Line.last.delete in console.
This is the way my lines controller create method looks now (working no bugs)
def create
if user_signed_in?
#line = Line.create(line_params)
if #line
if params[:line][:previous_line_id].empty?
#line.story = Story.create
#line.save
else
#line.story = #line.previous_line.story
#line.save
end
redirect_to line_path(#line)
else
flash[:error] = #line.errors
redirect_to line_path(Line.find(params[:line][:previous_line_id]))
end
else
Finally I ran #Lines.each { |line| line.update.attribute(:story_id: 3)}
This gave the necessary association between lines and story.
ORIGINAL POST BELLOW.
I'm getting this error in my rails app. I think that when I create a new line or start a story, it doesn't automatically add it to a story object. I've listed my show.html.erb file as well as my lines controller.rb file.
What am I missing? How do I get the controller to add data to the story object correctly?
Thanks!
I added a few lines of code to my lines controller:
class LinesController < ApplicationController
def new
params[:previous_line_id].nil? ? #line = Line.new : #line = Line.find(params[:previous_line_id]).next_lines.create
#lines = #line.collect_lines
#ajax = true if params[:ajax]
render :layout => false if params[:ajax]
if #line.previous_line
#line.update_attribute(:story_id, #line.previous_line.story.id)
else
story = Story.create
#line.story = story
#line.save
end
end
def create
if user_signed_in?
#line = Line.create(line_params)
if #line
redirect_to line_path(#line)
else
flash[:error] = #line.errors
redirect_to line_path(Line.find(params[:line][:previous_line_id]))
end
else
flash[:error] = "Please sign in or register before creating a line!"
unless params[:line][:previous_line_id].empty?
redirect_to line_path(Line.find(params[:line][:previous_line_id]))
else
redirect_to root_path
end
end
end
# params[:id] should correspond to the first line of the story.
# if params[:deeper_line_id] is not nil, that means that they want to render up to the nested line id
def show
#lines = Line.find(params[:id]).collect_lines
#next_lines = #lines.last.next_lines.ranked
#lines.last.update_attribute(:score, #lines.last.score + 1)
end
def select_next
#line = Line.find(params[:id])
#line.update_attribute(:score, #line.score + 1)
#lines = [#line]
#next_lines = #line.next_lines.ranked
render :layout => false
end
def send_invite
if user_signed_in?
UserInvite.send_invite_email(current_user,Line.find(params[:id]), params[:email]).deliver
flash[:notice] = "Your invite was sent!"
else
flash[:error] = "Please sign in"
end
redirect_to Line.find(params[:id])
end
private
def line_params
params.require(:line).permit(:text, :previous_line_id, :user_id)
end
end
I added these lines to the controller pictured above
if #line.previous_line
#line.update_attribute(:story_id, #line.previous_line.story.id)
else
story = Story.create
#line.story = story
#line.save
end
Here is my show.html.erb file
<div class="row">
<div class="col-lg-2">
</div>
<div class="box-container col-lg-7 ">
<div id="story" class="box">
<% #lines.each do |line| %>
<span class="story-line" data-id="<%=line.id%>"><%= link_to line.text, '#', :class=>"story-line" %></span>
<% end %>
</div>
<div id="next-steps">
<%= render 'next_steps' %>
</div>
<span style="font-size:.9em; margin-bottom:15px; display:block;">*If the links don't work, try refreshing.</span>
</div>
<div class="col-lg-2" style="padding-right:25px;">
<%= render 'invite' %>
Your Fellow Collaborators: <br />
<div class="collaborators">
<% #lines.last.story.collaborators.uniq.each do |collaborator| %>
<%= link_to profile_path(:id => collaborator.id) do %>
<%= image_tag collaborator.profile_image_uri, :class => "prof-icon" %>
<% end %>
<% end %>
</div>
Story model
class Story < ActiveRecord::Base
has_many :lines
has_and_belongs_to_many :collaborators, :class_name => "User", :join_table => "collaborators_stories", :association_foreign_key => :collaborator_id
def first_line
self.lines.first_lines.first_lines.first
end
end
Here is my lines.rb file
class Line < ActiveRecord::Base
scope :first_lines, -> { where previous_line_id: nil}
scope :ranked, -> { order("score + depth DESC")}
belongs_to :user
belongs_to :story
belongs_to :previous_line, :class_name => "Line", :foreign_key => "previous_line_id"
has_many :next_lines, :class_name => "Line", :foreign_key => "previous_line_id"
validates_presence_of :text
after_create :update_depths
def update_depths
line = self.previous_line
while !line.nil?
line.update_attribute(:depth, line.depth + 1)
line = line.previous_line
end
end
def first_line
line = self
while !line.previous_line.nil?
line = line.previous_line
end
line
end
def collect_lines
line = self
lines = [self]
while !line.previous_line.nil?
lines.unshift(line.previous_line)
line = line.previous_line
end
lines
end
end
Problem is orphaned lines in your database. Look for them and associate it to a story, or delete it:
How to find orphaned records:
http://antonzolotov.com/2013/01/26/how-to-find-and-delete-orphaned-records-with-ruby-on-rails.html
Then review the create method to ensure a line should be part of a story:
#short example review activerecord relations
#story = Story.find(params[:story_id])
story.lines.create(line_params)
That should work.
EDIT:
def self.find_orphan_ids
Lines.where([ "user_id NOT IN (?) OR story_id NOT IN (?)", User.pluck("id"), Story.pluck("id") ]).destroy_all
end