【发布时间】:2016-05-29 10:22:22
【问题描述】:
根据互联网的各个部分,我有以下代码:
控制器
class StudiesController < ApplicationController
def new
require_user
@study = Study.new
end
def create
@study = Study.new(study_params)
if @study.save
flash[:success] = "You made it!"
redirect_to root_path
else
flash[:danger] = "Uh oh—something went wrong"
redirect_to study_path
end
end
end
查看
<%= form_for(@study, url: {action: :create}, class: "study-form") do |f| %>
<%= f.text_field :title %><br>
<div class="btn-submit">
<%= f.submit "I studied today!", class: "btn btn-primary margin-top" %>
</div>
<%= end %>
它有效,但我的问题是:为什么我需要两次调用Study.new?如果我已经在new 中调用它,为什么还要在create 中调用它?
【问题讨论】:
-
尝试删除
url: {action: :create},你不需要那个。以及结束函数上的=。
标签: ruby-on-rails forms ruby-on-rails-4 crud