Mandrill API not sending - ruby-on-rails

I'm trying to set up the Mandrill API to send an email when a user clicks a button, but I cannot seem to get it to send. The email will send perfectly fine from the console, so I know it is not a problem with the template (Devise emails also send find).
I think it is to do with how I have set it up in the controller but I cannot find any help on where I should put it instead.
Here is the code from the controller:
def attending
#event = Event.find(params[:id])
type = params[:type]
if type == "attending" && #event.space != 0
current_user.attending << #event
#event.space = #event.space - 1
#event.save
AdminMailer.new_attending(#event.user, current_user)
redirect_to :back, notice: "You've joined the group. Your number will be sent to #{#event.user.name}"
else type == "unattending"
current_user.attending.delete(#event)
redirect_to :back, notice: "You've removed yourself from the group"
end
end
Here is the admin_mailer.rb
class AdminMailer < ActionMailer::Base
require 'mandrill'
def mandrill_client
#mandrill_client ||= Mandrill::API.new MANDRILL_API_KEY
end
def new_attending(creator, user)
template_name = "new-attending"
template_content = []
message = {
to: [{email: creator.email, name: creator.name}],
subject: "Someone wants to go riding with you!",
merge_vars: [
{rcpt: creator.email,
vars: [
{name: "CREATOR_NAME", content: creator.name},
{name: "USER_NAME", content: user.name},
{name: "USER_NUMBER", content: user.number}
]}
]
}
mandrill_client.messages.send_template template_name, template_content, message
end
end
And here is the link they click in the view.html.erb that should send the email:
<td><% if event.user != current_user && event.space != 0 && user_signed_in? %>
<% unless event.attendees.include?(current_user) %>
<%= link_to "Join", attending_event_path(event, type: "attending"), class: "btn btn-primary btn-xs", method: :put %>
<% end %>
<% end %></td>
Any help in figuring out why it's not sending would be great! As I said, it works in the console when I type:
AdminMailer.new_attending(#event, #user)

Please replace following code.
AdminMailer.new_attending(#event.user, current_user)
with
AdminMailer.new_attending(#event.user, current_user).deliver
I hope this will work.

Related

Trying to pass/fetch data for stripe api

I'm quite new to Ruby on Rails and I have been following this tutorial on how to implement an online marketplace by using Stripe's Connect API. This tutorial guides you on how to make single item purchases, I have tried to advance myself past this tutorial and create a marketplace where a user can purchase multiple items and put them in a basket and checkout, a nice challenge as the tutorial is focused on one item. However, I am stuck on the checkout _form.html.erb.
Before, to fetch the publishable_key,
<%= form_with model: current_user, local: true, url: subscription_url, method: :post, html: { id: "payment-form", class: "stripe-form" }, **data: { stripe_key: product.user.publishable_key }** do |form| %>
but now because I am submitting a cart full of products instead of one single item the data: { stripe_key: } is now dysfunctional. Instead, I thought it might be a good idea to do data: { stripe_key: #account.publishable_key } to fetch the publishable_key from the database but it is not working.
I am most likely forgetting something important. Any help would be sound!
http://localhost:3000/subscription/new?account=4&amount=17
The Error
NoMethodError in Subscriptions#new
Showing /Users/kaneandrewgibson/Desktop/Charlie/WOP/app/views/subscriptions/_form.html.erb where line #1 raised:
undefined method `publishable_key' for nil:NilClass
Subscriptions/_form.html.erb
<%= form_with model: current_user, local: true, url: subscription_url, method: :post, html: { id: "payment-form", class: "stripe-form" }, data: { stripe_key: #account.publishable_key } do |form| %>
<div>
<label for="card-element" class="label">
Credit or debit card
</label>
<div id="card-element">
<!-- A Stripe Element will be inserted here. -->
</div>
<!-- Used to display Element errors. -->
<div id="card-errors" role="alert" class="text-sm text-red-400"></div>
</div>
<input type="hidden" name="account" value="<%= params[:account] %>">
<button>Back <%= number_to_currency(params[:amount]) %> /mo toward <em><%= %></em></button>
<% end %>
SubscriptionsController
class SubscriptionsController < ApplicationController
before_action :authenticate_user!
def new
#account = User.find_by_id(params[:id])
end
# Reference:
# https://stripe.com/docs/connect/subscriptions
def create
#account = User.find_by_id(params[:id])
key = #account.user.access_code
Stripe.api_key = key
plan_id = params[:plan]
plan = Stripe::Plan.retrieve(plan_id)
token = params[:stripeToken]
customer = if current_user.stripe_id?
Stripe::Customer.retrieve(current_user.stripe_id)
else
Stripe::Customer.create(email: current_user.email, source: token)
end
Stripe::PaymentIntent.create({
customer: customer,
amount: #cart.total_price * 100,
confirm: true,
currency: 'gbp',
payment_method_types: ['card'],
application_fee_percent: 3,
}, {
stripe_account: 'key',
})
options = {
stripe_id: customer.id,
subscribed: true,
}
options.merge!(
card_last4: params[:user][:card_last4],
card_exp_month: params[:user][:card_exp_month],
card_exp_year: params[:user][:card_exp_year],
card_type: params[:user][:card_brand]
)
current_user.perk_subscriptions << plan_id
current_user.update(options)
# Update project attributes
project_updates = {
backings_count: #project.backings_count.next,
current_donation_amount: #project.current_donation_amount + (plan.amount/100).to_i,
}
#project.update(project_updates)
redirect_to root_path, notice: "Your subscription was setup successfully!"
end
def destroy
subscription_to_remove = params[:id]
plan_to_remove = params[:plan_id]
customer = Stripe::Customer.retrieve(current_user.stripe_id)
customer.subscriptions.retrieve(subscription_to_remove).delete
current_user.subscribed = false
current_user.perk_subscriptions.delete(plan_to_remove)
current_user.save
redirect_to root_path, notice: "Your subscription has been cancelled."
end
end
Kane.
It looks like your account ID isn't getting into your controller. I would expect the URL to look something like this (account_id instead of account):
http://localhost:3000/subscription/new?account_id=4&amount=17
And I would expect the SubscriptionsController#new to be looking in the Account model rather than the User model, and to be using the same account_id param:
#account = Account.find_by_id(params[:account_id])

How to fix 'Rails.ajax' error in site Console?

I have 2 different post types (news and articles). When i go to news and try to comment any news, nothing happen. In google chrome console it says:
POST /news/artifact-v-spiske-liderov-prodazh-steam-po-itogam-2018-goda/comments/ 404
rails-ujs.self-d109d8c5c0194c8ad60b8838b2661c5596b5c955987f7cd4045eb2fb90ca5343.js?body=1:7
Rails.ajax # rails-ujs.self-d109d8c5c0194c8ad60b8838b2661c5596b5c955987f7cd4045eb2fb90ca5343.js?body=1:7
Rails.handleRemote # rails-ujs.self-d109d8c5c0194c8ad60b8838b2661c5596b5c955987f7cd4045eb2fb90ca5343.js?body=1:31
(anonymous) # rails-ujs.self-d109d8c5c0194c8ad60b8838b2661c5596b5c955987f7cd4045eb2fb90ca5343.js?body=1:5
BUT! When i try comment in Articles type, all good, comments work. What is problem?
My _form.html.haml:
- if user_signed_in? && current_user.mute_to && current_user.mute_to > Time.current
%div
%strong
= "Вы не можете писать комментарии до #{current_user.mute_to.strftime("%F %R")}"
- elsif user_signed_in?
= form_with model: [commentable, Comment.new], html: { class:
local_assigns[:class], data: { target: local_assigns[:target] } } do |form|
.form-group
= form.text_area :body, placeholder: "Напишите комментарий (минимум 3 символа)", class: "form-control"
.form-group
= form.hidden_field :parent_id, value: local_assigns[:parent_id]
= form.submit 'Отправить', class: "btn"
My _comment.html.haml:
- nesting = local_assigns.fetch(:nesting, 1)
- max_nesting = local_assigns[:max_nesting]
- continue_thread = local_assigns[:continue_thread]
= tag.div id: dom_id(comment), class: "border-left pl-4 my-4" do
- if comment.deleted?
%strong [комментарий удален]
%small
= link_to time_ago_in_words(comment.created_at), url_for(comment: comment.id, anchor: dom_id(comment))
%p [комментарий удален]
- else
- comments_user = comment.user
%strong.comments_user_line
= fa_icon "user-circle", class: "mr-1"
= link_to comments_user.nickname, user_path(comments_user), class: 'user_link'
%small
= link_to time_ago_in_words(comment.created_at), polymorphic_path(comment.commentable, comment: comment.id, anchor: dom_id(comment))
= simple_format comment.body
%div.comments_block
- if user_signed_in? && current_user.mute_to && current_user.mute_to > Time.current
- else
%small
- if user_signed_in?
%btn.reply Отвеить
- else
= link_to "Отвеить", new_user_session_path
= link_to "Удалить", [comment.commentable, comment], method: :delete, data: {confirm: "Вы уверены?"} if comment.user == current_user
= render partial: "comments/form", locals: { |
commentable: comment.commentable, |
parent_id: reply_to_comment_id(comment, nesting, max_nesting), |
class: "mt-4 d-none replies_form", |
target: "reply.form" |
} |
= tag.div id: "#{dom_id(comment)}_comments" do
- if continue_thread.present? && nesting >= continue_thread && comment.comments.any?
= link_to "Открыть ветку", url_for(comment: comment.id, anchor: dom_id(comment))
- else
= render comment.comments, continue_thread: continue_thread, nesting: nesting + 1, max_nesting: local_assigns[:max_nesting]
My comments_controller.rb:
class CommentsController < ApplicationController
before_action :authenticate_user!
def create
#comment = #commentable.comments.new(comment_params)
#comment.user = current_user
if #comment.save
respond_to do |format|
format.html { redirect_to #commentable }
format.js
end
else
redirect_to #commentable, alert: "Ошибка :("
end
end
def destroy
#comment = #commentable.comments.find(params[:id])
return false unless current_user.id == #comment.user_id
#comment.destroy
redirect_to #commentable
end
private
def comment_params
params.require(:comment).permit(:body, :parent_id)
end
end
If i uncomment rails-ujs in application.js like this:
require rails-ujs
Then when i posting comment it will be say:
ActiveRecord::RecordNotFound in News::CommentsController#create
Couldn't find News with 'id'=artifact-v-spiske-liderov-prodazh-steam-po-itogam-2018-goda
Extracted source (around line #7):
6 def set_commentable
7 #commentable = News.find(params[:news_id])
8 end
9 end
How i can fix it? Thanks!
The first suggestion - comment require rails-ujs until you solve the issue. It will be easier to debug.
Then, the issue is that as a params[:news_id] you are passing news slug (artifact-v-spiske-liderov-prodazh-steam-po-itogam-2018-goda) instead of news id. One solution is to modify the code to pass news id as params[:news_id].
Another, a little big uglier solution, is to refactor set_commentable method in the News::CommentsController to something like this:
def set_commentable
#commentable = News.find_by(slug: params[:news_id])
end
However, it might break other places where set_commentable is used.

ActiveRecord::StatementInvalid in Show Controller

I've got a simple search form, in rails 4 app, that needs two params passed to be able to show relevant data.
I'm getting an 'Mysql2::Error: Unknown column 'data inputted' but the columns do exist. If I instead of '#search = Page.where(params[:one] && params[:two])' use '#search = Page.all' the data shows, but all of it shows.
Form
<%= form_tag(page_show_path, id: "search-form") do %>
<%= text_field_tag :one, params[:one], placeholder: "One" %>
<%= text_field_tag :two, params[:two], placeholder: "Two" %>
<%= submit_tag "Search", :name => nil %>
<% end %>
Model
def self.one(query)
where("one = ?", "%#{query}%")
end
def self.two(query)
where("two = ?", "%#{query}%")
end
Controller
def show
if (params[:one] && params[:two]).present?
#search = Page.where(params[:one] && params[:two])
else
redirect_to page_path, notice: "Not a valid combination"
end
end
You can Create and Use Scope.
scope :find_one_two, ->(query_one, query_two) { where("one = ? AND two = ? ", query_one, query_two) }
#search = Page.find_one_two(params[:one], params[:two])
OR
You can use.
#search = Page.where("one = ? AND two = ?", params[:one], params[:two])
def show
if (params[:one] && params[:two]).present?
#search = Page.where("one like ? AND two like ? ", "%#{params[:one]}%", "%#{params[:two]}%")
else
redirect_to page_path, notice: "Not a valid combination"
end
end
This may solve your problem.

Blacklist model in Rails 4

I am using Act_as_votable to implement like/dislike voting system. It works just perfect.
But now I am facing problem to blacklist exact that item that has received at least 30 downvotes.
I have model Advertisement. That has column in_blacklist with default value false.
I added in_blacklist field as permitted in Advertisement controller.
I have tried this so far.
In view:
<%= link_to "Like", like_advertisement_path(#advertisement), method: :put %> <%= #advertisement.get_likes.size %>)
(<%= link_to "Dislike", dislike_advertisement_path(#advertisement), method: :put %> <%= #advertisement.get_dislikes.size %>)
In controller:
def downvote
#advertisement.downvote_from current_user
flash[:notice] = 'Downvote added.' if #advertisement.vote_registered?
if #advertisement.get_dislikes.size == 30
#advertisement.in_blacklist = true
flash[:notice] = "#{#advertisement.name } added to blacklist. Information sent to #{#advertisement.user.email } "
respond_with(#advertisement)
else
respond_with(#advertisement)
end
end
So when I hit 30th dislike, there is no error messages. And when I check if the boolean has changed, but no it is still false.
You forgot to save the object.
def downvote
#advertisement.downvote_from current_user
flash[:notice] = 'Downvote added.' if #advertisement.vote_registered?
if #advertisement.get_dislikes.size == 30
#advertisement.in_blacklist = true
#advertisement.save # there you go!
flash[:notice] = "#{#advertisement.name } added to blacklist. Information sent to #{#advertisement.user.email } "
respond_with(#advertisement)
else
respond_with(#advertisement)
end
end

Second Email is not sending in Devise Invitable for Rails 3

Hi I have a batch invitation module where a user can invite multiple users to register
And I am using devise_invitable gem
The problem is that, the first inputted email is being sent but for the second one, I am not receiving any email(s).
Here's my form:
.user-unit
%h2 Invite
= form_tag batch_invite_users_path, :method => :post do
= label_tag "Emails"
= text_field_tag :user_email_1
= text_field_tag :user_email_2
%br
= submit_tag "Send", class: 'btn-primary'
And my controller:
def batch_invite
if request.post?
valid_emails = []
#invalid_emails = []
#invited_emails = []
#invited_emails << params[:user_email_1] << params[:user_email_2]
#invited_emails.each do |email|
if email =~ Devise.email_regexp
user = User.invite!({:email => email}, current_user)
user.roles << Role.find(3)
valid_emails << email
else
#invalid_emails << email
end
end
if valid_emails.empty?
flash[:notice] = "No email have been sent."
else
redirect_to :back
flash[:notice] = "An invitation has been sent to #{valid_emails.join(',')}."
end
end
end
I have to fields, user_email_1 and user_email_2 and I converted those into an array so that I could loop it for the !invite method provided by the gem
user_email_1 is receiving an email but not the user_email_2
Here's my log/trace: http://pastebin.com/1kqvZXWC
Any workarounds will be appreciated.

Resources