我现在有一个表格有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>
如果您键入底部文本字段,它实际上会将数据输入到消息中,如果您使用任何其他文本字段,我的控制台将返回此
答案 0 :(得分:1)
这是因为您每次都将表单提交到:message。因此,当提交表单时,它会将每个字段发布到params [:message],因此实际上只发布了最后一个。
编辑:
例如,如果我有一个帖子表格:
= form_for @post do |f|
.input-title
= f.text_field :title, placeholder: "Title", required: true
.input-content
= f.text_area :content, class: 'ckeditor', required: true
.input-submit
= f.submit
它是用haml编写的,但它几乎完全相同。你可以看到我的text_field是标题,我的身体只是命名内容。
在控制器中我会创建强大的参数
class PostsController < ApplicationController
def create
@post = current_user.posts.build(post_params) # this is where the params are saved
@post.forum_id = params[:forum_id]
if @post.save
usercount = current_user
usercount.post_count += 1
usercount.save
redirect_to forum_posts_path
else
render 'new'
end
end
private
def post_params
params.require(:post).permit(:title, :content) # this is permitting what can be saved from the form
end
答案 1 :(得分:0)
试试这个。据我了解,您想要更新该字段。它应该对你有帮助。
<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 :staff, "Staff:", class: "response_label"%>
<%= form.text_field :staff, class: "form-control", placeholder: "Change Staff Response!" %>
</div>
<div class="form-group">
<%= form.label :volunteer, "Volunteeer:", class: "response_label"%>
<%= form.text_field :volunteer, class: "form-control", placeholder: "Change Volunteer Response!" %>
</div>
<div class="form-group">
<%= form.label :dance, "Dance:", class: "response_label"%>
<%= form.text_field :dance, class: "form-control", placeholder: "Change Dance Response!" %>
</div>
<%= form.submit "Update", class: "btn btn-primary" %>
<% end %>
</div>
在Controller Update Strong Params中
def response_params
params.require(:response).permit(:message, :staff, :volunteer, :dance)
end