Ruby on rails 表格(适用于辅助人员)Rails
我现在有一个表单,它有4个文本字段,只有最下面的一个字段将数据添加到模型中?所有的文本字段都是相同的,就我个人而言,我不明白为什么它们的工作方式不一样。这是我的代码,希望有人会有任何简单的答案Ruby on rails 表格(适用于辅助人员)Rails,ruby-on-rails,ruby,form-helpers,Ruby On Rails,Ruby,Form Helpers,我现在有一个表单,它有4个文本字段,只有最下面的一个字段将数据添加到模型中?所有的文本字段都是相同的,就我个人而言,我不明白为什么它们的工作方式不一样。这是我的代码,希望有人会有任何简单的答案 class ResponsesController < ApplicationController def new @response = Response.new end def create @response = Response.new(response_params) if @resp
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>
编辑反弹响应
如果您在底部文本字段中键入,它将实际将数据输入消息,如果您使用任何其他文本字段,我的控制台返回如下
这是因为您每次都要将表单提交到: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
它是用哈默尔语写的,但几乎一模一样。您可以看到,我的文本_字段用于标题,而我的正文只是命名内容
在控制器中,我将创建强参数
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
class PostsController
试试这个。据我所知,您希望更新该字段。它应该对你有帮助
<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>
你可以发布你的
响应\u controller.rb
文件吗?你的编辑和更新方法在哪里?另外,您的表单位于\u form.html.erb
?我还不在那里,我只希望他们的文本字段将数据输入到model@but你正在尝试更新它。你应该创建更新方法。我同意,但我该如何修复它?而不是:message你可以将它们更改为描述它们的内容。message确实描述了它,我希望它们都发布到message中,因为最终每个都将属于不同的组,这取决于用户在保存数据的控制器上选择的字段。您可以创建强参数,并允许它们在提交时保存在控制器中表单同时发布,因此将它们全部保存为一个参数不起作用。只有最后一个参数实际发布到路由。很抱歉,这里的评论太多,没有那么清晰。我用一只胳膊,因为孩子们在扔东西。我会把这篇文章编辑得更清楚
def response_params
params.require(:response).permit(:message, :staff, :volunteer, :dance)
end