Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails ActiveModel::MassAssignmentSecurity::VideosController#创建时出错_Ruby On Rails_Ruby_Sqlite - Fatal编程技术网

Ruby on rails ActiveModel::MassAssignmentSecurity::VideosController#创建时出错

Ruby on rails ActiveModel::MassAssignmentSecurity::VideosController#创建时出错,ruby-on-rails,ruby,sqlite,Ruby On Rails,Ruby,Sqlite,我在rubymine和rails中遇到以下错误,我对环境完全陌生,不知道如何修复它: ActiveModel::MassAssignmentSecurity::VideosController#创建时出错 我使用的是Rails 3.2.15和ruby 1.9.3p392(2013-02-22)[i386-mingw32] 我正在“尝试”使用上述技术构建一个web应用程序。这是一个基本的视频租赁商店。其中我(目前)有4张桌子。用户、视频、租赁、类型。稍后,我将使用Desive添加购物车和登录,以及

我在rubymine和rails中遇到以下错误,我对环境完全陌生,不知道如何修复它:

ActiveModel::MassAssignmentSecurity::VideosController#创建时出错

我使用的是Rails 3.2.15和ruby 1.9.3p392(2013-02-22)[i386-mingw32]

我正在“尝试”使用上述技术构建一个web应用程序。这是一个基本的视频租赁商店。其中我(目前)有4张桌子。用户、视频、租赁、类型。稍后,我将使用Desive添加购物车和登录,以及可能需要附加表的基本自定义gem

当我尝试输入新视频或租赁时,会出现此错误。我在输入流派或用户方面没有问题。所以我假设这与关系有关,因为当我删除它们时,它会起作用

我在rubymine中使用了以下脚手架命令来创建我的4个表

user first_name:string{25} last_name:string{25} user_name:string{25} password:string{25} address:string email:string{150} phone:string{15} dob:date verified:boolean admin:boolean

#I added the following to the resulting user.rb model file
has_many :rentals

#Below is the code generated (and slightly modified) for the user.rb model file
class User < ActiveRecord::Base
    has_many :rentals
    attr_accessible :address, :admin, :dob, :email, :first_name, :last_name, :password, :phone, :user_name, :verified
end


genre genre_title:string{50}

#I added the following to the resulting genre.rb model file
has_many :videos

#Below is the code generated (and slightly modified) for the genre.rb model file
class Genre < ActiveRecord::Base
    has_many :videos
    attr_accessible :genre_title
end



video title:string{50} genre_title:string{50} description:text purchase_price:decimal rental_price:decimal sale_price:decimal image_url:string genre:references

#I added the following to the resulting video.rb model file
has_many :rentals
belongs_to :genre

#Below is the code generated (and slightly modified) for the video.rb model file
class Video < ActiveRecord::Base
    has_many :rentals
    belongs_to :genre
    attr_accessible :description, :genre_title, :image_url, :purchase_price, :rental_price, :sale_price, :title
end



rental user:references video:references borrowed_date:datetime returned_date:datetime due_date:datetime

#I added the following to the resulting rental.rb model file
belongs_to :user
belongs_to :video

#Below is the code generated (and slightly modified) for the rental.rb model file
class Rental < ActiveRecord::Base
  belongs_to :user
  belongs_to :video
  attr_accessible :borrowed_date, :due_date, :returned_date
end
user first_name:string{25}last_name:string{25}user_name:string{25}密码:string{25}地址:string电子邮件:string{150}电话:string{15}dob:date verified:boolean管理员:boolean
#我在生成的user.rb模型文件中添加了以下内容
有很多:租金
#下面是为user.rb模型文件生成(并稍微修改)的代码
类用户
这确实生成了适当的attr_可访问行,我稍微修改了这些行,因为遗漏了几个字段,我认为这可以解决问题

这可能会设置表和关系。如我所愿。我这么说可能是因为当我进入SQLite3并输入破坏关系完整性的数据时(例如,使用无效用户或视频id的租用)。这是允许的。因此,错误不会从数据库返回(即使它应该返回)

我通常在SQL Server上使用C#和实体框架……相反,我尝试学习RubyonRails和ActiveRecord以及SQLite3。到目前为止,这是一个缓慢而痛苦的过程,但我真的想给这个平台一个很好的机会,因为我听到了很多关于它的好消息

