Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails-创建和更新操作don';行不通_Ruby On Rails_Ruby On Rails 5 - Fatal编程技术网

Ruby on rails Rails-创建和更新操作don';行不通

Ruby on rails Rails-创建和更新操作don';行不通,ruby-on-rails,ruby-on-rails-5,Ruby On Rails,Ruby On Rails 5,我是Rails的初学者,我有一个SupElement控制器,不能创建或编辑SupElement(delete很好)。我没有收到任何错误,只是当我点击时什么都没有发生,并且从控制台一切正常。我尝试了我所知道的一切(不多),但我找不到这样的问题,类似的答案也没用。谢谢你的帮助,谢谢 class SuplementsController < ApplicationController before_action :set_suplement, only: [:show, :edit, :up

我是Rails的初学者,我有一个SupElement控制器,不能创建或编辑SupElement(delete很好)。我没有收到任何错误,只是当我点击时什么都没有发生,并且从控制台一切正常。我尝试了我所知道的一切(不多),但我找不到这样的问题,类似的答案也没用。谢谢你的帮助,谢谢

class SuplementsController < ApplicationController
  before_action :set_suplement, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!
  def index
   @suplement = Suplement.all.order("created_at DESC")
  end

  def new
    @suplement = Suplement.new
  end

  def create
    @suplement = Suplement.new(suplement_params)
  if @suplement.save
    redirect_to '/suplements'
  else
    render '/suplements/new'
  end
end

def show
end

def edit
end

def update
  if @suplement.update(suplement_params)
    redirect_to '/suplements'
  else
    redirect_to '/suplements/new'
  end
end

def destroy
  @suplement.destroy
    redirect_to '/suplements'
end

private

  def set_suplement
    @suplement = Suplement.find(params[:id])
  end

  def suplement_params
    params.require(:suplement).permit(:name,
                                      :number_of_units,
                                      :daily_dosage_in_units,
                                      :number_of_days,
                                      :suplement_cost
                                      )
  end
end
class SupplementsController
以下是一个视图:

<h1>Create new suplement</h1>

  <%= form_for(@suplement) do |f| %>

  <%= render 'form', suplement: @suplement %>

  <div class="actions">
    <%= f.submit %>
  </div>

<% end %>
创建新的辅助元素
这是一张表格:

<%= form_for(@suplement) do |f| %>
    <% if @suplement.errors.any? %>
      <div id="error_explanation">
        <h2><%= pluralize(@suplement.errors.count, "error") %> prohibited this suplement from being saved:</h2>

        <ul>
        <% @suplement.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
        </ul>
      </div>
    <% end %>

    <div class="field">
      <%= f.label :name %>
      <%= f.text_field :name %>
    </div>

    <div class="field">
      <%= f.label :number_of_units %>
      <%= f.text_field :number_of_units %>
    </div>

    <div class="field">
      <%= f.label :daily_dosage_in_units %>
      <%= f.text_area :daily_dosage_in_units %>
    </div>

    <div class="field">
      <%= f.label :number_of_days %>
      <%= f.text_area :number_of_days %>
    </div>

    <div class="field">
      <%= f.label :suplement_cost %>
      <%= f.text_area :suplement_cost %>
    </div>

  <% end %>

禁止保存此元素:
还有我的模型:

class Suplement < ApplicationRecord
  belongs_to :user
  validates :name,
            :number_of_units,
            :daily_dosage_in_units,
            :number_of_days,
            :suplement_cost,
            presence: true
end
class supelement

class用户
在控制器的
#edit
中,您需要设置
@supelement
变量的值

def edit
  @suplement = Suplement.find(params[:id])
end
您还应该在
#update
方法中包含上述行作为第一行

def update
  @suplement = Suplement.find(params[:id])
  if @suplement.update_attributes(suplement_params)
    # continued...
end

看起来问题在于你有两张表格。 u在
\u form.html.erb
文件和
新的.html.erb
文件中都有@supelement
表单。尝试将其从
new.html.erb
中删除,使您的文件如下所示

new.html.erb

<h1>Create new suplement</h1>
<%= render 'form', suplement: @suplement %>
创建新的辅助元素
_form.html.erb

<%= form_for(@suplement) do |f| %>
    <% if @suplement.errors.any? %>
      <div id="error_explanation">
        <h2><%= pluralize(@suplement.errors.count, "error") %> prohibited this suplement from being saved:</h2>

        <ul>
        <% @suplement.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
        </ul>
      </div>
    <% end %>

    <div class="field">
      <%= f.label :name %>
      <%= f.text_field :name %>
    </div>

    <div class="field">
      <%= f.label :number_of_units %>
      <%= f.text_field :number_of_units %>
    </div>

    <div class="field">
      <%= f.label :daily_dosage_in_units %>
      <%= f.text_area :daily_dosage_in_units %>
    </div>

    <div class="field">
      <%= f.label :number_of_days %>
      <%= f.text_area :number_of_days %>
    </div>

    <div class="field">
      <%= f.label :suplement_cost %>
      <%= f.text_area :suplement_cost %>
    </div>
    <div class="actions">
      <%= f.submit %>
    </div>
  <% end %>

禁止保存此元素:
我所做的是:

1) 已删除new.html.erb内的
表单和提交按钮 2) 在_form.html.erb中添加了提交按钮,因此可以访问变量
f

此外,由于要将变量
@supelement
传递给局部变量
supelement
,因此可以在\u form.html.erb文件中使用变量
supelement
,而不使用
@
符号

编辑(关于评论):

获取用户状态验证错误,因为在Rails 5.0中,
属于
关联会自动验证是否存在

如果您的SupElement对象中不需要用户,则应将您的关联更改为
归属\u to:user,可选:true

如果您确实需要该用户,并且始终希望该用户是当前登录的用户,请将其添加到您的_表单中


这将使用Desive helper方法获取当前登录用户并将其分配给此隐藏字段。当你说“我点击时什么也没发生”时,别忘了在你的控制器
suplement_params
controller方法中添加这个参数,你的意思是你甚至没有看到POST请求击中服务器吗?是的,绝对没有。库利科夫斯基发现,这是双形式的新形式和u形式的领域。它做到了!但现在,当我创建新项目时,尽管我已登录,但仍收到一个错误“用户必须存在”。为什么会这样?更新是好的。在观看一些示例时,我想知道@suplement或只是suplement的形式,现在我明白了为什么两者都可以工作。谢谢@我很高兴这有帮助。请参阅我的答案的编辑版本以获取评论:)
<%= form_for(@suplement) do |f| %>
    <% if @suplement.errors.any? %>
      <div id="error_explanation">
        <h2><%= pluralize(@suplement.errors.count, "error") %> prohibited this suplement from being saved:</h2>

        <ul>
        <% @suplement.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
        </ul>
      </div>
    <% end %>

    <div class="field">
      <%= f.label :name %>
      <%= f.text_field :name %>
    </div>

    <div class="field">
      <%= f.label :number_of_units %>
      <%= f.text_field :number_of_units %>
    </div>

    <div class="field">
      <%= f.label :daily_dosage_in_units %>
      <%= f.text_area :daily_dosage_in_units %>
    </div>

    <div class="field">
      <%= f.label :number_of_days %>
      <%= f.text_area :number_of_days %>
    </div>

    <div class="field">
      <%= f.label :suplement_cost %>
      <%= f.text_area :suplement_cost %>
    </div>
    <div class="actions">
      <%= f.submit %>
    </div>
  <% end %>