简体   繁体   English

Ruby on Rails - 节省用户输入的麻烦

[英]Ruby on Rails - Saving user input troubles

I have been trying to allow the user to input a name for an activity and then save it into the database. 我一直在尝试允许用户输入活动的名称,然后将其保存到数据库中。 However, I am running into the the multiple issues 但是,我遇到了多个问题

The first of which is the following: 第一个是以下内容:

ActionController::ParameterMissing in ActivitiesController#create ActionController :: ParametersMissing在ActivitiesController #create中

and the line it refers to is the following: 它引用的行如下:

params.require(:activities).permit(:a_name) params.require(:活动).permit(:a_name)

I have looked some solutions to this and from what I understand, it stems from the "require(:activities)" part of that line. 我已经看了一些解决方案,根据我的理解,它源于该行的“require(:activities)”部分。 What I don't understand is why having that particular line results in the error. 我不明白的是为什么让那条特定的行导致错误。

Removing the ".remove(:activities)" will fix it but I run into another issue where when I input a name and then click the "Create" button, it thinks that I did not have a name and throws a validation error. 删除“.remove(:activities)”将修复它,但我遇到另一个问题,当我输入一个名称,然后单击“创建”按钮时,它认为我没有名称并抛出验证错误。

Validation failed: A name can't be blank, A name is too short (minimum is 1 character) 验证失败:名称不能为空,名称太短(最小为1个字符)

and the line it refers to: 以及它所指的线:

if @activities.save! if @ activities.save!

This leads me to believe that my code for the "form_for" is incorrect and is not treating what is in the text field as an input for the name. 这让我相信我的“form_for”代码不正确,并没有将文本字段中的内容视为名称的输入。 I'm still very new to HTML and Ruby on Rails so could anyone see what I am doing wrong? 我仍然是HTML和Ruby on Rails的新手,所以任何人都可以看到我做错了什么? Thanks! 谢谢!

Activities Model: 活动模式:

class Activity < ApplicationRecord

  validates :a_name, presence: true, length: {minimum: 1}    
end

Activities Controller: 活动总监:

class ActivitiesController < ApplicationController
  def display
    @activities = Activity.all
  end

  def index
    @activities = Activity.all
  end

  def show
  end

  def new
    @activities = Activity.new
  end

  def create
    @activities = Activity.create(activities_params)
    if @activities.save!
      flash[:success] = 'Activity created successfully!'
      redirect_to activities_display_path
    else
      flash[:error] = 'ERROR: Activity was not saved!'
      #render_to_string #normally would have it render to the name of view ex: :new
    end
  end

  def edit
    @activities = Activity.find(params[:id])
  end

  def update
    @activities = Activity.update(activities_params)
    if @activities.save!
      flash[:success] = 'Activity successfully updated!'
      redirect_to root
    else
      flash[:error] = 'ERROR: Activity failed to update'
      render_to_string
    end
  end

  private
    def activities_params
       params.require(:activities).permit(:a_name)
    end

end

Activities New HTML: 活动新HTML:

<body>
<% flash.each do |key, value| %>
    <div class="alert alert-<%= key %>"><%= value %></div>
<% end %>

<%= form_for @activities  do |a| %>
    <div class = "form-group">
      <div class = "row top-buffer text-center">
        <div class='col-md-3'>
          <%= a.label :a_name, 'Activity Name' %>:
          <%= a.text_field :a_name %>
        </div>
      </div>

      <div class ="row top-buffer text-center">
        <div class="col-md-3">
          <%= a.submit 'Create', class: 'btn btn-primary'%>
        </div>
      </div>
    </div>
<% end %>

</body>

As Max commented, my mistake was thinking the parameter was "activities[a_name]" and not "activity[a_name]". 正如Max评论的那样,我的错误是认为参数是“activities [a_name]”而不是“activity [a_name]”。

Changing 更改

params.require(:activities).permit(:a_name)

to

params.require(:activity).permit(:a_name)

fixed the first error and subsequently allowed me to save user inputs. 修复了第一个错误,随后允许我保存用户输入。 Thanks for the help Max 感谢Max的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM