I have created a database of users in my Ruby on Rails app, and now I'm trying to create a mailer that send emails to all users in my database whenever I want.
Here's my model:
class MailMessage < ActionMailer::Base
def contact(recipient, subject, message)
# host = Hobo::Controller.request_host
# app_name = Hobo::Controller.app_name || host
#subject = subject
# #body = { :user => user, :host => host, :app_name => app_name }
#body["title"] = 'This is title'
#body["email"] = 'mark#doc.org.uk'
#body["message"] = message
#recipients = recipient
#from = 'no-reply#doc.org.uk'
#sent_on = Time.now
#headers = {}
end
end
Here's my controller:
class MailMessageController < ApplicationController
def sendmail
email = #params["email"]
recipient = email["recipient"]
subject = email["subject"]
message = email["message"]
MailMessage.deliver_contact(recipient, subject, message)
return if request.xhr?
render :text => 'Message sent successfully'
end
def index
render :file => 'app/views/mail_message/index.html'
end
end
Here's my views/mail_message:
<h1>Send Email</h1>
<%= form_tag :action => 'sendmail' %>
<p>
<label for="email_subject">Subject</label>
<%= text_field 'email', 'subject' %>
</p>
<p>
<label for="email_recipient">Recipient</label>
<%= text_field 'email', 'recipient' %>
</p>
<p>
<label for="email_message">Message</label>
<%= text_area 'email', 'message' %>
</p>
<%= submit_tag "Send" %>
<%= form_tag %>
Here's my enviroment.rb:
ActionMailer::Base.delivery_method = :sendmail
ActionMailer::Base.sendmail_settings = {
:location => '/usr/sbin/sendmail',
:arguments => '-i -t'
}
ActionMailer::Base.perform_deliveries = true # the "deliver_*" methods are available
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.default_charset = "utf-8"
ActionMailer::Base.default_content_type = "text/html" # default: "text/plain"
ActionMailer::Base.default_mime_version = "1.0"
ActionMailer::Base.default_implicit_parts_order = [ "text/html", "text/plain"]
When I run a test message, I get the following error:
You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]
app/controllers/mail_message_controller.rb:4:in `sendmail'
It doesn't seem to recognise sendmail, but I have given its location. Any clues for how to fix this error will be very appreciated.
It looks like this line is the problem:
#params["email"]
If it's meant to be the data from the form, drop the #.
#params isint initialized in your controller.
You probably simple want to use params to get your http action parameters.
Related
When I write city in field and click button, on page has to be that name of city and another. In my case every time name is New York. When I write for example Las Vegas my link is http://localhost:3000/?query=Las+Vegas&commit=Search, the data on the page is about New York.
class HomeController < ApplicationController
def index
require 'net/http'
require 'json'
#params = {
:access_key => "1dbc1a3aa6b2e76a0c8eda1cba0c9c8b",
:query => "New York"
}
#uri = URI("http://api.weatherstack.com/current?access_key=1dbc1a3aa6b2e76a0c8eda1cba0c9c8b&query=#{#params[:query]}")
#uri.query = URI.encode_www_form(#params)
#json = Net::HTTP.get(#uri)
#api_response = JSON.parse(#json)
if #params[:query].empty?
#city = "Enter city"
#temperature = "(in English)"
else
#city = " #{#api_response['location']['name']}"
#temperature = "#{#api_response['current']['temperature']}°"
end
end
end
routes.rb
Rails.application.routes.draw do
root 'home#index', :as => 'search'
end
index.html.erb
<div class="main">
<h1 class="weather">Weather</h1>
<%= form_with(url: search_path, method: "get", local: true) do %>
<%= text_field_tag(:query) %>
<%= submit_tag("Search",class: "btn btn-primary") %>
<% end %>
<div class="city"> <h1><%= #city %> <h5><%= #temperature %></h5></h1> </div>
</div>
It looks like you have hardcoded New York in your params.
#params = {
:access_key => "1dbc1a3aa6b2e76a0c8eda1cba0c9c8b",
:query => "New York"
}
So you need to change this to be something like:
#params = {
:access_key => "1dbc1a3aa6b2e76a0c8eda1cba0c9c8b",
:query => params[:query]
}
I am using gem mail_form to handle contact in a Rails app, and I have 6 different contact forms.
I put a hidden_field_tag in the form and pass the desired subject as a variable. In the html, the value is there but the email arrives with (no subject). What can I be doing wrong?
In controllers
def where_to_buy
#contact = Contact.new
#the_subject = "Where to buy"
end
In contact form
= form_for #contact do |f|
= render "form", f: f
= f.text_area :message
.hide
= f.text_field :nickname, hint: 'Leave this field empty!'
= hidden_field_tag :mail_subject, #the_subject
= f.submit "Send Message"
In model
class Contact < MailForm::Base
attribute :mail_subject
attribute :first_name, validate: true
attribute :last_name, validate: true
attribute :message, validate: true
attribute :nickname, captcha: true
def headers
{
subject: %(#{mail_subject}),
to: "jorge#email123.com",
from: %("#{first_name} #{last_name}" <#{email}>)
}
end
end
Output html in chrome:
<input type="hidden" name="mail_subject" id="mail_subject" value="Where to buy">
Instead of:
= hidden_field_tag :mail_subject, #the_subject
You will want to use:
= f.hidden_field :mail_subject, value: #the_subject
If you inspect the parameters that are logged into your development.log you will see why.
When you use hidden_field_tag the mail_subject is defined as its own independent parameter and will not be included in the contact hash. You'll have something like:
params = { "contact" => { "message" => "text here", ... }, "mail_subject => "Where to buy" }
But when you use f.hidden_field the mail_subject will be included in the contact hash. You'll have something like:
params = { "contact" => { "message" => "text here", "mail_subject => "Where to buy", ... } }
And then when you call Contact.new(params[:contact]) the new contact object will get the mail_subject value.
I plan to use snippets in Comfortable Mexican Sofa to store email templates. But is there a way to add a field into a snippet. I would use it to store the subject of the email. Would be good to have also the subject in the cms so that our editors can change it if they feel like it.
By default snippets seem to have only two fields 'Label' and 'Identifier'. And of course the 'Content'. I would like to add also a 'Subject' String into the fields.
Simple Three Step Process:
1:
rails g migration AddSubjectToComfyCmsSnippets subject:string
2:
rake db:migrate
3:
create app/views/comfy/admin/cms/snippets/_form.html.haml with the following content:
- content_for :right_column do
= render 'comfy/admin/cms/files/index'
= render :partial => 'comfy/admin/cms/partials/snippet_form_before', :object => form
= form.text_field :label, :data => {:slugify => #snippet.new_record?}
= form.text_field :identifier, :data => {:slug => true}
= form.text_field :subject
= form.text_area :content, :data => {'cms-rich-text' => true}
= render :partial => 'comfy/admin/cms/categories/form', :object => form
= render :partial => 'comfy/admin/cms/partials/snippet_form_after', :object => form
= form.form_group :class => 'form-actions' do
= form.submit t(#snippet.new_record?? '.create' : '.update'), :class => 'btn btn-primary'
= link_to t('.cancel'), comfy_admin_cms_site_snippets_path, :class => 'btn btn-link'
Now you can reference the subject in your app like so:
Subject: #{#snippet.subject}
Monkey patch to get fixtures working:
create config/initializers/cms_monkey_patch.rb with the following contents:
ComfortableMexicanSofa::Fixture::Snippet::Importer.class_eval do
def import!
Dir["#{self.path}*/"].each do |path|
identifier = path.split('/').last
snippet = self.site.snippets.find_or_initialize_by(:identifier => identifier)
# setting attributes
categories = []
if File.exists?(attrs_path = File.join(path, 'attributes.yml'))
if fresh_fixture?(snippet, attrs_path)
attrs = get_attributes(attrs_path)
snippet.label = attrs['label']
snippet.subject = attrs['subject']
categories = attrs['categories']
end
end
# setting content
%w(html haml).each do |extension|
if File.exists?(content_path = File.join(path, "content.#{extension}"))
if fresh_fixture?(snippet, content_path)
snippet.content = extension == "html" ?
::File.open(content_path).read :
Haml::Engine.new(::File.open(content_path).read).render.rstrip
end
end
end
# saving
if snippet.changed? || self.force_import
if snippet.save
save_categorizations!(snippet, categories)
ComfortableMexicanSofa.logger.info("[FIXTURES] Imported Snippet \t #{snippet.identifier}")
else
ComfortableMexicanSofa.logger.warn("[FIXTURES] Failed to import Snippet \n#{snippet.errors.inspect}")
end
end
self.fixture_ids << snippet.id
end
# cleaning up
self.site.snippets.where('id NOT IN (?)', fixture_ids).each{ |s| s.destroy }
end
end
end
ComfortableMexicanSofa::Fixture::Snippet::Exporter.class_eval do
def export!
prepare_folder!(self.path)
self.site.snippets.each do |snippet|
snippet_path = File.join(self.path, snippet.identifier)
FileUtils.mkdir_p(snippet_path)
# writing attributes
open(File.join(snippet_path, 'attributes.yml'), 'w') do |f|
f.write({
'label' => snippet.label,
'subject' => snippet.subject,
'categories' => snippet.categories.map{|c| c.label}
}.to_yaml)
end
# writing content
open(File.join(snippet_path, 'content.html'), 'w') do |f|
f.write(snippet.content)
end
ComfortableMexicanSofa.logger.info("[FIXTURES] Exported Snippet \t #{snippet.identifier}")
end
end
end
I've build quite complex form which creates one prescription with many realtions. I am using this syntax in view:
- provide(:title, 'Create prescription')
%h1 Add medicines to prescription
.row
.span6.offset3
= form_for #prescription do |f|
= render 'shared/error_prescription_messages'
%p
= f.hidden_field :patient_id, :value => params[:patient_id]
= f.hidden_field :user_id, :value => current_user.id
= f.fields_for :relations do |builder|
= render 'child_form', :f => builder
%p= f.submit "Submit"
chlid_form is quite simple :
- it=f.options[:child_index].to_i
- n= it.to_s
%h2
= "Medicine ##{it+1}"
= f.hidden_field :medicine_id, :id => "my_medicine_id#{it}"
- if params[:prescription].nil? || params[:prescription][:relations_attributes][n.to_sym][:medicine_name].nil?
= f.autocomplete_field :medicine_name, autocomplete_medicine_name_relations_path, :id_element => "#my_medicine_id#{it}"
- else
= f.autocomplete_field :medicine_name, autocomplete_medicine_name_relations_path, :id_element => "#my_medicine_id#{it}", :value => params[:prescription][:relations_attributes][n.to_sym][:medicine_name]
= f.label :amount, "Amount of medicine boxes"
= f.number_field :amount, :value => 1
= f.label :daily
= f.number_field :daily, :value => 1
= f.label :period_in_days, "Duration of treatment (in days)"
= f.number_field :period_in_days, :value => 1
So as you can see I'm using f.options[:child_index] to get index of child (0,1,2...) cause I generate multiple items with this particular form. I then put it to variable it and sucessfully use it in :id_element => "#my_medicine_id#{it}" which works PERFECTLY fine (creates my_medicine_id0, my_medicine_id1 ....) Although it doesn't work in this line:
:value => params[:prescription][:relations_attributes][n.to_sym][:medicine_name]
where n is just n=it.to_s.
I though somethings wrong in controller but if I change this line to whatever
:value => params[:prescription][:relations_attributes]**[:'0']**[:medicine_name] or any other integer from 0 to 4 everything works great, but I NEED dynamic change in this one. So I got proof that it DOES work because it generates integer fine here "#my_medicine_id#{it}" but won't work in hash! And when I print the whole hash from params I get this:
{"patient_id"=>"7", "user_id"=>"1", "relations_attributes"=>{"0"=>{"medicine_id"=>"13490", "medicine_name"=>"Locacid 500 mcg/g (0,05%) (1 tuba 30 g)", "amount"=>"0", "daily"=>"1", "period_in_days"=>"1"}, "1"=>{"medicine_id"=>"", "medicine_name"=>"", "amount"=>"1", "daily"=>"1", "period_in_days"=>"1"}, "2"=>{"medicine_id"=>"", "medicine_name"=>"", "amount"=>"1", "daily"=>"1", "period_in_days"=>"1"}, "3"=>{"medicine_id"=>"", "medicine_name"=>"", "amount"=>"1", "daily"=>"1", "period_in_days"=>"1"}, "4"=>{"medicine_id"=>"", "medicine_name"=>"", "amount"=>"1", "daily"=>"1", "period_in_days"=>"1"}}}
so to get the values I need it's pretty obvious that
params[:prescription][:relations_attributes][SOME_KIND_OF_INETEGER][:medicine_name] should work, but doesn't.
Controller code:
class PrescriptionsController < ApplicationController
before_action :signed_in_user
before_action :doctor_user, only: [:new, :create]
before_action :pharmacist_user, only: [:update]
def new
#prescription =Prescription.new
5.times { #prescription.relations.build }
end
def create
#prescription = Prescription.new(new_prescription_params)
if #prescription.save
flash[:success] = "Prescription created."
redirect_to #prescription
else
5.times { #prescription.relations.build }
render 'new', :prescription => params[:prescription]
end
end
def show
#prescription = Prescription.find(params[:id])
#medicines = #prescription.medicines.paginate(page: params[:page], :per_page => 10)
end
def update
#prescription = Prescription.find(params[:id])
#patient = Patient.find(params[:patient_id])
if !prescription_expired?(#prescription)
#prescription.realized = 1
if #prescription.save
flash[:success] = "Prescription realized."
redirect_to #patient
else
redirect_to root_url
end
else
flash[:notice] = "Can't realize, prescription expired."
redirect_to #patient
end
end
private
def new_prescription_params
params.require(:prescription).
permit(:patient_id, :user_id, relations_attributes: [:medicine_id, :medicine_name, :amount, :daily, :period_in_days])
end
def doctor_user
redirect_to(root_url) unless current_user.function == "doctor"
end
def pharmacist_user
redirect_to(root_url) unless current_user.function == "pharmacist"
end
def prescription_expired?(presc)
presc.created_at < 1.month.ago
end
def signed_in_user
unless signed_in?
store_location
flash[:notice] = "Please log in."
redirect_to login_url
end
end
end
I run out of ideas so I ask you guys if anyone can help. Thanks.
There is no point in using params in your view since you already assigned those to your models. Also when you rendering your new action, those params doesn't exist as nothing has been send to the server yet. Just get rid of all the values from inputs.
Your partial should look like:
- it=f.options[:child_index].to_i
- n= it.to_s
%h2
= "Medicine ##{it+1}"
= f.hidden_field :medicine_id, :id => "my_medicine_id#{it}"
= f.autocomplete_field :medicine_name, autocomplete_medicine_name_relations_path
= f.label :amount, "Amount of medicine boxes"
= f.number_field :amount
= f.label :daily
= f.number_field :daily
= f.label :period_in_days, "Duration of treatment (in days)"
= f.number_field :period_in_days
If you want your fields to have default value, set default value inside your database.
I have this form:
= form_for([:mobile,#disclosure], :html => {:id => "disclosure-form", :remote => true}) do |f|
%p
=f.label :display_as, (t :select_disclosure_type)
=f.select :display_as, options_from_collection_for_select(Disclosure.basics, :display_as,
:name, f.object.display_as)
%p
=f.label :notes, (t :notes)
=f.text_area :notes, :class => "#{validation_rules(:free_disclosure_notes)}", :rows => 5 , :id => "disclosure_notes_entry"
= buttonset({:submit_label => (t "buttons.influencers.disclosures.create"),:submit_css_class => "call-to-action",
:cancel_url => "#",:direction => :left_to_right})
and I have this controller so far:
def create
Rails.logger.debug "-"*100
Rails.logger.debug session.inspect
Rails.logger.debug "-"*100
provider = nil
#disclosure = params[:disclosure-form].blank?
resource = build_resource({})
begin
if disclosure.nil?
print "hello 1"
else
print "hello 2"
end
rescue Exception => ex
Rails.logger.debug ex
end
respond_to do |format|
format.html{}
end
end
But I am not sure how to get the #disclosures object to be populated so that I can insert it into the database?
How do I get the right data out of the form and add it to the database?
Thanks!!
Your form data should be in params[:disclosure] hash. So, in your create action you would do something like:
def create
#disclosure = Disclosure.new(params[:disclosure])
if #disclosure.save
# redirect to show or something else
else
# render 'new' for with validation errors
end
end
This should save your object in DB. If there is no validation errors.
I'm not sure what this line does: #disclosure = params[:disclosure-form].blank?