2017-01-30 71 views
0

我正在构建新闻稿管理表单,我想使用simple_form。 email参数应通过POST方法发送到email_subscriber#manage控制器/操作。如何使用simple_form向控制器发送任意参数

的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

感谢

编辑:

耙路输出(在此同一选项卡中打开)

http://pastebin.com/eFGdvxid

+0

你可以发表你的路线? '耙路线' –

+0

添加了一个完整的'rake routes'输出的pastebin – danielbker

回答

0

实际上,你可以模拟这种作为传统的基于REST的资源,而不是:

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 %> 

然后,您可以创建磨机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 
+0

我需要一些时间让它沉入。在技​​术上不回答这个问题,因为我仍然想知道如何在没有模型的情况下使用simple_form,但我喜欢你的方法。非常聪明!现在将您的答案集成到我的应用程序中。 – danielbker

+0

通过创建一个包含“ActiveModel :: Model”的“虚拟”模型,或者您只需使用普通的旧rails标记助手来创建输入元素,就可以在没有表格支持的模型中使用它。如果没有模型,SF没有建立起来。 – max

+0

同时它也是一个好主意,可以创建您通过电子邮件发送的令牌,而不是将用户电子邮件作为参数。 http://api.rubyonrails.org/classes/ActiveRecord/SecureToken/ClassMethods.html – max