下面是我被要求添加的进一步代码。我希望这对你有帮助,谢谢你

  #This is the create action from the video controller
  # POST /videos
  # POST /videos.json
  def create
    @video = Video.new(params[:video])

    respond_to do |format|
      if @video.save
        format.html { redirect_to @video, notice: 'Video was successfully created.' }
        format.json { render json: @video, status: :created, location: @video }
      else
        format.html { render action: "new" }
        format.json { render json: @video.errors, status: :unprocessable_entity }
      end
    end
  end



<!-- This is the _form.html.erb video view -->
<%= form_for(@video) do |f| %>
  <% if @video.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@video.errors.count, "error") %> prohibited this video from being saved:</h2>

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

  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :genre_title %><br />
    <%= f.text_field :genre_title %>
  </div>
  <div class="field">
    <%= f.label :description %><br />
    <%= f.text_area :description %>
  </div>
  <div class="field">
    <%= f.label :purchase_price %><br />
    <%= f.text_field :purchase_price %>
  </div>
  <div class="field">
    <%= f.label :rental_price %><br />
    <%= f.text_field :rental_price %>
  </div>
  <div class="field">
    <%= f.label :sale_price %><br />
    <%= f.text_field :sale_price %>
  </div>
  <div class="field">
    <%= f.label :image_url %><br />
    <%= f.text_field :image_url %>
  </div>
  <div class="field">
    <%= f.label :genre %><br />
    <%= f.text_field :genre %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>



<!-- This is the new.html.erb video view -->
<h1>New video</h1>

<%= render 'form' %>

<%= link_to 'Back', videos_path %>



<!-- This is the index.html.erb video view -->
<h1>Listing videos</h1>

<table>
  <tr>
    <th>Title</th>
    <th>Genre title</th>
    <th>Description</th>
    <th>Purchase price</th>
    <th>Rental price</th>
    <th>Sale price</th>
    <th>Image url</th>
    <th>Genre</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @videos.each do |video| %>
  <tr>
    <td><%= video.title %></td>
    <td><%= video.genre_title %></td>
    <td><%= video.description %></td>
    <td><%= video.purchase_price %></td>
    <td><%= video.rental_price %></td>
    <td><%= video.sale_price %></td>
    <td><%= video.image_url %></td>
    <td><%= video.genre %></td>
    <td><%= link_to 'Show', video %></td>
    <td><%= link_to 'Edit', edit_video_path(video) %></td>
    <td><%= link_to 'Destroy', video, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
<% end %>
</table>

<br />

<%= link_to 'New Video', new_video_path %>
#这是视频控制器的创建操作
#帖子/视频
#POST/videos.json
def创建
@视频=视频。新建(参数[:视频])
回应待办事项|格式|
如果@video.save
format.html{将_重定向到@video,注意:'视频已成功创建。}
format.json{render json:@video,status::created,location:@video}
其他的
format.html{呈现操作:“新建”}
format.json{render json:@video.errors,status::unprocessable_entity}
结束
结束
结束
禁止保存此视频:








新视频 视频列表 标题 体裁标题 描述 购买价格 租金 销售价格 图像url 体裁

感谢您的帮助。

在您的
\u表单中,您包含了一个
类型的字段,该字段在您的模型中不是可访问的
属性。您应该将其更改为
genre\u id

最好的表示方法是使用
选择
选项,标签显示
流派_标题
,值为
流派id

<%= f.select :genre_id, Genre.all.map{ |g| [g.genre_title, g.id] } %>


您使用的是Rails 3还是Rails 4?您的模型中是否包含任何可访问的
attr\u
?您是否可以显示
视频
控制器
模型
?请显示两个位置:
视频
模型堆和错误上升的位置。@FrancisRodgers,问题是,当您创建一个新的
视频
时,您正在传递一个
参数,而该视频不在
属性可访问的
中。这将提高
消息分配
。如果看不到你正在通过什么,就很难说出问题出在哪里。你应该包括你的
new
表单和你的
create
操作,从
video\u controller
@FrancisRodgers,我应该更清楚一点。我所说的
新的
表单,是指
\u表单
部分<代码>呈现“表单”
对我们没有帮助。理想情况下,我希望在创建/更新时在下拉列表框中显示流派标题。A.