Contact Form Not Working on Mobile Browser Rails 7 - ruby-on-rails

I am working on my first ruby on rails by following a tutorial, now deployed to production, I noticed the contact is working fine on PC browser message do get sent and delivered to mail but on mobile browser send button only reload the page with input again.
this is the html code
<%= form_for(#contact, :html => { :class => "form-stacked"}, :remote => true) do |f| %>
<form class="contact__form">
<h3 class="text-md ">Contact Us</h3>
<p class="mb-5">Reach out to the world’s most reliable services.</p>
<div class="input-group">
<%= f.text_field :name, required: true, class: 'input', placeholder: "Your name"%></br>
</div>
<div class="input-group">
<%= f.text_field :Email, required: true, class: 'input', placeholder: "Email Address"%></br>
</div>
<div class="input-group-2 mb-4">
<%= f.text_area :message, required: true, class: 'input', rows: "4", placeholder: "Your Message"%></br>
</div>
<%= f.submit 'Send Message', class: 'btn btn-main' %>
<% end %>
this is the controller
class ContactsController < ApplicationController
skip_before_action :verify_authenticity_token
def new
#contact = Contact.new
end
def create
#contact = Contact.new(params[:contact])
#contact.request = request
if #contact.deliver
flash.now[:success] = 'Message sent!'
else
flash.now[:error] = 'Could not send message'
render :new
end
end
end
log example of message sent from pc:
[55fef2dc-48af-448a-9af1-5d63774682c6] Started POST "/contacts" for 172.71.102.128 at 2023-01-17 12:46:11 +0000
[55fef2dc-48af-448a-9af1-5d63774682c6] Processing by ContactsController#create as HTML
[55fef2dc-48af-448a-9af1-5d63774682c6] Parameters: {"contact"=>{"name"=>"siri", "Email"=>"user#gmail.com", "message"=>"piytan"}, "commit"=>"Send Message"}
[55fef2dc-48af-448a-9af1-5d63774682c6] Rendered /layers/heroku_ruby/gems/vendor/bundle/ruby/3.1.0/gems/mail_form-1.9.0/lib/mail_form/views/mail_form/contact.erb (Duration: 1.7ms | Allocations: 834)[2023-01-17T12:46:11.726778 #1] INFO -- : [55fef2dc-48af-448a-9af1-5d63774682c6] Delivered mail 63c6989313cac_1a3482043b#asi4her-588fcb97b9-c8d2m.mail (646.5ms)
[55fef2dc-48af-448a-9af1-5d63774682c6] Rendered contacts/create.html.erb within layouts/application (Duration: 0.9ms | Allocations: 179)
[55fef2dc-48af-448a-9af1-5d63774682c6] Rendered layout layouts/application.html.erb (Duration: 3.2ms | Allocations: 1186)
[55fef2dc-48af-448a-9af1-5d63774682c6] Completed 200 OK in 671ms (Views: 3.7ms | ActiveRecord: 0.0ms | Allocations: 7819)
Example of one sent from mobile:
[f272778b-c913-49f1-b407-e1c8b4288798] Started POST "/contacts" for 172.71.182.191 at 2023-01-17 12:45:53 +0000
[[f272778b-c913-49f1-b407-e1c8b4288798] Processing by ContactsController#create as HTML
[f272778b-c913-49f1-b407-e1c8b4288798] Parameters: {"contact"=>{"name"=>"siri", "Email"=>"User#gmail.com ", "message"=>"Hii", "commit"=>"Send Message"}
[f272778b-c913-49f1-b407-e1c8b4288798] Rendered contacts/new.html.erb within layouts/application (Duration: 1.3ms | Allocations: 929)
[f272778b-c913-49f1-b407-e1c8b4288798] Rendered layout layouts/application.html.erb (Duration: 3.6ms | Allocations: 1899)
[f272778b-c913-49f1-b407-e1c8b4288798] Completed 200 OK in 5ms (Views: 4.3ms | ActiveRecord: 0.0ms | Allocations: 2148)

Related

Rails Hotwire + Admin Namespace

I have an app running rails 6.1.3.2. I'm starting to migrate to use hotwire instead of ujs. My app uses an admin namespace to allow a user to make edits, create items via this. Example route below -
resources :event_attachments
namespace :admin do
resources :event_attachments
end
When making an update via the admin view to an event_attachment, the turbo_stream is not processing the partial in the admin view, instead it's looking in EventAttachmentsController. I'm not sure why it's doing this - any suggestions would be appreciated.
Admin:EventAttachmentsController -
def update
if #event_attachment.update(event_attachment_params)
respond_to do |format|
format.turbo_stream do
render turbo_stream: [
turbo_stream.update("flash", partial: "shared/flash", locals: { notice: "Event image updated" }),
turbo_stream.replace(:event_attachments, partial: 'admin/pages/event_attachment', locals: { event_attachment: #event_attachment })
]
end
format.html do
redirect_to edit_admin_event_attachment_path(#event_attachment), notice: 'Event image updated'
end
end
else
render :edit, status: :unprocessable_entity
end
end
Here's a sample of my form in admin/event_attachments -
<%= form_for([:admin, event_attachment], :id => dom_id(event_attachment)) do |f| %>
<%= f.text_field :title, :placeholder => "Event image name *" %>
<% end %>
Rails Server Log (as a test, I created a partial in the main event_attachments view) -
Started PATCH "/event_attachments/25" for ::1 at 2022-01-03 22:18:53 -0500
Processing by EventAttachmentsController#update as TURBO_STREAM
Parameters: {"utf8"=>"✓", "authenticity_token"=>"[FILTERED]", "event_attachment"=>{"title"=>"Test Name", "summary"=>"caption"}, "commit"=>"Save Changes", "id"=>"25"}
EventAttachment Load (1.1ms) SELECT `event_attachments`.* FROM `event_attachments` WHERE `event_attachments`.`id` = 25 LIMIT 1
↳ app/controllers/event_attachments_controller.rb:51:in `set_event_attachment'
Rendered event_attachments/_event_attachment.html.erb (Duration: 0.1ms | Allocations: 22)
[ActionCable] Broadcasting to event_attachments: "<turbo-stream action=\"update\" target=\"event_attachment_25\"><template><li id=\"event_attachment_25\">\n hello\n</li>\n</template></turbo-stream>"
Redirected to http://localhost:3000/event_attachments/25
Completed 302 Found in 4ms (ActiveRecord: 1.1ms | Allocations: 1640)
It looks like the problem here is in the url that your form is sending the request to. If you look in the logs you will see it says processing by EventAttachmentsController#update this is because in your form the URL is wrong. My thoughts is you should change from using form_for and the [] to use form_with
<%= form_with(url: admin_event_attachments_path, :id => dom_id(event_attachment)) do |f| %>
<%= f.text_field :title, :placeholder => "Event image name *" %>
<% end %>

Rails - Not Redirecting even though it says “Redirected to http://localhost:3000” in my sever

I have a form_with that works in my server but not in my brother:
My form in my view :
<%= form_with url: sessions_path, method: "post" do |f|%>
<h3 class="text-center text-info">Se Connecter</h3>
<div class="form-group">
<%= label_tag "email", "Adresse mail:", class: "text-info" %><br>
<%= f.email_field "email", class: "form-control" %>
</div>
<div class="form-group">
<%= label_tag "password", "Mot De Passe", class: "text-info" %><br>
<%= f.password_field "password", class: "form-control" %>
</div>
<div class="form-group" style="padding: 20px;">
<%= f.submit "Se connecter", class: "btn btn-info btn-md" %>
</div>
<% end %>
My controller :
def create
user = User.find_by(email: params[:email])
if user && user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to gossips_path, :notice => "Bienvenue <%= User.find_by(id: session[:user_id]).first_name %>"
else
flash.now[:danger] = 'Email ou Mot De Passe invalide'
render 'new'
end
end
My server :
Started POST "/sessions" for ::1 at 2020-10-04 13:10:25 +0200
Processing by SessionsController#create as JS
Parameters: {...}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."email" = $1 LIMIT $2 [["email", "..."], ["LIMIT", 1]]
↳ app/controllers/sessions_controller.rb:8
#<User:0x00007f667d7f36f0>
Redirected to http://localhost:3000/gossips
Completed 302 Found in 246ms (ActiveRecord: 0.2ms)
Started GET "/gossips" for ::1 at 2020-10-04 13:10:26 +0200
Processing by GossipsController#index as JS
Rendering gossips/index.html.erb within layouts/application
Gossip Load (0.4ms) SELECT "gossips".* FROM "gossips"
↳...
Rendered gossips/index.html.erb within layouts/application (75.5ms)
Completed 200 OK in 102ms (Views: 85.6ms | ActiveRecord: 12.8ms)
But my browser stay in the same page. I don't know why.
Can you help me?
<%= form_with url: sessions_path, method: "post", local: true do |f|%>
# ...
<% end %>
form_with unlike form_for defaults to remote: true. That means that the form will by default send an AJAX request and thus the redirect does nothing in the browser. You can see that its an AJAX request by:
Processing by SessionsController#create as JS

Rails form redirect renders in console only

I am using Rails 5.1 and want to include a search field in my website, so I have added a small form with "form_with". In my server log claims that all is rendered as expected but the page /products does not load anyway. Instead the page does not react to "Submit":
Rendered products/index.html.erb within layouts/application (24.5ms)
This is my form:
<%= form_with(url: products_path, method: 'get') do |form| %>
<%= form.label :q, 'Search for:' %>
<%= form.text_field :q, id: 'q', value: params[:q] %>
<%= form.submit 'Search' %>
<% end %>
And this is my complete server log, just in case it helps.
Started GET "/products?utf8=%E2%9C%93&q=x&commit=Search" for 127.0.0.1 at 2017-09-09 09:39:37 +0200
Processing by ProductsController#index as JS
Parameters: {"utf8"=>"✓", "q"=>"x", "commit"=>"Search"}
Rendering products/index.html.erb within layouts/application
Product Load (0.7ms) SELECT "products".* FROM "products"
Rendered collection of products/_product.html.erb [5 times] (2.8ms)
Rendered products/index.html.erb within layouts/application (24.5ms)
Completed 200 OK in 98ms (Views: 78.3ms | ActiveRecord: 0.7ms)
The search field is on a different page than products/index.html.erb. But nevertheless my server says that it is "rendered". Why am I not redirected to it then?

Bringing Form Values into a Controller - Ruby on Rails

I am trying to create a basic form where the user can change their password but needs to enter their old password in order to do it. I am having trouble verifying the user's old password. Everytime I enter an old password it says password doesn't match when I know that it does. If a replace the actual password in the authenticate field it works. How can I bring in what was entered in the form to verify the old password that was entered?
Form:
<%= form_for(#user, :url => change_password_action_path(current_user.id), html: { "role" => "form" }) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="form-group">
<%= f.label :old_password, "Old Password:", :class => "control-label" %>
<%= f.password_field :old_password, :class => "form-control" %>
</div>
<div class="form-group">
<%= f.label :password, "New Password:", :class => "control-label" %>
<%= f.password_field :password, :class => "form-control" %>
</div>
<div class="form-group">
<%= f.label :password_confirmation, "Password Confirmation:", :class => "control-label" %>
<%= f.password_field :password_confirmation, :class => "form-control" %>
</div>
<%= f.submit "Update Password", class: "btn btn-large btn-primary" %>
Controller
def change_password
#user = User.find(current_user.id)
end
def change_password_action
user = current_user.id
if User.find(user).authenticate(params[:old_password]) == false
flash[:danger] = "Password Doesnt Match: "
else
flash[:success] = "Password Match"
# Validate the new and confirm password.
end
redirect_to action: :change_password
end
Routes
get '/change_password' => 'main#change_password'
patch '/change_password_action' => 'main#change_password_action'
Rails Server Logs
Started PATCH "/change_password_action.1" for 127.0.0.1 at 2014-01-15 09:04:38 -0600
Processing by MainController#change_password_action as
Parameters: {"utf8"=>"✓", "authenticity_token"=>"yYdUx37Q7alr3SccuMVjPwCJoMgMPOaiKTesSsILlP4=", "user"=>{"old_password"=>"[FILTERED]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Update Password"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'fc1baf63bac072bfefd5ed27664ece5427ad9e64' LIMIT 1
{"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"yYdUx37Q7alr3SccuMVjPwCJoMgMPOaiKTesSsILlP4=", "user"=>{"old_password"=>"test123", "password"=>"", "password_confirmation"=>""}, "commit"=>"Update Password", "controller"=>"main", "action"=>"change_password_action", "format"=>"1"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Redirected to http://localhost:3000/change_password
Completed 302 Found in 115ms (ActiveRecord: 0.7ms)
Started GET "/change_password" for 127.0.0.1 at 2014-01-15 09:04:39 -0600
Processing by MainController#change_password as HTML
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'fc1baf63bac072bfefd5ed27664ece5427ad9e64' LIMIT 1
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Rendered shared/_error_messages.html.erb (0.1ms)
Rendered main/change_password.html.erb within layouts/application (2.6ms)
Rendered layouts/_header.html.erb (0.5ms)
Rendered layouts/_footer.html.erb (0.0ms)
Completed 200 OK in 19ms (Views: 16.2ms | ActiveRecord: 0.4ms)
It looks like you're passing the wrong parameter into your authenticate method.
Try using params[:user][:old_password] instead of params[:old_password].
The param value you want will be under the :user key, because your form_for is using a user object.
You can also see this in your server logs where the user param in your params hash is:
"user"=>{"old_password"=>"[FILTERED]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}

form submitting without performing any action rails

This is my login form
<%= form_for Customer.new, url: {action: :login} do |f| %>
<%= f.text_field :username,placeholder: 'Username or Email' %>
<%= f.password_field :password,placeholder: 'Password' %>
<%= f.submit 'Login' %>
<% end %>
This is my controller
def login
username = params[:username]
password = params[:password]
unless username.blank? && password.blank?
#My code doesn't entering to this block
end
end
If i submit form it is just submitted it doesn't perform any action
Edit 1
This is what my console return when form submitted
Started POST "/auth/login" for 127.0.0.1 at 2013-09-08 08:23:05 +0530
Processing by AuthController#login as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"9f7TKlIspKQwX7jMSzI7XGrabgJKvnzj8Ip0OLTDtW4=", "customer"=>{"username"=>"xxx", "password"=>"[FILTERED]"}, "commit"=>"Login"}
Rendered auth/login.html.erb within layouts/application (3.5ms)
Rendered layouts/_header.html.erb (0.4ms)
Completed 200 OK in 21ms (Views: 19.3ms | ActiveRecord: 0.0ms)
If you check your params hash you'll see that those keys don't exist. Instead, you'll see that params[:custpmer] does and that it contains those fields. This is just how rails builds the params in a form_for style. So try this instead:
username = params[:customer][:username]
password = params[:customer][:password]

Resources