表单发布到错误的路径错误创建方法Simple_Form

时间:2014-03-23 23:38:13

标签: ruby-on-rails simple-form

第二次更新

这是我模型中的代码

after_initialize :start_two_weeks


    def start_two_weeks
        @subscription = TrialSubscription.all 
        @subscription.manual_expiry_date = 
        @subscription.save
    end 

如何动态设置时间从Time.now到Time.now - 2周? 它是一个datetime字段,datetime字段的值是多少? 更新更新 这意味着我没有注销

Filter chain halted as :require_no_authentication rendered or redirected

我退出了,用户确实已经创建但是通过注册控制器.....

更新 我刚刚意识到表单正在发布给/ users。注册控制器继承自devise :: registration

Started POST "/users" for 127.0.0.1 at 2014-03-23 19:58:08 -0400
Processing by RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"8xhRi7TBEA+zXfmuzuJLqLjjo5JMkyEjUmgmRT6DsOQ=", "user"=>{"email"=>"helloworld@gmail.com", "password"=>"[FILTERED]"}, "commit"=>"Create User"}
  User Load (0.7ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Redirected to http://localhost:3000/
Filter chain halted as :require_no_authentication rendered or redirected
Completed 302 Found in 4.1ms (ActiveRecord: 0.7ms)

表单不会发布到此处

new_trial_subscription GET        /trial_subscriptions/new(.:format)                      trial_subscriptions#new

POST       /trial_subscriptions(.:format)                          trial_subscriptions#create

好的我正在尝试重现我在rails控制台中所做的事情,将它们放入控制器中。

我有两个型号

class TrialSubscription < ManualSubscription

    accepts_nested_attributes_for :user
    attr_accessible :user_attributes 

end 

ManualSubscription继承自Subscription和Subscription模型

belongs_to :user

由于其他原因,我正在通过订阅构建用户模型。

在我的rails控制台中我做了这些

@subscription = TrialSubscription.new
@user = @subscription.build_user(email: "helloworld@gmail.com", password: "thisiseightcharacters")
@subscription.save

我可以同时创建用户及其相关的试用订阅。我试图在控制器中重现这一点,内容将由用户输入。

这是

中的表格

app / views / trial_subscriptions / new.html.erb

<%= simple_form_for @subscription.user do |f| %>
  <%= f.input :email %>
  <%= f.input :password %>
  <%= f.button :submit %>
<% end %>

我添加了

resources :trial_subscriptions

所以网址为trial_subscriptions/new

这是我的控制器

class TrialSubscriptionsController < ApplicationController

    def index
    end 

    def new
        @subscription = TrialSubscription.new
        @subscription.build_user 
    end 

    def create
        @subscription = TrialSubscription.new(params[:subscription])
        @subscription.email = :email
        @subscription.password = :password
        if @subscription.save 
            flash[:notice] = "Trial Subscription has started"
        else
            flash[:error] = "There was an error creating this subscription, please try again"
        end 
    end 
end 

1)new和create之间发生了什么?

2)在我的create方法中,params [:subscription]指向我表单中的@subscription对象是正确的吗?

3):电子邮件实际上是用户属性,它是否正确地在@ subscription.email =:email发送?

1 个答案:

答案 0 :(得分:1)

您的新操作看起来正确,您的创建操作需要一些更新。让我们先从您的关联开始,然后转到表单,然后转到create行动。

在表单视图代码中,您正在为@subscription.user构建表单。 simple_form_for方法会将此转化为create中的UsersController操作。但是你没有这个,你打算通过user创建TrialSubscriptionsController

因此,为了实现这一目标,您需要更改表单,以便在呈现和/或提交表单时创建trial_subscription的表单以及user_attributes中定义的trial_subscription }。因此,请将您的视图更新为:

<%= simple_form_for @subscription do |f| %>
  <%= f.simple_fields_for :user do |u| %>
    <%= u.input :email %>
    <%= u.input :password %>
  <% end %>
  <%= f.button :submit %>
<% end %>

更改为simple_form_for @subscription.usersimple_form_for @subscription,并emailpassword字段包含f.simple_fields_for :user。通过这些更改,当您的表单提交时,它将执行create的{​​{1}}操作。

下一个更改是TrialSubscriptionsController操作。

TrialSubscriptionsController#create

此处的更改为def create @subscription = TrialSubscription.new(params[:trial_subscription]) if @subscription.save flash[:notice] = "Trial Subscription has started" else flash[:error] = "There was an error creating this subscription, please try again" end end @subscription = TrialSubscription.new(params[:subscription]),这是因为您的对象属于@subscripition = TrialSubscription.new(params[:trial_subscription])而非TrialSubscription

第二个变化是删除以下行:

Subscription

这是因为,正如您所说,@subscription.email = :email @subscription.password = :password emailpassword而非user的成员。

希望这有帮助。