【问题标题】:Form_for helpers - RailsForm_for 助手 - Rails
【发布时间】: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


【解决方案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

【讨论】:

  • 我同意,但我该如何解决?
  • 代替 :message 您可以将它们更改为描述它们的内容。
  • 消息确实描述了它我希望所有人都发布到消息中,因为最终每个人都将属于不同的组,具体取决于用户选择的字段
  • 发布保存数据的控制器。您可以创建强参数并允许它们在提交时保存在控制器中
  • 表单是同时发布的,因此将它们都保存为一个参数是行不通的。只有最后一个参数实际发布到路由。很抱歉 cmets 的弹幕,而不是清晰的帖子。我用一只胳膊,因为孩子们正在投掷。我将编辑帖子以使其更清晰
【解决方案2】:

试试这个。据我了解,您想更新该字段。它应该对你有帮助。

  <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>

在控制器中更新强参数

def response_params
    params.require(:response).permit(:message, :staff, :volunteer, :dance)
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多