Search code examples
ruby-on-railslink-to

link_to custom controller method with params


I am trying to use a link_to to trigger a custom controller method. The custom method is used to trigger a mailer.

My link_to in the view:

<%= link_to 'Resend sign up instructions', send_sign_up_instructions_path(team: @team.name, email: email), class: "button tiny radius" %>

My route:

resources :teams

get 'teams/send_sign_up_instructions', to: 'teams#send_sign_up_instructions', as: :send_sign_up_instructions

Custom method in TeamsController:

def send_sign_up_instructions
  team_name = params[:team]
  email = params[:email]
  TeamMailer.notify_signup(team_name, email)
end

Error output:

{"email"=>"[email protected]", "team"=>"Some js team", "action"=>"show", "controller"=>"teams", "id"=>"send_sign_up_instructions"}

Am I missing something blindinly obvious?


Solution

  • Seems like there is a route pointing to teams/:id defined in resources :teams, so you cannot use teams/something as another route url. Just change it to some url else rather than 'teams/...'

    This will work as an example:

    get 'team/send_sign_up_instructions', to: 'teams#send_sign_up_instructions', as: :send_sign_up_instructions