【发布时间】:2016-04-07 23:39:53
【问题描述】:
我现在有一个表单,它有 4 个 text_fields 并且只有底部的一个实际上将数据添加到模型中?所有的 text_fields 都是相同的,对于我的生活,我无法弄清楚为什么它们都不一样。这是我的代码,希望有人能轻松回答?
class ResponsesController < ApplicationController
def new
@response = Response.new
end
def create
@response = Response.new(response_params)
if @response.save
flash[:notice] = "Response has been edited"
redirect_to new_response_path(:response)
else
render "new"
end
end
private
def response_params
params.require(:response).permit(:message)
end
这是我的看法
<div class="container">
<div class="row">
<h3 class="text-center">Edit The Bounce Back Response</h3>
<div class="col-lg-8 col-lg-offset-2 well">
<%= form_for @response do |form| %>
<div class="form-group">
<%= form.label :message, "Visitor:", class: "response_label"%>
<%= form.text_field :message, class: "form-control", placeholder: "Change Visitor Response!" %>
</div>
<div class="form-group">
<%= form.label :message, "Staff:", class: "response_label"%>
<%= form.text_field :message, class: "form-control", placeholder: "Change Staff Response!" %>
</div>
<div class="form-group">
<%= form.label :message, "Volunteeer:", class: "response_label"%>
<%= form.text_field :message, class: "form-control", placeholder: "Change Volunteer Response!" %>
</div>
<div class="form-group">
<%= form.label :message, "Dance:", class: "response_label"%>
<%= form.text_field :message, class: "form-control", placeholder: "Change Dance Response!" %>
</div>
<%= form.submit "Update", class: "btn btn-primary" %>
<% end %>
</div>
如果您在底部文本字段中输入,它实际上会将数据输入到消息中,如果您使用任何其他文本字段,我的控制台返回是这样的
【问题讨论】:
-
你能发布你的
responses_controller.rb文件吗? -
您的
edit and update方法在哪里?你的 form_for 也位于_form.html.erb吗? -
我还没有,我只是想让他们 text_field 将数据输入到模型中
-
@但你正在尝试更新它。你应该创建更新方法。
标签: ruby-on-rails ruby form-helpers