Я пытаюсь настроить базовую форму с возможностью загрузки изображения, заголовка и текста. Я использую жемчужину paperclip 4.2.1
и rails 4.2.0
.paperclip gem upload not working in rails 4 multipart form
Форма отображается хорошо, я могу напечатать заголовок, текст тела и выбрать изображение. Однако, когда я отправляю форму, она пропускает метод show и возвращается на индексную страницу, и изображение не загружается в базу данных.
Когда я отправляю форму только с заголовком и текстом тела, она отображается в методе show. Я сделал проверить исходный код страницы формы показывает «ENCTYPE =» многослойную/форм-данных». Кто-нибудь знает, что им не хватает ??
schema.rb
ActiveRecord::Schema.define(version: 20150310181944) do
create_table "articles", force: :cascade do |t|
t.string "title"
t.text "text"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
end
конец
Модель -> article.rb
class Article < ActiveRecord::Base
has_attached_file :image
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end
View -> new.html.erb
Новая статья
<%= form_for @article, html: { multipart: true } do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.label :image %>
<%= f.file_field :image %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= link_to 'Back', articles_path %>
вид -> index.html.erb
<h1>Listing articles</h1>
<%= link_to 'New article', new_article_path %>
<table>
<tr>
<th>Title</th>
<th>Text</th>
<th>Image</th>
</tr>
<% @articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= image_tag article.image.url %></td>
</tr>
<% end %>
</table>
вид -> show.html.erb
<p>
<strong>Title:</strong>
<%= @article.title %>
</p>
<p>
<strong>Text:</strong>
<%= @article.text %>
</p>
<p>
<%= image_tag @article.image.url %>
</p>
<%= link_to 'Back', articles_path %>
Controller -> articles_controller.rb
class ArticlesController < ApplicationController
def index
@articles = Article.all
end
def show
@article = Article.find(params[:id])
end
def new
@article = Article.new
end
def create
@article = Article.create(article_params)
@article.save
redirect_to @article
end
private
def article_params
params.require(:article).permit(:title, :text, :image)
end
end
Я очень новичок в рубине и программировании вообще, так что, наверное, где-то, но я искал несколько дней, и я не уверен, почему изображение не загрузится.
может вы печатаете что-то в методе 'show', и можете ли вы опубликовать параметры, которые вы получаете в методе 'show', в контроллере – Sontya
im not sure how – amsterdamAL