0

I have a student_admissions controller with a show and foo methods. My intention is to have a button to send an email when a student passed an admissions threshold so I do not have a view named foo as I had planned to intercept the call, send my email and redirect back to the same page.

show works and binding.pry if I put it in show. I can even call my mailer from irb here. But I am missing something about ruby magic when calling my foo method. None of my foo code ever seems to run, it never stops on binding.pry and there is no evidence in the rails s output that by sql query was run. Then rails s output is always

T "/student_admissions/foo" for 127.0.0.1 at 2023-03-30 08:09:47 -0500
Processing by StudentAdmissionsController#foo as HTML
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."active" = $1 AND "users"."employee" = $2 AND "users"."id" = $3 LIMIT $4  [["active", true], ["employee", true], ["id", 3737], ["LIMIT", 1]]
  User: 2
Sent file public/404.html (0.3ms)
Completed 404 Not Found in 5ms (ActiveRecord: 0.4ms)

My rails routes | grep something output. I put in both get and put as a test to see if I was calling the wrong one.

                        student_admissions_show GET     /student_admissions/show(.:format)                           student_admissions#show
                student_admissions_readmit_info GET     /student_admissions/readmit_info(.:format)                   student_admissions#readmit_info
             student_admissions_semester_status GET     /student_admissions/semester_status(.:format)                student_admissions#semester_status
 student_admissions_foo POST    /student_admissions/foo(.:format)    student_admissions#foo
                                                GET     /student_admissions/foo(.:format)    student_admissions#foo

Part of my student_admissions contoller

class StudentAdmissionsController < SecureController
  before_action :find_person

...
   
  def foo
    @person_semesters = @person.person_semesters
    .includes(:semester)
    .references(:semester)
    .order('semesters.start_date DESC')
binding.pry
    WelcomeNewApplicantMailer.send_email(@person).deliver
    redirect_to student_admissions_show_path
  end
end

I have tried these different link calls but nothing seems to work.

    <%= link_to_protected 'Button Welcome Email', :action => 'foo' %>
    <br>
    <%= link_to 'Call Action1', student_admissions_foo_path, method: :post %>
    <br>   
    <%= button_to 'Call Action2', student_admissions_foo_path, method: :post %>
    <br>   
    <%= button_to 'Call Action3', student_admissions_foo_path, method: :get %>

How to call a controller method from a button in rails 4 was a help, but has not solved this for me. What stupid little thing am I missing? Is rails stumbling on the fact that I do not have a foo view

wruckie
  • 1,717
  • 1
  • 23
  • 34
  • 3
    your controller is assuming a person_id for the find_person before_action, even with your button_to post link -- there is no person_id being sent with the call, so it is likely the before action is throwing the 404. try making your button_to url `student_admissions_foo_path(person_id: @person.id)` – dbugger Mar 30 '23 at 13:54

0 Answers0