我正在使用Rails 4,并且我在页面中有一个名称字段,可以在应用中创建色情明星。当我尝试创建色情明星对象的新实例时,我收到一个错误,指出名称字段不能为空。如果不是空白,我不知道为什么我会收到这些错误...
我使用simple_form生成表单。
这是我正在使用的部分形式:
应用/视图/页/色情明星/ _form.html.erb
<%= simple_form_for @pornstar do |f| %>
<% if @pornstar.errors.any? %>
<div id="error_explanation">
<h2>
<%= "#{pluralize(@pornstar.errors.count, "error")} prohibited this post from being saved:" %>
</h2>
<ul>
<% @pornstar.errors.full_messages.each do |msg| %>
<li>
<%= msg %>
</li>
<% end %>
</ul>
</div>
<% end %>
<%= f.input :name %>
<%= f.input :link %>
<%= f.submit 'Save' %>
<% end %>
色情明星表定义为此
分贝/ schema.rb
create_table "pornstars", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "link"
end
这是我的色情明星模型文件
应用/模型/ pornstar.rb
class Pornstar < ActiveRecord::Base
has_and_belongs_to_many :videos
has_many :categories, through: :videos
validates :name, presence: true, length: { maximum: 105 },
uniqueness: { case_sensitive: false }
end
我正在读到这可能是一个控制器错误,我必须确保强大的params接受属性,但在我的情况下它似乎没有改变任何东西。
这是我的控制器
应用/控制器/ pornstars_controller.rb
class PornstarsController < ApplicationController
#Show all pornstars action
def index
@pornstar = Pornstar.all
end
#New action for adding a pornstar
def new
if current_user.try(:admin?)
@pornstar = Pornstar.new
else
redirect_to root_path
end
end
#Create action saves the pornstar into the database
def create
@pornstar = Pornstar.new
if @pornstar.save(pornstar_params)
flash[:notice] = "Successfully posted pornstar!"
redirect_to pornstar_path(@pornstar)
else
flash[:alert] = "Error posting new pornstar!"
render :new
end
end
#Edit action retrieves the video and renders the edit page
def edit
if current_user.try(:admin?)
else
redirect_to root_path
end
end
#Update action updates the video with the new information
def update
if @pornstar.update_attributes(pornstar_params)
flash[:notice] = "Successfully updated pornstar!"
redirect_to pornstar_path(@pornstar)
else
flash[:alert] = "Error updating pornstar!"
render :edit
end
end
#The show action renders the individual video after finding the ID
def show
end
#The destroy action permanently deletes the video from the database
def destroy
if @pornstar.destroy
flash[:notice] = "Successfully deleted pornstar!"
redirect_to pornstars_path
else
flash[:notice] = "Error updating pornstar!"
end
end
private
def pornstar_params
params.require(:pornstar).permit(:name, :link)
end
def find_pornstar
@pornstar = Pornstar.find(params[:id])
end
end
我不知道问题出在哪里。你有什么建议?
提前多多感谢!!
修改
以下是我在 log / development.log
中找到的内容Started GET "/pornstars/new" for 69.159.166.176 at 2016-12-22 04:20:15 +0000
Cannot render console from 69.159.166.176! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by PornstarsController#new as HTML
[1m[35mUser Load (0.2ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Rendered pornstars/_form.html.erb (6.9ms)
Rendered pornstars/new.html.erb within layouts/application (7.8ms)
Rendered layouts/_header.html.erb (0.4ms)
Completed 200 OK in 54ms (Views: 51.2ms | ActiveRecord: 0.2ms)
Started POST "/pornstars" for 69.159.166.176 at 2016-12-22 04:20:24 +0000
Cannot render console from 69.159.166.176! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by PornstarsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"CQeWAfAWnTNBLITi7ieIokbg8HtPQ80rBTMfv7TwFeeBsVv0UWixynUObyPLi2jD9rz4zj0Necf08i9UmcHD+w==", "pornstar"=>{"name"=>"Matilde", "link"=>"http://"}, "commit"=>"Save"}
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
[1m[35mPornstar Exists (0.2ms)[0m SELECT 1 AS one FROM "pornstars" WHERE "pornstars"."name" IS NULL LIMIT 1
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
Rendered pornstars/_form.html.erb (7.4ms)
Rendered pornstars/new.html.erb within layouts/application (8.2ms)
[1m[35mUser Load (0.2ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Rendered layouts/_header.html.erb (1.8ms)
Completed 200 OK in 141ms (Views: 136.2ms | ActiveRecord: 0.7ms)
再次感谢!
修改2
所以我删除了唯一性验证(只是为了看看还有什么可能导致错误)并尝试输入一些有效的信息。
现在我收到了这个
Started POST "/pornstars" for 69.159.166.176 at 2016-12-22 12:50:47 +0000
Cannot render console from 69.159.166.176! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by PornstarsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"NcNhg4jPVK8WcQjrQIbTulBzT0n0/Pe9/hmDGcT5mDy9dax2KbF4ViJT4yplKjPb4C9H/IayQ1EP2LPy6chOIA==", "pornstar"=>{"name"=>"Crystal", "link"=>"http://1234"}, "commit"=>"Save"}
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
[1m[35m (0.1ms)[0m rollback transaction
Rendered pornstars/_form.html.erb (5.7ms)
Rendered pornstars/new.html.erb within layouts/application (6.4ms)
[1m[36mUser Load (0.2ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1[0m [["id", 1]]
Rendered layouts/_header.html.erb (1.8ms)
Completed 200 OK in 44ms (Views: 40.4ms | ActiveRecord: 0.4ms)
同样的错误说明名称字段不能为空。
再次感谢您提出我的问题!
答案 0 :(得分:0)
您已添加验证,以便在未提供名称字段时不应保存记录
validates :name, presence: true, length: { maximum: 105 },
从您的development.log中我可以看到name参数为nil,这就是它抛出错误的原因。
[1m[35mPornstar Exists (0.2ms)[0m SELECT 1 AS one FROM "pornstars" WHERE "pornstars"."name" IS NULL LIMIT 1
请尝试在名称字段中发送一些有效值(即nam,e应该是唯一的,因为还有唯一性验证,它不应该是nil)。