Rails - 我的“创建/新建”方法不起作用

如何解决Rails - 我的“创建/新建”方法不起作用

我创建了一个表单,以便用户可以将新图片添加到网站目录中,但由于某些原因它不起作用。填写完表格后,我点击“提交”按钮,但没有任何反应,我只是停留在同一页面上(并且我没有任何错误消息......)。 我不认为错误来自我的控制器,因为当我在控制器的“创建”方法添加 raise 关键字时没有任何反应。

我确定这是一个业余错误……但我看不出它是什么。感谢您的帮助!

图片.rb / 图片模型:

class Picture < ApplicationRecord
  validates :name,presence: true
  has_many_attached :photo
end

new.html.erb / 这是我的表格:

<%= form_for(@picture) do |f| %>
  <div class="form-group">
    <%= f.label :name,"Please indicate the name" %>
    <%= f.text_field :name,class:"form-control",placeholder:"(mandatory field)" %>
  </div>
  <div class="form-group">
    <%= f.label :description,"Add a description" %>
    <%= f.text_area :description,class:"form-control" %>
  </div>
  <div class="form-group">
    <div class="row">
      <div class="col">
        <%= f.label :category,"Add a category" %>
        <%= f.text_field :category,class:"form-control" %>
      </div>
      <div class="col">
        <%= f.label :price,"Indicate the price (when applicable)" %>
        <%= f.number_field :price,placeholder:"0,00$ CAD" %>
      </div>
    </div>
  </div>
  <div class="form-group">
    <div class="row">
      <div class="col">
        <p>Where do you want to add this item?</p>
          <div>
            <%= f.check_Box :is_home_item %>
            <%= f.label :is_home_item,"Homepage" %>
          </div>
          <div>
            <%= f.check_Box :is_portfolio_item %>
            <%= f.label :is_portfolio_item,"Work" %>
          </div>
          <div>
            <%= f.check_Box :is_sketchbook_item %>
            <%= f.label :is_sketchbook_item,"Sketchbook" %>
          </div>
        <div>
          <%= f.check_Box :is_shopping_item %>
          <%= f.label :is_shopping_item,"Shopping" %>
        </div>
      </div>
      <div class="col">
        <div class="form-group">
          <%= f.label :photo,"Select your picture" %>
          <%= f.file_field :photo,class:"form-control-file" %>
        </div>
      </div>
    </div>
  </div>
  <div class="form-group">
    <%= f.submit class:"btn btn-lg btn-primary" %>
  </div>
<% end %>

pictures_controller.rb /

class PicturesController < ApplicationController
  def new
    @picture = Picture.new
  end

  def create
    @picture = Picture.new(picture_params)
    if @picture.save
      redirect_to root_path(@picture),notice: "Picture was successfully created"
    else
      render :new
    end
  end

  private

  def picture_params
    params.require(:picture).permit(:name,:description,:category,:price,:is_home_item,:is_portfolio_item,:is_sketchbook_item,:is_shopping_item,:photo)
  end
end

And here is the text I got on my local server console

这是我的表单的 HTML 输出

<form>
<input type="hidden" name="authenticity_token" value="7MxWbzbltjXcAx2dvgRwIu07WMpyjPQji6LI6ELifMZODLJyMBucNApnPRk8vsjshSEjyMMenDUfvDw6pN+/4Q==">
  <div class="form-group">
    <label for="picture_name">Indicate the name</label>
    <input class="form-control" placeholder="(mandatory field)" type="text" name="picture[name]" id="picture_name">
  </div>
  <div class="form-group">
    <label for="picture_description">Add a description</label>
    <textarea class="form-control" name="picture[description]" id="picture_description"></textarea>
  </div>
  <div class="form-group">
    <div class="row">
      <div class="col">
        <label for="picture_category">Add a category</label>
        <input class="form-control" type="text" name="picture[category]" id="picture_category">
      </div>
      <div class="col">
        <label for="picture_price">Indicate the price (when applicable)</label>
        <input class="form-control" placeholder="0,00$ CAD" type="number" name="picture[price]" id="picture_price">
      </div>
    </div>
  </div>
  <div class="form-group">
    <div class="row">
      <div class="col">
        <p>Where do you want to add your picture?</p>
          <div>
            <input name="picture[is_home_item]" type="hidden" value="0"><input type="checkBox" value="1" name="picture[is_home_item]" id="picture_is_home_item">
            <label for="picture_is_home_item">Homepage</label>
          </div>
          <div>
            <input name="picture[is_portfolio_item]" type="hidden" value="0"><input type="checkBox" value="1" name="picture[is_portfolio_item]" id="picture_is_portfolio_item">
            <label for="picture_is_portfolio_item">Work</label>
          </div>
          <div>
            <input name="picture[is_sketchbook_item]" type="hidden" value="0"><input type="checkBox" value="1" name="picture[is_sketchbook_item]" id="picture_is_sketchbook_item">
            <label for="picture_is_sketchbook_item">Sketchbook</label>
          </div>
        <div>
          <input name="picture[is_shopping_item]" type="hidden" value="0"><input type="checkBox" value="1" name="picture[is_shopping_item]" id="picture_is_shopping_item">
          <label for="picture_is_shopping_item">Shopping</label>
        </div>
      </div>
      <div class="col">
        <div class="form-group">
          <label for="picture_photo">Select your picture</label>
          <input class="form-control-file" type="file" name="picture[photo]" id="picture_photo">
        </div>
      </div>
    </div>
  </div>
  <div class="form-group">
    <input type="submit" name="commit" value="Create Picture" class="btn btn-lg btn-primary" data-disable-with="Create Picture">
  </div>

我的路线:

Rails.application.routes.draw do
  devise_for :users
  
  # Pages routes
  root to: 'pages#home'
  get 'about',to: 'pages#about'
  get 'portfolio',to: 'pages#portfolio'
  get 'sketchbook',to: 'pages#sketchbook'
  get 'shopping',to: 'pages#shopping'
  
  # Pictures routes
  resources :pictures

end

And these are my routes in the Terminal

解决方法

不太清楚为什么会发生这种情况,但不推荐使用 java.sql.SQLType,因此您应该将其替换为 form_for

您可以使用模型,例如 form_with 和 form_with,如the docs 中所述。

@picture

这应该创建一个如下所示的表单 DOM 对象:

<%= form_with(model: @picture) do |form| %>
  # fields
<% end %>

如果由于某种原因没有,你总是可以在 form_with 中传递一个 <form action="/pictures" method="post" accept-charset="UTF-8" > <input name="authenticity_token" type="hidden" value="..." /> ... </form> (即 method: :post)。

,

目前,您得到了带有单数 has_many_attachedphoto。这似乎不是正确的语法。您是否尝试更改为 has_many_attached :photos 并修改 picture_params 以将照片作为数组 photos: [] 传递?

或者如果您想附加一张照片,那么您应该尝试在图片模型中更改为 has_one_attached :photo

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?