2013-09-25 3 views
0

나는 직업을 찾고 있습니다. 사용자가 포스터를 만들 수있는 Rails 4 앱. 포스터에는 업로드 할 이미지가 여러 개있을 수 있습니다. 사실, 나는 명백한 오류가없고 단지 두 가지 질문이 있습니다. 하지만 질문하기 전에 여기에 내 파일이 있습니다. Poster.rb :여러 개의 클립이 첨부 된 클립에 관한 두 가지 질문

class Poster < ActiveRecord::Base 
    has_many :poster_images, dependent: :destroy 
    accepts_nested_attributes_for :poster_images, allow_destroy: true 
    belongs_to :type 
    belongs_to :user 
    default_scope -> { order('created_at DESC') } 

    validates :title, :body, :publish_date, :user_id, :presence => true 
end 

poster_image.rb :

class PosterImage < ActiveRecord::Base 
    belongs_to :poster 
    has_attached_file :image, :styles => {:medium => "300x300>", :thumb => "100x100>" } 
end 

posters_controller.rb :

class PostersController < ApplicationController 
    before_filter :set_poster, only: [:show, :edit, :update, :destroy] 
    before_filter :authenticate_user!, :except => [:index, :show] 
    authorize_resource 
    load_resource except: :create 
    # GET /posters 
    # GET /posters.json 
    def index 
    @posters = Poster.paginate(page: params[:page], :per_page => 10) 
    end 

    # GET /posters/1 
    # GET /posters/1.json 
    def show 
    end 

    # GET /posters/new 
    def new 
    end 

    # GET /posters/1/edit 
    def edit 

    end 

    # POST /posters 
    # POST /posters.json 
    def create 
    @poster = Poster.new(poster_params) 
    @poster.user_id = current_user.id 
    respond_to do |format| 
     if @poster.save 
     format.html { redirect_to @poster, notice: 'Poster was successfully created.' } 
     format.json { render action: 'show', status: :created, location: @poster } 
     else 
     format.html { render action: 'new' } 
     format.json { render json: @poster.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /posters/1 
    # PATCH/PUT /posters/1.json 
    def update 
    respond_to do |format| 
     if @poster.update(poster_params) 
     format.html { redirect_to @poster, notice: 'Poster was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: 'edit' } 
     format.json { render json: @poster.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /posters/1 
    # DELETE /posters/1.json 
    def destroy 
    @poster.destroy 
    respond_to do |format| 
     format.html { redirect_to posters_url } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_poster 
     @poster = Poster.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def poster_params 
     params.require(:poster).permit(:title, :body, :publish_date, :type_id, :type_name, poster_images_attributes: :image) 
    end 
    end 

_form.html.erb :

<%= simple_nested_form_for @poster, :html => {:multipart => true } do |f| %> 
    <%= f.input :title, :autofocus => true %> 
    <%= f.input :body %> 
    <%= f.input :publish_date %> 
    <%= f.input :type_id, :collection => Type.all, required:  true %> 


    <%= f.fields_for :poster_images do |images_f| %> 
    <%= images_f.file_field :image %> 
    <%= images_f.link_to_remove "X" %> 

    <% end %> 
    <%= f.link_to_add "Add image", :poster_images %> 


    <div class="form-actions"> 
    <%= f.button :submit, :class => 'btn-primary' %> 
    <%= link_to t('.cancel', :default => t("helpers.links.cancel")), 
      posters_path, :class => 'btn' %> 
    </div> 
<% end %> 

show.html. 예 :

<%- model_class = Poster -%> 
<div class="page-header"> 
    <h1><%=t '.title', :default => model_class.model_name.human.titleize %></h1> 
</div> 

<dl class="dl-horizontal"> 
    <dd><h3><%= @poster.title %></h3></dd> 

    <dd><%= @poster.body %></dd> 

    <dt><strong><%= model_class.human_attribute_name(:publish_date) %>:</strong></dt> 
    <dd><%= @poster.publish_date %></dd> 
    <dt><strong>Author:</strong></dt> 
    <dd><%= link_to @poster.user.name, @poster.user %></dd> 
    <dt><strong>Type:</strong></dt> 
    <dd><%= @poster.type.name %></dd> 
</dl> 

<dt><strong>Image(s):</strong></dt> 

<% @poster.poster_images.each do |p| %> 
<%= content_tag "p_#{p.id}" do %> 
<%= image_tag p.image.url(:medium) %> 
<% end %> 
<% end %> 



<div class="form-actions"> 
    <%= link_to t('.back', :default => t("helpers.links.back")), 
      posters_path, :class => 'btn' %> 

    <% if current_user && (current_user.has_role?(:admin) || [email protected]_id) %> 
     <% if can? :update, Poster %> 
    <%= link_to t('.edit', :default => t("helpers.links.edit")), 
      edit_poster_path(@poster), :class => 'btn' %> 
     <% end %> 
    <%= link_to t('.destroy', :default => t("helpers.links.destroy")), 
      poster_path(@poster), 
      :method => 'delete', 
      :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, 
      :class => 'btn btn-danger' %> 
     <% end %> 
</div> 

그리고 지금이 내 질문 :

  1. 내가 포스터를 만드는거야, 다 잘 간다. 그러나 포스터를 업데이트하고 "편집"을 누르려고 할 때 양식이 들어오고 포스터를 만들 때 가지고있는 모든 필드를 볼 수 있습니다. 그러나 포스터를 만드는 동안 그림을 추가하는 동안 편집 양식을 볼 때 그림을 추가하기위한 링크를 열었습니다. 그리고 그것을 사용하면 괜찮습니다.하지만 예를 들어 내 이름을 편집하고 새 사진을 추가하지 않고 저장하면 내 "show.html.erb"는 내 이전 사진과 "broken"이미지의 표시를 보여줍니다. 그것. 포스터를 업데이트하는 동안 두 개의 이미지가있는 경우 레일은 두 개의 이미지를 더 추가하거나 두 개의 이미지가 잘못 추가되도록합니다. 어떤 아이디어? (위에서 읽은 것을 이해했다면)

  2. "poster_params"컨트롤러의 "poster_images_attributes : : image"에 일부 키를 추가하는 것을 잊었습니다. SOF에서 여기서 충분하다는 것을 알았지 만 포스터를 만들면 충분하지만 편집 및 삭제 작업에는 충분하지 않다고합니다. poster_params에 무엇을 써야 하나? 내 질문에 모두 나에게 해결책을 준 무엇 감사

답변