ruby-on-rails Ruby on Rails:如何显示图片标题?(编辑)

kmbjn2e3  于 2022-11-19  发布在  Ruby
关注(0)|答案(2)|浏览(165)

我想在重定向到用户配置文件后在图片上方显示图片标题。我上传了一张带有标题的图片,但它只显示带有标签和用户的图片。我尝试在下面的show.html.haml中添加代码,但标题根本不显示。我做错了什么?
图片_控制器.rb

class PhotosController < ApplicationController
  def create
    @user = User.find(params[:user_id])
    if params[:photo][:title] == ""
      flash[:alert] = "Please enter an image title"
      redirect_to :back
    elsif params[:photo][:image] == nil
      flash[:alert] = "Please upload a photo"
      redirect_to :back
    else
      @photo = Photo.create(photo_params)
      @photo.user_id = @user.id
      @photo.save
      flash[:notice] = "Successfully uploaded a photo"
      redirect_to user_path(@user)
    end
  end

  def new
    @user = User.find(params[:user_id])
    @photo = Photo.create()
  end

  private
  def photo_params
    params.require(:photo).permit(:image, :title)
  end
end

photo.rb

class Photo<ActiveRecord::Base
  belongs_to :user
  has_many :tags
  
  has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }
  validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
  validates :title, :presence => true
end

20150318211456_照片_表格. rb

class PhotosTable < ActiveRecord::Migration
  def change
    create_table :photos do |photons|
      photons.column :user_id, :integer
      photons.column :title, :string
      photons.timestamps
    end
  end
end

new.html.haml

%h1 Add a Photo
= form_for Photo.new(), :url => user_photos_path, :html => {:multipart => true} do |form|
    = form.label :title
    = form.text_field :title
    %br
    %br
    = form.file_field :image
    %br
    %br
    %br
    = form.submit 'Upload', class: "btn btn-primary btn-lg"

show.html.haml

= link_to @user_photos_path do
    = @photo.title
    = image_tag @photo.image.url
    = image_tag @photo.image.url(:thumb)
h43kikqp

h43kikqp1#

您可以为:caption添加一个字段,就像对:image所做的那样:

class PhotosController < ApplicationController
  ...

  def new
    @user = User.find(params[:user_id])
    @photo = Photo.new() # don't create the photo yet
  end

  private
  def photo_params
    params.require(:photo).permit(:image, :caption) # allow caption field
  end
end

new.html.erb

<h1>Add a Photo</h1>
<%= form_for Photo.new(), :url => user_photos_path, :html => {:multipart => true} do |form| %>
  <%= form.text_field :caption %>
  <%= form.file_field :image %>
  <%= form.submit 'Upload'%>
<% end %>

show.html.erb

<p><%= @photo.caption %></p>
<%= image_tag @photo.image.url %>
<%= image_tag @photo.image.url(:thumb) %>
q1qsirdb

q1qsirdb2#

您的尝试很有创意,但实际上与如何在Rails中执行CRUD没有任何关系:
第一个
您混淆了用于初始化内存中新示例的Photo.new方法和将记录保存到数据库中的Photo.create方法-您不希望在控制器的new方法中执行此操作。
if params[:photo] == nil是无意义的。params.require(:photo)将引发ActionController::ParameterMissing异常是参数不存在。如果你想确保用户上传了照片或标题存在,你use validations in your model
您没有检查的是一件实际上很重要的事情-记录实际上是有效的,并保存在DB中。
使用form_with而不是旧的form_for,后者将最终被取消,并将表单绑定到您在控制器中创建的模型示例:

<h1>Add a Photo</h1>
<%= form_with model: [@user, @photo], html: { multipart: true} do |form| %>
  <div class="field">
    <%= form.label :image %>
    <%= form.file_field :image %>
  </div>
  <div class="field">
    <%= form.label :caption %>
    <%= form.text_field :caption %>
  </div>
  <div class="actions">
    <%= form.submit 'Upload'%>
  </div>
<% end %>

这样,当表单提交无效时,表单将保留用户输入。这就是为什么调用render :new而不是redirect_to :back的原因。如果客户端不发送HTTP_REFERRER头(换句话说,不使用它),后者实际上也不起作用。

相关问题