Ruby on rails 未定义的方法'image';对于#<;位置0x..>;

Ruby on rails 未定义的方法'image';对于#<;位置0x..>;,ruby-on-rails,forms,paperclip,Ruby On Rails,Forms,Paperclip,我用的是回形针宝石。用户可以提交带有图像的位置。我不希望用户复制已提交的位置,但他们可以向其中添加图像 我一直在想办法。我将在下面留下重要代码: location.rb 看起来您正在尝试为记录集合创建表单标记,我认为这是行不通的。相反,我认为你需要一个类似这样的结构: <% @locations.each do |location| %> <%= form_for location, html: {multipart: true} do |f| %> . .

我用的是回形针宝石。用户可以提交带有图像的位置。我不希望用户复制已提交的位置,但他们可以向其中添加图像

我一直在想办法。我将在下面留下重要代码:

location.rb
看起来您正在尝试为记录集合创建表单标记,我认为这是行不通的。相反,我认为你需要一个类似这样的结构:

<% @locations.each do |location| %>
  <%= form_for location, html: {multipart: true} do |f| %>
  .
  .
  .
  <!--  User enters image-->
  <%= f.fields_for location.submissions.new do |s| %>
    <div class="form-group">
      <h3>Upload Image:</h3>
      <%= s.file_field :image, class: 'form-control' %>
    </div>
  <% end %>
<% end %>

.
.
.
上载图像:
用于的
表单和用于
字段都需要指向单个资源


旁注:,建议您改为使用Rails内部ActiveStorage。

看起来您正在尝试为记录集合创建表单标记,我认为这不起作用。相反,我认为你需要一个类似这样的结构:

<% @locations.each do |location| %>
  <%= form_for location, html: {multipart: true} do |f| %>
  .
  .
  .
  <!--  User enters image-->
  <%= f.fields_for location.submissions.new do |s| %>
    <div class="form-group">
      <h3>Upload Image:</h3>
      <%= s.file_field :image, class: 'form-control' %>
    </div>
  <% end %>
<% end %>

.
.
.
上载图像:
用于
表单和用于
字段都需要指向单个资源


旁注:,建议您改用Rails内部ActiveStorage。

因此我接受了您的建议,并将其转换为ActiveStorage。我想我的主要问题是,我有一种多对多的关系,这让我很反感。我想我可以添加一个图像,但我在显示它时遇到了问题。在我的show.html.erb中,对于我有“”的位置,我仍然得到与上面相同的错误。你知道我应该在这里做什么吗?所以这里要记住的是,你要处理的是一个位置数组,每个位置都有一个带有图像的提交数组。因此,您需要在位置上循环,然后在每个位置的提交上循环,然后为每个位置渲染一个图像标记。类似于
。所以我接受了您的建议,并转换为ActiveStorage。我想我的主要问题是,我有一种多对多的关系,这让我很反感。我想我可以添加一个图像,但我在显示它时遇到了问题。在我的show.html.erb中,对于我有“”的位置,我仍然得到与上面相同的错误。你知道我应该在这里做什么吗?所以这里要记住的是,你要处理的是一个位置数组,每个位置都有一个带有图像的提交数组。因此,您需要在位置上循环,然后在每个位置的提交上循环,然后为每个位置渲染一个图像标记。类似于
class LocationsController < ApplicationController

  # Before actions get routed and ran, find_location will occur
  before_action :find_location, only: [:show, :edit, :update, :destroy]


  # For the Locations/index.html.erb
  def index
    @locations = Location.all
  end

  # Binds submission object to the form in new.html.erb
  def new
    @locations = Location.new
    @locations.submissions.build
  end

  # For creating a new location in the new.html.erb form
  def create
    @locations = Location.new(user_params)
    # Everything went well. User will be sent to @locations show page
    if  @locations.save
      # Redirects to user submitted locations
      redirect_to @locations
    else
      render 'new'
    end
  end

  # Finds new location user submitted by its unique id
  def show
  end

  # Allowing user to update their submitted location
  def edit
  end

  # Updates users edited submission
  def update
    if @locations.update(user_params)
      # Redirects to user submitted locations
      redirect_to @locations
    else
      render 'edit'
    end
  end

  # Deletes users submission
  def destroy
    @locations.destroy
    # Redirects to user submitted locations
    redirect_to @locations
  end

  private

  # Used for finding user submitted location (Prevents DRY)
  def find_location
    @locations = Location.find(params[:id])
  end

  # Strong parameters for security - Defines what can be update/created in location model
  def user_params
    params.require(:location).permit(:city, :state, :submissions_attributes => [:image])
  end
end
<%= form_for @locations, html: {multipart: true} do |f| %>
.
.
.
<!--  User enters image-->
  <%= f.fields_for :submissions do |s| %>
  <div class="form-group">
    <h3>Upload Image:</h3>
    <%= s.file_field :image, class: 'form-control' %>
  </div>
    <% end %>
<% end %>
<h3>Image: <%= image_tag @locations.image.url(:medium) %></h3>
"undefined method `image' for.."
<% @locations.each do |location| %>
  <%= form_for location, html: {multipart: true} do |f| %>
  .
  .
  .
  <!--  User enters image-->
  <%= f.fields_for location.submissions.new do |s| %>
    <div class="form-group">
      <h3>Upload Image:</h3>
      <%= s.file_field :image, class: 'form-control' %>
    </div>
  <% end %>
<% end %>