2015-07-14 2 views
0

У меня есть форма, которая в настоящее время сохраняет заголовок, описание и URL изображения, которые загружаются в мой проект. Я добавил параметр загрузки в форму, и теперь я смущен тем, как именно он может сэкономить на моем сервере и появится после загрузки. Также - и это не приоритет. Есть ли способ назначить мою загрузку URL-адреса, чтобы пользователи могли делиться?Как загрузить изображение для работы с рельсами?

Моя форма: (f.file_field картина, что я использую, чтобы загрузить изображение)

<h1>Add a picture</h1> 
<%= link_to "Back to Pictures", pictures_url %> 
<%= form_for @picture do |f| %> 
    <p> 
    <%= f.label :artist %><br> 
    <%= f.text_field :artist %> 
    </p> 
    <p> 
    <%= f.label :title %><br> 
    <%= f.text_field :title %> 
    </p> 
    <p> 
    <%= f.file_field :picture %> 
    </p> 
    <p> 
    <%= f.label :url %><br> 
    <%= f.text_field :url %> 
    </p> 
    <p> 
    <%= f.submit "Save" %> 
    </p> 
<% end %> 

</center> 

Контроллер:

class PicturesController < ApplicationController 

    def index 
    @pictures = Picture.all 
    end 

    def new 
    @picture = Picture.new 
    end 

    def create 
    # make a new picture with what picture_params returns (which is a method we're calling) 
    @picture = Picture.new(picture_params) 
    if @picture.save 
     # if the save for the picture was successful, go to index.html.erb 
     redirect_to pictures_url 
    else 
     # otherwise render the view associated with the action :new (i.e. new.html.erb) 
     render :new 
    end 
    end 

    def show 
    @picture = Picture.find(params[:id]) 
    end 

    def edit 
    @picture = Picture.find(params[:id]) 
    end 

    def update 
    @picture = Picture.find(params[:id]) 

    if @picture.update_attributes(picture_params) 
     redirect_to "/pictures/#{@picture.id}" 
    else 
     render :edit 
    end 
    end 

    def destroy 
    @picture = Picture.find(params[:id]) 
    @picture.destroy 
    redirect_to pictures_url 
    end 

    private 
    def picture_params 
    params.require(:picture).permit(:artist, :title, :url) 
    end 

end 

Спасибо за вашу помощь! Я хочу сохранить это как можно меньше и не хочу выбирать драгоценный камень.

ответ

0

Ваш :picture не разрешается в вашем picture_params. Разрешение на это, чтобы сохранить в DB

def picture_params 
    params.require(:picture).permit(:artist, :title, :url, :picture) 
end 
+0

спасибо! назначить ли его t.string или? – icecreamrabbit

+0

@icecreamrabbit Вы имеете в виду миграцию? – Pavan

+0

Да, я полагаю, когда вы говорите, что вы имеете в виду мою схему? – icecreamrabbit

Смежные вопросы