If link_to is clicked flash message on new page? - ruby-on-rails

view
<% if current_user.challenges.badges.count > 0 %>
<%= link_to new_duel_path(:challenge_daddy => #user.id), class: "btn", style: "padding-top: 10px; padding-bottom: 10px; margin-top: 15px; color: white;" do %>
<span class='glyphicon glyphicon-tower'></span> Duel
<% end %>
<% else %>
# user will be sent to performance page
<%= link_to performance_path, class: "btn", style: "padding-top: 10px; padding-bottom: 10px; margin-top: 15px; color: white;" do %>
<span class='glyphicon glyphicon-tower'></span> Duel
<% end %>
<% end %>
controller
def performance
#user = current_user
# only if the user is brought to performance via the above link_to should this be flashed
flash[:alert] = "YOU'RE NOT AUTHORIZED TO INITIATE A DUEL UNTIL YOU BECOME A NINJA, BUT YOU CAN BE INVITED TO A DUEL"
end
I tried to send the flash directly in link_to, but nothing happened: link_to performance_path(:alert => "Flash message") also tried (:error => "Flash message")

view
<%= link_to performance_path(alert: true), class: "btn", style: "padding-top: 10px; padding-bottom: 10px; margin-top: 15px; color: white;" do %>
<span class='glyphicon glyphicon-tower'></span> Duel
<% end %>
controller
def performance
#user = current_user
flash[:alert] = "YOU'RE NOT AUTHORIZED TO INITIATE A DUEL UNTIL YOU BECOME A NINJA, BUT YOU CAN BE INVITED TO A DUEL" if params[:alert].present?
end
performance view
<% if flash[:alert] %>
<%= flash[:alert] %>
<% end %>

Related

Index page is not re rendering/refreshing on search in Rails

I am making a simple search in rails on the index page. My problem is that on debug I get the correct results but they don't show on the frontend.
def index
if !params[:category].present? and !params[:title].present?
#services = Service.take(4)
else
if params[:category] != "Find Service" and params[:title].present?
#services = Service.where(category: params[:category], title: params[:title]).take(4)
elsif params[:category] == "Find Service" and params[:title].present?
#services = Service.where(title: params[:title]).take(4)
elsif params[:category] != "Find Service" and !params[:title].present?
#services = Service.where(category: params[:category]).take(4)
else
#services = Service.take(4)
end
end
end
My view index.html.erb
<%= form_with(url: "services", method: "get") do |form| %>
<div class="banner-input" style="margin-top">
<div class="listing-sort listing-sort--main-listing" style="width: 50%;float: left; border-radius: 6px; border: 2px solid white; background: transparent; padding: 0px;">
<input type="submit" value="" class="main-listing__form-field agent-submit" style="width: 10% ;float:right">
<%= form.text_field "title", id: "search_agent", class: "main-listing__form-field ", placeholder: "Search by Name or Location...", style: "width: 75% ;float:right; background: transparent; color: white!;" %>
<div class="listing-sort__inner" style="float:left;margin: 0px; width: 25%; padding: 0px;">
<p class="listing-sort__sort">
<%= form.select("category",options_for_select(["Find Service", "Assembly", "Carpet Cleaning", "Electrical", "House Cleaning", "HVAC", "Handyman", "IT", "Junk Removal", "Lawn Care","Moving", "Painting", "Plumbing", "Staging", "Photography", "Videography", "Inspectors"], :selected => "Find Service"), {include_blank: false}, { :class => 'ht-field listing-sort__field' }) %>
</p><!-- .listing-sort__sort -->
</div><!-- .listing-sort__inner -->
</div><!-- .listing-sort -->
</div>
<% end %>
<section style="margin-top: 50px">
<h2 style="margin-left: 15px; padding-bottom: 30px; font-size: 24px;">Recently viewed & more</h2>
<% #services.each do |service| %>
<%= render 'services/service_card', :f => service %>
<% end %>
</section>
<section class="jumbotron"style="margin-top: 50px">
<h2 style="margin-left: 15px; padding-bottom: 30px; font-size: 24px;">Pro SEM services
</h2>
<% #services.each do |service| %>
<%= render 'services/service_card', :f => service %>
<% end %>
</section>
This is _service_card
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-3" style="padding-bottom: 10px">
<div class="boxes">
<div class="box-img">
<img src="assets/MobileApps.png">
</div>
<div class="user-detail-main">
<div class="user-detail-img">
<img src="assets/MobileApps.png">
</div>
<div class="user-detail">
<a><h3><%= f.user.profile.first.first_name %></h3></a>
<p>Level 1 Seller</p>
</div>
<div class="user-detail-para">
<% byebug %>
<p><%= f.description %></p>
</div>
<div style="padding-top: 50px;">
<i class="fas fa-star" style="color: #ffbf00"></i> (5)
</div>
</div>
<div class="user-detail-footer">
<div class="heart-icon">
<i class="fas fa-heart" style="color: #c6c6c6"></i>
<i class="fas fa-bars" style="color: #c6c6c6"></i>
</div>
<div class="user-value">
<p>Starting At $ <%= f.packages.first.price%></p>
</div>
</div>
</div>
Please help me regarding this. Looking forward. It is working fine without search, but with search it doesn't update the results and show same results.

rake aborted! ActionView::Template::Error: undefined method 'challenges' for nil:NilClass

I run this rake:
task monthly_report: :environment do
User.all.each do |user|
if user.challenges.present? && user.challenges.any?
UserMailer.monthly_report(user).deliver_now
end
end
end
Now maybe in the each method itself is there a way to not send to user's who don't have any challenges?
I keep getting this error:
ActionView::Template::Error: undefined method 'challenges' for nil:NilClassafter I run in productionheroku run rake monthly_report
A user has_many challenges and challenges belongs_to user.
user_mailer.rb
def monthly_report(user)
#user = user if user.email.present?
mail to: user.email, subject: "Sorry! Please Ignore | Testing Email - Monthly Challenges Report"
end
monthly_report.html.erb
Accomplished: <%= #user.challenges.accomplished.count %> Ongoing: <%= #user.challenges.unaccomplished.count %>
<% if #user.challenges.accomplished.present? %>
<% #user.challenges.accomplished.order("deadline ASC").each do |challenge| %>
<% if challenge.date_started != nil %>
<%= link_to challenge_url(challenge) do %>
<%= challenge.name %> <%= challenge.committed_description %> for <%= challenge.days_challenged %> Days
<% end %>
<% if challenge.deadline.present? %>
<span style="display: inline; padding: .2em .6em .3em; font-size: 75%; font-weight: bold; line-height: 1; color: #fff; text-align: center; white-space: nowrap; vertical-align: baseline; border-radius: .25em; background-color: #446CB3; text-decoration: none;"><%= challenge.deadline.strftime("%b %d, %Y") %></span>
<% end %>
<% else %>
<%= link_to challenge_url(challenge) do %>
<%= challenge.name %>
<% end %>
<% if challenge.deadline.present? %>
<span style="display: inline; padding: .2em .6em .3em; font-size: 75%; font-weight: bold; line-height: 1; color: #fff; text-align: center; white-space: nowrap; vertical-align: baseline; border-radius: .25em; background-color: #446CB3; text-decoration: none;"><%= challenge.deadline.strftime("%b %d, %Y") %></span>
<% end %>
<% end %>
<% end %>
<% else %>
None.
<% end %>
Please update your rake task
task monthly_report: :environment do
User.all.each do |user|
if user.email.present? && user.challenges.present? && user.challenges.any?
UserMailer.monthly_report(user).deliver_now
end
end
end
I think error raising if your user dose not have any email in th record try this

Textarea submitting ruby on rails

When I use this, it works
<%= form_for(#patient, url: patient_add_comment_path(#patient)) do |f| %>
<%= f.text_area :doctor_comment, cols: 65, rows: 3, style: "resize: none; border: 2px solid #888; background-color:#FFF; font-size: 16px; margin-top:0px; margin-left:5px;", placeholder: "doktor comment.." %>
<%= f.text_area :hidden_comment, cols: 65, rows: 3, style: "resize: none; border: 2px solid #888; background-color:#FFF; font-size: 16px; margin-top:0px; margin-left:5px;", placeholder: "doktor hidden comment.." %>
<%= f.submit "Kaydet", class: "btn btn-circle green-haze btn-sm", style: "margin-top: 5px;"%>
<% end %>
But I wanted to use text editor for this for using the class of 'summernote_1'
<div class="col-md-12">
<%= form_for(#patient, url: patient_add_comment_path(#patient)) do |f| %>
<div class="col-md-6">
Doktor Comment <%= f.text_area :doctor_comment, class: "summernote_1"%>
</div>
<div class="col-md-6">
Hidden Comment <%= f.text_area :hidden_comment, class: "summernote_1"%>
</div>
<%= f.submit "Save", class: "btn btn-circle green-haze btn-sm", style: "margin-top: 5px;"%>
<% end %>
</div>
Bu when I click Save button, the link try to send and it does not update, how can I solve this problem?
http://localhost:3000/doctors/2?utf8=✓&_method=patch&authenticity_token=OPPiCvNbcONDyHzzEQ%3D%3D&patient[doctor_comment]=asdfadsfasdfasdfasdfa&patient[hidden_comment]=asdfadsfasd&commit=Save

Tree menu using ancestry in rails

according to this answer https://stackoverflow.com/a/11335283/2397494
i'm trying to make tree menu for my appliction
this answer working only for root and first children.
I'm looking for suggestion to transform this code, work well with n childrens.
<ul id="menu">
<% Hub.roots.each do |category| %>
<li> <%= link_to h(category.title), category %>
<% unless category.children.empty? %>
<ul id="sub-menu">
<% category.children.each do |subcategory| %>
<li><%= link_to h(subcategory.title), subcategory %></li>
<% end %>
</ul>
<% end %>
</li>
<% end %>
</ul>
<% Hub.roots.each do |category| %>
<div> <%= link_to h(category.title), category %>
<% if category.children.present? %>
<div id="submenu">
<%= render 'hubs/sub', category: category %>
</div>
<% end %>
</div>
<% end %>
partial _sub
<ul>
<% category.children.each do |sub| %>
<li>
<%= link_to_unless_current sub.title, sub %>
<%= render 'hubs/sub', category: sub %>
</li>
<% end %>
</ul>
and css
#submenu > ul, ol {
margin-right: 20px;
li {
font-size: 11px;
margin: 4px 0;
margin-bottom:-5px !important;
}
ul, ol {
font-size: 11px;
margin: 0;
padding-left: 16px;
margin-bottom:-5px !important;
li {
margin: 4px 0;
}
}
}

rails seems to have a problem with text_area

Rails 3.0.3 does not seems to accept <%= f.text_area :message, :class => "share_ta" %> as a valid statement, and says ActionView::Template::Error (undefined method 'message' for []:Array):
Does anyone know why?
--Edit--
This is the form_for
<%= form_for :activity, :url => post_activity_path do |f| %>
<div class="share_tb">
<div class=share_t><span style="margin-left: 10px;">Tell us what's new <span style="color: #1fc2d1;"><%= #user.name %></span></span></div>
<%= f.text_area :message, :class => "share_ta" %>
</div>
<div id=sm_share class=sm_share_rc>
<ul>
<li style="color: #6b6b6b; font-size: 10pt; display: inline; list-style-type: none; float: right; margin-right: 10px; margin-top: 5px;"><%= f.check_box :everyone, "0", :class => "styled" %>Everyone</li>
<li style="color: #6b6b6b; font-size: 9pt; display: inline; list-style-type: none; height: 3px; float: left; margin-top: 5px;"><input type="checkbox" name="friends_only" value="3" class="styled">My Friends<br></li>
</ul>
<%= f.submit "Share", :class => "sm_share_b" %>
</div>
</div>
<% end %>
#Amit,
How have you specified form_for?
EDIT: 2nd time with more information from original poster.
It should be
<%= form_for :activity, :url => acti_path do |f| %>
<%= f.text_area :message, :class => "share_ta" %>
<% end %>

Resources