gpt4 book ai didi

ruby-on-rails - Rails 4 强大的参数和嵌套形式

转载 作者:数据小太阳 更新时间:2023-10-29 07:48:09 24 4
gpt4 key购买 nike

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

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

最佳答案

我最近完成了类似的工作,这似乎很有效...

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

此外,您需要更新您的 操作,使屏幕截图成为复数形式,如下所示:

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

关于ruby-on-rails - Rails 4 强大的参数和嵌套形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19235410/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com