admin管理员组文章数量:1122832
_pin.html '''
<% @pin.images.each do |image| %>
<div id="<%= dom_id pin %>">
<div class="carousel-cell">
<div class="panel.panel-default">
<div class="panel-heading.pin_image">
<p>
<%= image_tag (pin.image.variant(resize_to_limit: [500, 500])) %>
</p>
</div>
<div class="panel-body">
</div>
<div class="panel-footer">
</div>
</div>
</div>
</div>
<% end %>
'''
index.html '''
<div id="pins">
<div class="carousel" data-flickity='{ "cellAlign": "left", "contain": true }'>
<% @pins.each do |pin| %>
<%= render pin %>
<p>
<%= link_to "Show this pin", pin %>
</p>
<% end %>
''' pin.rb '''
class Pin < ApplicationRecord
has_many_attached :images
end
''' form '''
<%= form_with(model: pin) do |form| %>
<% if pin.errors.any? %>
<div style="color: red">
<h2><%= pluralize(pin.errors.count, "error") %> prohibited this pin from being saved:</h2>
<ul>
<% pin.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div>
<%= form.label :title, style: "display: block" %>
<%= form.text_field :title %>
</div>
<div>
<%= form.label :description, style: "display: block" %>
<%= form.textarea :description %>
</div>
<div>
<%= form.file_field :photos, :multiple => true %></br>
</div>
<div>
<%= form.submit %>
</div>
<% end %>
'''
keep getting stuck with image uploads can someone help me fix quick? trying to set up some js and a pins kinda thing. fought for 2 days to get this for can i get a quick hand? will poost further questions here. ..............................................................................................................................................................................................................................................................................
edit
pins controller '''
class PinsController < ApplicationController
before_action :set_pin, only: %i[ show edit update destroy ]
# GET /pins or /pins.json
def index
@pins = Pin.all
end
# GET /pins/1 or /pins/1.json
def show
end
# GET /pins/new
def new
@pin = Pin.new
end
# GET /pins/1/edit
def edit
end
# POST /pins or /pins.json
def create
@pin = Pin.new(pin_params)
images.each do |image|
@pin.images.attach(image)
end
respond_to do |format|
if @pin.save
format.html { redirect_to @pin, notice: "Pin was successfully created." }
format.json { render :show, status: :created, location: @pin }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @pin.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /pins/1 or /pins/1.json
def update
respond_to do |format|
if @pin.update(pin_params)
format.html { redirect_to @pin, notice: "Pin was successfully updated." }
format.json { render :show, status: :ok, location: @pin }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @pin.errors, status: :unprocessable_entity }
end
end
end
# DELETE /pins/1 or /pins/1.json
def destroy
@pin.destroy!
respond_to do |format|
format.html { redirect_to pins_path, status: :see_other, notice: "Pin was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_pin
@pin = Pin.find(params.expect(:id))
end
# Only allow a list of trusted parameters through.
def pin_params
params.require(:pin).permit( :images => [])
end
end
'''
_pin.html '''
<% @pin.images.each do |image| %>
<div id="<%= dom_id pin %>">
<div class="carousel-cell">
<div class="panel.panel-default">
<div class="panel-heading.pin_image">
<p>
<%= image_tag (pin.image.variant(resize_to_limit: [500, 500])) %>
</p>
</div>
<div class="panel-body">
</div>
<div class="panel-footer">
</div>
</div>
</div>
</div>
<% end %>
'''
index.html '''
<div id="pins">
<div class="carousel" data-flickity='{ "cellAlign": "left", "contain": true }'>
<% @pins.each do |pin| %>
<%= render pin %>
<p>
<%= link_to "Show this pin", pin %>
</p>
<% end %>
''' pin.rb '''
class Pin < ApplicationRecord
has_many_attached :images
end
''' form '''
<%= form_with(model: pin) do |form| %>
<% if pin.errors.any? %>
<div style="color: red">
<h2><%= pluralize(pin.errors.count, "error") %> prohibited this pin from being saved:</h2>
<ul>
<% pin.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div>
<%= form.label :title, style: "display: block" %>
<%= form.text_field :title %>
</div>
<div>
<%= form.label :description, style: "display: block" %>
<%= form.textarea :description %>
</div>
<div>
<%= form.file_field :photos, :multiple => true %></br>
</div>
<div>
<%= form.submit %>
</div>
<% end %>
'''
keep getting stuck with image uploads can someone help me fix quick? trying to set up some js and a pins kinda thing. fought for 2 days to get this for can i get a quick hand? will poost further questions here. ..............................................................................................................................................................................................................................................................................
edit
pins controller '''
class PinsController < ApplicationController
before_action :set_pin, only: %i[ show edit update destroy ]
# GET /pins or /pins.json
def index
@pins = Pin.all
end
# GET /pins/1 or /pins/1.json
def show
end
# GET /pins/new
def new
@pin = Pin.new
end
# GET /pins/1/edit
def edit
end
# POST /pins or /pins.json
def create
@pin = Pin.new(pin_params)
images.each do |image|
@pin.images.attach(image)
end
respond_to do |format|
if @pin.save
format.html { redirect_to @pin, notice: "Pin was successfully created." }
format.json { render :show, status: :created, location: @pin }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @pin.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /pins/1 or /pins/1.json
def update
respond_to do |format|
if @pin.update(pin_params)
format.html { redirect_to @pin, notice: "Pin was successfully updated." }
format.json { render :show, status: :ok, location: @pin }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @pin.errors, status: :unprocessable_entity }
end
end
end
# DELETE /pins/1 or /pins/1.json
def destroy
@pin.destroy!
respond_to do |format|
format.html { redirect_to pins_path, status: :see_other, notice: "Pin was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_pin
@pin = Pin.find(params.expect(:id))
end
# Only allow a list of trusted parameters through.
def pin_params
params.require(:pin).permit( :images => [])
end
end
'''
Share Improve this question edited Nov 21, 2024 at 13:50 Brendhann Prior asked Nov 21, 2024 at 12:49 Brendhann PriorBrendhann Prior 11 bronze badge 3 |1 Answer
Reset to default 2In "_pin.html.erb" you need to change this:
@pin.images.each do |image|
to
pin.images.each
@pin
is an unassigned instance variable (which will cause this call to be nil.images
resulting in the error you are reporting). pin
on the other hand is the implicit local variable set when you call render pin
(assuming that pin
is a Pin
object).
Additionally render can accept an entire collection so you could clean this up a bit more using:
index.html.erb
<div id="pins">
<div class="carousel" data-flickity='{ "cellAlign": "left", "contain": true }'>
<%= render @pins %>
</div>
</div>
_pin.html.erb
<% pin.images.each do |image| %>
<div id="<%= dom_id pin %>">
<div class="carousel-cell">
<div class="panel.panel-default">
<div class="panel-heading.pin_image">
<p><%= image_tag(pin.image.variant(resize_to_limit: [500, 500])) %></p>
</div>
<div class="panel-body"></div>
<div class="panel-footer"></div>
</div>
</div>
</div>
<% end %>
<p><%= link_to "Show this pin", pin %></p>
Your form has issues too, which are technically outside the scope of this question but:
<%= form.file_field :photos, :multiple => true %></br>
I assume this is supposed to be the "images" but I cannot say for sure and your pin_params
in the controller do not whitelist :title
or :description
coming from the form.
本文标签: rubyundefined method images39 for nil rails activestorageStack Overflow
版权声明:本文标题:ruby - undefined method `images' for nil rails active_storage - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736310786a1934503.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
image_tag (...)
is actually doing isimage_tag((...))
so if you add more than one argument it will cause a syntax error. – max Commented Nov 22, 2024 at 10:51