如何使用simple_form将任意参数发送到控制器

时间:2017-01-30 05:07:12

标签: ruby-on-rails ruby simple-form

我正在构建简报管理表单,我想使用simple_form。 email参数应通过email_subscriber#manage方法发送到POST控制器/操作。

的routes.rb

get 'email/login' => 'email_subscribers#login', as: 'email_login'
get 'email/manage' => 'email_subscribers#manage', as: 'email_manage'

email_subscribers_controller.rb

def login

end

def manage
    @subscriber = EmailSubscriber.find_by_email(safe_params(:email))

    unless @subscriber
        # redirect_to email_login_path, notice: 'That email does not exist.'
    end
end

电子邮件/登录表单

<%= render :layout => 'application/container' do %>
    <%= simple_form_for(@subscriber, path: :email_manage_path, method: :get) do |f| %>
            <%= f.error_notification %>

            <div class="form-inputs">
                <%= f.input :email, as: :email %>
            </div>

            <div class="form-actions">
                <%= f.button :submit, value: 'Manage Subscription' %>
            </div>
    <% end %>

登录路线是表单的位置。它允许用户输入他们的电子邮件以取消订阅时事通讯。

表单应重定向到manage操作,并传递没有相应模型的email参数。

目前的表格不起作用。由于某种原因,它会重定向到EmailSubscribers索引页面。 将email_manage路由更改为POST会导致missing route POST email/login,这是没有意义的,因为表单已发布到email_manage_path,而不是email_login_path

由于

编辑:

rake路线输出(在同一标签中打开)

http://pastebin.com/eFGdvxid

1 个答案:

答案 0 :(得分:0)

您实际上可以将其建模为传统的RESTful资源:

resources :subscriptions
namespace :subscriptions do
  resources :logins
  get '/login', to: 'logins/create'
end

优点是你可以使用规范的crud动词获得更简单的设置,并且你也可以使用正确的HTTP动词。

这里唯一的非常规部分是我们通过GET向create添加了另一条路线:

# app/controllers/subscriptions/logins_controller.rb
class Subscriptions::LoginsController < ApplicationController
  rescue_from ActionController::ParameterMissing, with: :subscription_not_found

  # GET /subscriptions/logins/new
  def new
    @subscription = Subscription.new
  end

  # POST /subscriptions/logins
  # GET /subscriptions/login
  def create
    @subscription = Subscription.find_by_email(email_param)
    if @subscription
      redirect_to edit_subscription_path(@subscription)
    else
      subscription_not_found
    end
  end

  private

  def subscription_not_found
    render :new, error: 'Email could not be found.'
  end

  def email_param
    if request.post?
      params.require(:subscription).fetch(:email)
    else
      params.fetch(:email)
    end
  end
end

由于我们实际上是绑定到资源,因此您可以非常直接地设置表单。我们还添加了一条GET路由,允许用户直接从链接登录。

表格非常直接。

<%= simple_form_for(@subscription, path: subscriptions_sessions_path) do %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :email, as: :email %>
  </div>

  <div class="form-actions">
    <%= f.button :submit, value: 'Manage Subscription' %>
  </div>
<% end %>

然后,您可以创建一个漂亮的mill CRUD控制器,让用户可以编辑或取消订阅:

# app/controllers/subscriptions_controller.rb
class SubscriptionsController < ApplicationController

  before_action :set_subscription, only: [:edit, :update, :destroy]
  rescue_from ActiveRecord::RecordNotFound, with: :invalid_email

  def new
    @subscription = Subscription.new
  end

  def create
    @subscription = Subscription.new(subscription_params)
    if @subscription.save(subscription_params)
      redirect_to root_path, success: 'Your subscription settings have been creates'
    else
      render :new
    end
  end

  def edit
  end

  def update
    if @subscription.update(subscription_params)
      redirect_to root_path, success: 'Your subscription settings have been updated'
    else
      render :edit
    end
  end

  def destroy
    @subscription.destroy
    redirect_to root_path
  end

  private 

  def set_subscription
    @subscription = Subscription.find(params[:id])
  end

  def subscription_params
    params.require(:subscription).permit(:foo, :bar, :baz)
  end
end