Rails mailboxer gem, send message to user using form - ruby-on-rails

I have installed the mailboxer gem to my rails app. It's successfully working when i'm using it in the console.
Exemple: current_user.send_message(User.last, "Body", "subject")
But i want to know how to do to make it work with a form view and a controller.
I want to be able to pass the send_message arguments through a view and send it to a message or a conversation controller.
I don't know the right way to handle this fantastic gem.
Thanks in advance
J.D.

The controller to send a message from a form:
class MessageController
# GET /message/new
def new
# display form
end
# POST /message/create
def create
recipient = User.find(params[:recipient_id])
current_user.send_message(recipient, params[:body], params[:subject])
end
end
Form view:
<%= form_tag({controller: "messages", action: "create"}, method: :post) do %>
<%= text_field_tag :subject %>
<%= text_area_tag :body %>
<%= submit_tag 'Send email' %>
<% end %>
A field for the recipient is missing in this example.

you need to have the basic knowledge in Actionmailer in rails take a look at the actionmailer
link:
guides.rubyonrails.org/action_mailer_basics.html

Related

How to set mail attachments without saving file in database?

I have created simple application form and i was trying to upload file and send it as email attachment. And i did it with this approach:
class ApplyController < ApplicationController
def prepare_email_content
ApplyMailer.with(params).apply.deliver_now
end
end
class ApplyMailer < ApplicationMailer
def apply
#company = Company.find(params[:company_id])
#candidate = params[:name]
#candidate_mail = params[:email]
#email = #company.email
attachments[params[:cv].original_filename] = params[:cv].read
mail to: #email, subject: 'Hello'
end
end
<h1>APPLY</h1>
<%= form_tag(apply_path, method: :post, multipart: true) do %>
<%= label_tag(:name, "First and last name:") %>
<%= text_field_tag(:name) %>
<%= label_tag(:email, "Email:") %>
<%= text_field_tag(:email) %>
<%= hidden_field_tag :company_id, params[:company_id] %>
<%= file_field_tag 'cv' %>
<%= submit_tag "Search", :name => nil %>
<% end %>
<%= link_to 'All offers', hello_path %>
Everything was working fine - i have tested application and it was fine. Then i have developed my application and when i come back to testing i started getting this error:
On the way i have installed some gems and updated few of them. I was checking out to commit where this feature was working and it is. But i'm not able to find any differences in my code. There were some changes in /.idea folder but i don't know if any of this files could trigger this issue.
I'm using rails 6.0.3 and ruby 2.5.8
EDIT
I can see that there is a problem inside called methods. Looks like it cannot find #sort_order value and it sets data value as nil. But i have no idea how to change working of this.
Replace the line you're attaching the file with
attachments["#{params[:cv].original_filename}"] = File.read(params[:cv].path)
Even though you're not saving the file, there is a still a local path to grab the file from.
Also, you should really consider passing the parameters to the mailer from the controller, rather than passing params in its entirety. That way if the information or format of the upload is incorrect, you can redirect back to the form and bypass sending the email.

Ruby on Rails form param is missing or the value is empty. But value is set

I'm trying to submit a form in ruby on rails that i made, but keep getting de next error.
Ruby on Rails form: param is missing or the value is empty
my form
<%= form_for #test do |f| %>
<div class="field">
<%= f.text_field :first_name %><br>
<%= f.text_field :last_name %><br>
</div>
<div class="actions">
<%= f.submit "Create" %>
</div>
<% end %>
my controller
def new
#test = Test.new
end
def create
#test = Test.new(allow_params)
if #test.save
redirect_to 'test/index'
else
render 'test/new'
end
end
private
def allow_params
params.require(:last_name).permit(:first_name)
end
my routes
resources :test
get 'test/index'
get 'test/new'
get 'test/create'
post '/tests' => 'test#create'
Your attributes are within the testlabel, so here you should go :
def allow_params
params.require(:test).permit(:first_name, :last_name)
end
Look, this is what you form posts when you click submit:
{"utf8"=>"✓","authenticity_token"=>"...", "test"=>"first_name"=>"poldo", "last_name"=>"de poldis"},"commit"=>"Create"}
As you can see first_name and last_name are inside an hash as value of a key called test. Indeed your function allow_params expects something like this
last_name: {first_name: 'poldo'}
as you can see the param (last_name) is missing, because is inside test!
The right way is as Ben answered:
params.require(:test).permit(:first_name, :last_name)
To understand better how strong parameters works I suggest to you to check this page Api doc or even better The latest version ofthe official manual

How to use devise-invitable

