Rails 4强参数和嵌套形式

时间:2013-10-07 21:49:50

标签: ruby-on-rails ruby ruby-on-rails-4

我正在构建一个简单的应用程序(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

这是我的screens_controller:

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 %>

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

1 个答案:

答案 0 :(得分:7)

我最近经历过类似的事情,这似乎有用......

将您的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