Rails:使用表单调用自定义方法(带变量)

时间:2013-10-15 02:08:56

标签: ruby-on-rails devise routes haml controllers

我正在尝试使用此表单使用自定义方法将变量传递给我的控制器。它似乎没有工作。当我提交表单时,它不显示页面加载或任何内容。

HAML查看

= 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

路线:

    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

1 个答案:

答案 0 :(得分:0)

由于:remote => true方法中的form_for,您在提交表单时看不到页面加载。这使表单通过ajax提交,不会重新加载页面。