form submitting without performing any action rails - ruby-on-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]

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 %>

Missing Param in Rails 6 Form

This is my first Rails 6 project. Stuck on this. Any advice would be appreciated.
I have a form on index.html.erb
<%= form_with model: #volunteer_submission do |form| %>
<%= form.text_field :email %>
<%= form.text_field :first_name %>
<%= form.text_field :last_name %>
<%= form.submit %>
<% end %>
with a controller
def create
#volunteer_submission = VolunteerSubmission.new(volunteer_submission_params)
#volunteer_submission.save
redirect_to #volunteer_submissions
end
def volunteer_submission_params
params.require(:volunteer_submission).permit(:email, :first_name, :last_name)
end
I get this
Started POST "/volunteer_submissions" for ::1 at 2020-03-29 13:55:38 -0700
Processing by VolunteerSubmissionsController#create as JS
Parameters: {"authenticity_token"=>"OIlkqKqXJeA7XjYPqQHubZFGm64ivCe+9fimFwO8uBrtXJ3iPyiIZbgWtWS4FXFnS+KhZYCT5CgzN76YCTqC+Q==", "email"=>"a", "first_name"=>"a", "last_name"=>"a", "commit"=>"Save "}
Completed 400 Bad Request in 2ms (ActiveRecord: 0.0ms | Allocations: 666)
ActionController::ParameterMissing (param is missing or the value is empty: volunteer_submission):
app/controllers/volunteer_submissions_controller.rb:16:in `volunteer_submission_params'
app/controllers/volunteer_submissions_controller.rb:10:in `create'
If I leave out the volunteer_submission_params..it will attempt it...but I think rolls back because I have the DB set up for it not to accept null values. So...I'm not sure why the params aren't passing.
Started POST "/volunteer_submissions" for ::1 at 2020-03-29 13:40:33 -0700
Processing by VolunteerSubmissionsController#create as JS
Parameters: {"authenticity_token"=>"1cpAIcVZuM/qcJdt1KNg2rYMIX58yDVCO5mRMXmbiUjMHr81cAseqJZN/TDYDFaGXGLPJhs7iIwhvQgr5g/RRg==", "email"=>"a", "first_name"=>"a", "last_name"=>"a", "commit"=>"Save "}
(0.3ms) BEGIN
↳ app/controllers/volunteer_submissions_controller.rb:11:in `create'
VolunteerSubmission Create (7.1ms) INSERT INTO "volunteer_submissions" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id" [["created_at", "2020-03-29 20:40:33.125665"], ["updated_at", "2020-03-29 20:40:33.125665"]]
↳ app/controllers/volunteer_submissions_controller.rb:11:in `create'
(0.4ms) ROLLBACK
↳ app/controllers/volunteer_submissions_controller.rb:11:in `create'
Completed 500 Internal Server Error in 14ms (ActiveRecord: 7.8ms | Allocations: 3296)
ActiveRecord::NotNullViolation (PG::NotNullViolation: ERROR: null value in column "email" violates not-null constraint
routes
Rails.application.routes.draw do
resources :volunteer_submissions
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
get 'volunteers', to: 'volunteer_submissions#index'
root to: 'pages#home'
end
I believe your issue is the fact that you are sending your params without a volunteer_submission key.
Based on this method
def volunteer_submission_params
params.require(:volunteer_submission).permit(:email, :first_name, :last_name)
end
Your controller is expecting { "authenticity_token"=>"OIlkqKqXJeA7XjYPqQHubZFGm64ivCe+9fimFwO8uBrtXJ3iPyiIZbgWtWS4FXFnS+KhZYCT5CgzN76YCTqC+Q==", "volunteer_submission" => { "email"=>"a", "first_name"=>"a", "last_name"=>"a" }}.
the form should look like this <%= form_with(model: item, local: true) do |form| %> also in model: item you should put the name of your model rb file located in app/models
in my case its app/models/item.rb
so my form is <%= form_with(model: item, local: true) do |form| %>
for me I had this issue because I was using an html input inside the rails form and was passing in the name of value without the model name.
<% form_with model: order_item do |f| %>
<input type="number" name="weight_in_oz"></input>
<% end %>
params look like this
{"authenticity_token"=>"...", "weight_in_oz"=>"16", "commit"=>"Add To Cart", "controller"=>"order_items", "action"=>"create"} permitted: false>
I fixed this by adding the model name before the input name and put the attribute in an array which nests the attribute inside of a model hash inside of params.
<% form_with model: order_item do |f| %>
<input type="number" name="order_item[weight_in_oz]"></input>
<% end %>
and this is the params after just what I was expecting
{"authenticity_token"=>"....", "order_items"=>{":weight_in_oz"=>"16"}, "commit"=>"Add To Cart", "controller"=>"order_items", "action"=>"create"} permitted: false>

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]"}

Create Action Doesn't Insert - Rails 3

I'm creating a profile that is associated with a member id based on Devise authentication.
When creating the profile, the values aren't inserted into the database.
Routes file.
resources :troopers do
resource :trooper_profile
end
Trooper model
has_one :trooper_profile
Trooper Profile model
belongs_to :trooper
Trooper Controller #create action
def create
#trooper = current_trooper
#profile = #trooper.build_trooper_profile(params[:profile])
respond_to do |format|
if #profile.save
format.html { redirect_to(trooper_trooper_profile_path, :notice => 'Profile was successfully created.') }
else
format.html { render :action => "new" }
end
end
end
Profile Form
<%= form_for #profile, :url => trooper_trooper_profile_path(#trooper) do |f| %>
<%= f.label :first_name %><br />
<%= f.text_field :first_name %><br /><br />
<%= f.label :last_name %><br />
<%= f.text_field :last_name %><br /><br />
<p><%= submit_tag "Create Profile" %></p>
<% end %>
Server output
Started POST "/troopers/1/trooper_profile" for 127.0.0.1 at 2011-05-20 13:04:01 +1000
Processing by TrooperProfilesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"LhT6b4xu5bIJ5kwhS74L7dpaGbuR5BTdirh9AziD+Ew=", "trooper_profile"=>{"first_name"=>"Robert", "last_name"=>"", "commit"=>"Create Profile", "trooper_id"=>"1"}
Trooper Load (0.5ms) SELECT "troopers".* FROM "troopers" WHERE ("troopers"."id" = 1) LIMIT 1
TrooperProfile Load (0.3ms) SELECT "trooper_profiles".* FROM "trooper_profiles" WHERE ("trooper_profiles".trooper_id = 1) LIMIT 1
Rendered trooper_profiles/_form.html.erb (11.7ms)
Rendered shared/_head.html.erb (1.6ms)
Rendered shared/_menutop.html.erb (1.2ms)
Rendered trooper_profiles/new.html.erb within layouts/application (18.5ms)
Completed 200 OK in 109ms (Views: 22.5ms | ActiveRecord: 0.8ms)
I've got this same setup in another application and can't see why this isn't working.
Change the following...
#profile = #trooper.build_trooper_profile(params[:trooper_profile])
It appears the parameters being passed are :trooper_profile
trooper_profile"=>{"first_name"=>"Robert", "last_name"=>"", "commit"=>"Create Profile", "trooper_id"=>"1"}
However, you're referencing :profile
If it's rendering the new view, then the profile isn't saving - perhaps it's a validation error? Does TrooperProfile require a last name (blank in your example)?

Resources