【问题标题】:Rails - My 'create/new' method doesn't workRails - 我的“创建/新建”方法不起作用
【发布时间】:2021-02-19 04:00:17
【问题描述】:

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

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

picture.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, class:"form-control", 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

【问题讨论】:

  • &lt;%= form_for(@picture) do |f| %&gt; 更改为&lt;%= form_for(@picture, local: true) do |f| %&gt;
  • 感谢@sam,但不幸的是它不起作用:/
  • 嗯...您可能希望将 form_for 更改为 form_with(model: @picture。如果这不起作用,当您单击提交时,控制台中会显示什么内容?
  • 它仍然不起作用:/我在原始消息的末尾添加了我在本地服务器控制台上获得的文本的屏幕截图
  • 好的。所以它正在向图片#new 发出 GET 请求。您需要它来向图片#create 发出 PUT 请求。我现在的猜测是正在发生两件事之一。您的路线配置错误(它们应该是resources :pictures)或者由于某种原因您的 form_with 没有默认为具有正确操作的 post 方法。您可以添加表单的 html 输出吗?您可以去掉真实性令牌部分,只是以&lt;form 开头的行。还有你的路线图。

标签: ruby-on-rails model-view-controller crud form-with


【解决方案1】:

不太清楚为什么会发生这种情况,但 form_for 已被弃用,因此您应该将其替换为 form_with

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

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

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

<form action="/pictures" method="post" accept-charset="UTF-8" >
  <input name="authenticity_token" type="hidden" value="..." />
  ...
</form>

如果由于某种原因没有,您始终可以在 form_with 中传递一个method: :post(即&lt;%= form_with(model: @picture, method: :post) do |form| %&gt;)。

【讨论】:

  • 再次感谢山姆的帮助!其实我已经试过了,但它不起作用。您认为这可能是数据库问题(而不是编码问题)吗?可能与 ActiveRecords 或 Active Storage 有关?我想知道我是否正确设置了我的数据库...
  • 我不这么认为。现在你的问题是 GET 请求。你需要解决那部分。不过,您可以尝试删除照片并查看是否可以保存名称。同样在控制台中运行Picture.create!(name: 'Test Picture') 以确保没有数据库问题。
  • 我在控制台中尝试过,它成功了。所以你是对的,问题出在其他地方。正如 Ayushi 在她的评论中提到的,问题出在我的图片模型上。我更新了它和我的picture_params 方法,它现在可以工作了。感谢您的所有帮助!
【解决方案2】:

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

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

【讨论】:

  • 成功了!我用has_many_attached :photos 更新了图片模型,还修改了picture_params 以将照片作为数组photos: [] 传递,它似乎正在工作。非常感谢您的帮助 Ayushi :)
猜你喜欢
  • 1970-01-01
  • 2011-05-17
  • 1970-01-01
  • 2014-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-30
相关资源
最近更新 更多