Question

I am trying to use this form to pass a variable on to my controller with a custom method. It doesn't seem to be working. It doesn't show a page loading or anything when I submit the form.

HAML View

= form_for :emails, :url=>{ :action => "make_admin", :controller => "member"}, :remote => true, :html => {:class => "form-inline"} do |f|
  .form-group
    %span
      %p{:style => "float: left;"}
        = f.label :email, "Email:"
        = f.text_field :email, :style=> "width: 200px;", :class => "form-control"
      %p{:style => "margin-left: 10px; float: left;"}
        = f.submit "Make Admin", :class => 'btn btn-success'

Controller (member_controller.rb)

class MemberController < ApplicationController
  before_filter :authenticate_user!, :except => [:show, :index]
  layout "dashboard"

  def dashboard
    render :dashboard
  end

  def develop
  end

  def make_admin
    @this_user = User.find(params[:email])
    @this_user.update_attribute :admin, true
    redirect_to '/dashboard/develop'
  end
end

Routes:

    Mvhomefront::Application.routes.draw do
  match '/dashboard' => redirect("/dashboard/volunteer"), via: 'get'
  match '/dashboard/volunteer', to: 'member#volunteer', via: 'get'
  match '/dashboard/events', to: 'events#index', via: 'get'
  match '/dashboard/about', to: 'member#about', via: 'get'

  resources :posts
  resources :events
  resources :signup
  resources :emails do
    get :make_admin
  end
  match '/dashboard/news', to: 'posts#new', via: 'get'
  match '/dashboard/officers', to: 'member#officers', via: 'get'
  match '/dashboard/develop', to: 'member#develop', via: 'get'
  match '/dashboard/manage_events', to: 'events#new', via: 'get'
  match '/dashboard/manage_hours', to: 'member#manage_hours', via: 'get'
  match '/dashboard/manage_users', to: 'member#manage_users', via: 'get'


  root :to => "home#index"
  match '/about', to: 'home#about', via: 'get'
  match '/news', to: 'posts#index', via: 'get'
  match '/officers', to: 'home#officers', via: 'get'

  devise_for :users, :controllers => {:omniauth_callbacks => "users/omniauth_callbacks", :registrations => "registrations"}, :path_names => { :sign_up => "register" }
  resources :users
end
Was it helpful?

Solution

You won't see the page loading when the form is submitted because of the :remote => true in your form_for method. This makes the form submit via ajax, which will not reload the page.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top