I just started with Rails and devise and I have a task, allow users to sign up only by invitation of existing user. I choose devise-invitable gem and got stuck with a bit unclear documentation. I have this code:
def invitationForm
#nuser = User.new
end
def invite_user
#user = User.invite!({:email => #nuser.email}, current_user)
end
Where invitationForm renders a form:
<%= form_for #nuser, url: {action: "invite_user"} do |f| %>
<%= f.text_field :email %>
<%= f.submit "Invite" %>
<% end %>
After all I`m getting this error:
RuntimeError in User#invite
Showing //invite.html.erb where line #2 raised:
Could not find a valid mapping for nil
What am I doing wrong and what should I do?
I think one of your problems could be here:
<%= form_for #nuser, url: {action: "invite_user"} do |f| %>
Your form was pointing to invite instead of the invite_user method you have created.
Okay, the deal was in the setup, somehow. After creating a new project and starting from scratch all workred fine.

POST is going to 'new' and not 'create' Rails

So I am trying to implement the password_reset functionality into my site using bcrypt. An issue I am having is the POST is going to my new action rather to my create action.
My View
<%= form_for password_resets_path, method: 'post' do %>
<div>
<h3>Please enter your email address</h3>
<%= text_field_tag :email, params[:email] %>
</div>
<div>
<%= submit_tag "Reset Password" %>
</div>
My Controller
class PasswordResetsController < ApplicationController
def new
end
def create
user = User.find_by(email: params[:email])
user.send_password_reset if user
redirect_to root_url, :notice => 'Email sent with password reset instructions.'
end
end
My Routes
resources :password_resets
And I am getting this error
ActionController::RoutingError (No route matches [POST] "/password_resets/new"):
I looked at different solutions already, and since I do not have a model the #object, would not work for me. Since I am simply just trying to call to an action.
I feel like I am missing something so very simple but for the life of me I have been unable to figure it out. Many thanks in advance to whomever is the one to help me.
Problem: <%= form_for password_resets_path, method: 'post' do %>
form_for needs an object. If you don't want an object, just use the form_tag helper:
<%= form_tag password_resets_path do %>
<%= text_field_tag :email, params[:email], placeholder: "Please enter your email address" %>
<%= submit_tag "Reset Password" %>
<% end %>
This should work for you.

Mailboxer with rails 4 (undefined local variable or method `root_url')

Update
The error was that rails cant find the root_url
Visit <%= link_to root_url, root_url %> and go to your inbox for more info.
for a quick fix and I dont need to sent the user to the root_url just a notification for the user to go to the app. I change the code to this: on the mailbox email views
Visit **messages** and go to your inbox for more info.
Question
I got devise set with my rails 4 app. Im following the example mailboxer-app when I sent the message I get a error:
`error undefined local variable or method `root_url' for #<#<Class:0x007ffe0b881678>:0x007ffe0b068298>`
Stuff I have fix to get it working
Got the form sending message to user with email
user can sent and reply
mark as delete
view inbox,sentbox and trash
this are my steps
install gem -v 0.12.1
rails g mailboxer:install
run migration
use the code from the example app(controller,view,routes)
add to my user.rb acts_as_messageable and
Conversations Controller
before_filter :authenticate_user!
helper_method :mailbox, :conversation
def index
#inbox ||= current_user.mailbox.inbox.paginate(:page => params[:inbox], :per_page => 5 )
#sentbox ||= current_user.mailbox.sentbox.paginate(:page => params[:sentbox], :per_page => 5 )
#trash ||= current_user.mailbox.trash.paginate(:page => params[:trash], :per_page => 5 )
end
def create
recipient_emails = conversation_params(:recipients).split(',')
recipients = User.where(email: recipient_emails).all
conversation = current_user.
send_message(recipients, *conversation_params(:body, :subject)).conversation
redirect_to :conversations
end
form
<%= bootstrap_form_for :conversation, url: :conversations do |f| %>
<%= f.text_field :recipients%>
<%= f.text_field :subject%>
<%= f.text_field :body %>
<div class="form-actions">
<%= f.primary "send" %>
<%= submit_tag 'Cancel', type: :reset, class: 'btn btn-danger' %>
</div>
<% end %>
View
<% #inbox.each do |conversation| %>
<%= conversation.originator.username%>
<%= link_to raw(truncate(strip_tags(conversation.subject), :length => 15)), conversation_path(conversation) %>
<% end %>
Ok got the fix to this problem.
what happen is that the mailboxer mailer was looking for root_url. Rails 4.1 wont generate the views for that just copy the files from the source code and works greate.
and just change that part of the code here.
view/mailboxer/all of this files
message_mailer
notification_mailer
change this
Visit <%= link_to root_url, root_url %> and go to your inbox for more info.
to this
Visit **messages** and go to your inbox for more info.
Thanx to this guy supremebeing7. on the mailboxer issue page

Resources