3

我正在构建一个简单的应用程序(Ruby 2.0.0 和 Rails 4),用户可以在其中创建一个项目,并为每个项目创建多个屏幕。创建屏幕时,用户可以上传屏幕截图,该屏幕截图引用其自己的模型(我这样做是为了处理同一屏幕的多个版本)。

创建屏幕时,由于权限问题,似乎没有创建屏幕截图。这是服务器日志:

Processing by ScreensController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"kezaGADsaLmY/+zozgbjEe5UfdeqRPg58FCf1qzfHxY=", "screen"=>{"project_id"=>"24", "name"=>"Billing", "description"=>"This is the page where a user will enter their credit card information", "screenshot"=>{"image"=>#  <ActionDispatch::Http::UploadedFile:0x007fcdce25b2c0 @tempfile=#<Tempfile:/var/folders/pv/srwrv0qj35b0hsxkt42l_z500000gn/T/RackMultipart20131007-91790-tewse9>, @original_filename="fb-banner.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"screen[screenshot][image]\"; filename=\"fb-banner.png\"\r\nContent-Type: image/png\r\n">}}, "commit"=>"Create Screen"}
Unpermitted parameters: screenshot

这些是我的模型:

屏幕

class Screen < ActiveRecord::Base

  belongs_to :project
  has_many :screenshots

  validates :name, presence: true

  accepts_nested_attributes_for :screenshots

end

截屏

class Screenshot < ActiveRecord::Base

  belongs_to :screen

end

这是我的屏幕控制器:

class ScreensController < ApplicationController
  before_action :set_screen, only: [:show, :edit, :update, :destroy]


  def index
    @screens = Screen.all
  end


  def show
  end


  def new
    @screen = Screen.new(:project_id => params[:project_id])
    @screen.screenshot.build
  end


  def edit
  end


  def create
    @screen = Screen.create(screen_params)
    if @screen.save
      flash[:notice] = "A new screen has been added to this project"
      redirect_to [@screen.project]
    else
      render :action => 'new'
    end
  end


  def update
    @screen = Screen.find(params[:id])
    if @screen.update_attributes(screen_params)
      flash[:notice] = "The screen has been successfully updated"
      redirect_to [@screen.project]
    else
      render :action => 'edit'
    end
  end

  def destroy
    @screen = Screen.find(params[:id])
    @screen.destroy
    flash[:notice] = "Successfully destroyed screen"
    redirect_to [@screen.project]
  end

  private
    def set_screen
      @screen = Screen.find(params[:id])
    end

    def screen_params
      params.require(:screen).permit(:project_id, :name, :description, screenshot_attributes: [ :id, :screen_id, :image, :version ])
    end
end

最后是表格:

<%= form_for @screen, :html => { :multipart => true } do |f| %>
  <% if @screen.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@screen.errors.count, "error") %> prohibited this screen from being saved:</h2>

      <ul>
      <% @screen.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.hidden_field :project_id %>
  </div>
  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :description %><br>
    <%= f.text_field :description %>
  </div>

  <%= f.fields_for :screenshot do |s| %>
    <%= s.hidden_field :screen_id, :value => @screen.id %>
    <%= s.hidden_field :version, :value => "1" %>
    <%= s.label :image %><br>
    <%= s.file_field :image %>
  <% end %>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

我希望这足以帮助我发现问题。在编程方面,我是新手,所以任何帮助都非常受欢迎。

4

1 回答 1

8

我最近经历了类似的事情,这似乎奏效了......

将您的 fields_for 更改为复数:

<%= f.fields_for :screenshots do |s| %>

而且,让你的参数

def screen_params
  params.require(:screen).permit(:project_id, :name, :description, screenshots_attributes: [ :id, :screen_id, :image, :version ])
end

此外,您需要更新您的new操作以使屏幕截图复数,如下所示:

def new
  @screen = Screen.new(:project_id => params[:project_id])
  @screen.screenshots.build
end
于 2013-10-07T22:03:28.883 回答