我在rails中使用scaffold来创建一个用户可以提交想法的应用程序,然后所有想法都将列在主页上。我目前只是尝试获取提交和列表功能,但是_form.html.erb文件中生成的脚手架形式似乎并没有实际提交任何内容或更新数据库。即,我既没有收到“提交成功的想法”的消息,也没有收到“提交想法错误”的消息。它只是刷新表单页面。
我想知道我的routes.rb文件是否有问题,因为在互联网上花了很多时间之后我觉得我的大部分代码都应该有效。
idea.rb
class Idea < ApplicationRecord
belongs_to :user
belongs_to :category
has_many :comments
validates :title, :user, :anonymous, :contents, presence: true
validates :deleted, inclusion: { in: [true, false]}
scope :alive, -> { where deleted: false }
def children
Comment.where(idea_id: id, deleted: false)
end
end
ideas_controller.rb
class IdeasController < ApplicationController
def index
@ideas = Idea.alive.paginate(page: params[:page], per_page: 20)
end
def show
@idea = Idea.find(params[:id])
@comments = @idea.children
@reply = Comment.new
end
def new
@idea = Idea.new
@categories = Category.all.map{|c| [ c.name, c.id ] }
end
def new_comment
@idea = Idea.find(params[:id])
@comment = Comment.new
end
def create
@idea = Idea.new(idea_params)
if @idea.save
redirect_to @idea, alert: "Idea submitted successfully."
else
redirect_to new_idea_path, alert: "Error submitting idea."
end
end
def idea_params
params.require(:idea).permit(:title, :contents, :anonymous, :category)
end
end
_form.html.erb:
<%= form_for @idea do |f| %>
<% if idea.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(idea.errors.count, "error") %> prohibited this idea from being saved:</h2>
<ul>
<% idea.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.check_box(:anonymous) %>
<%= f.label(:anonymous, "Hide my name") %>
</div>
<div class="field">
<%= f.label :contents %><br />
<%= f.text_area :contents %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
create_ideas.rb:
class CreateIdeas < ActiveRecord::Migration[5.0]
def change
create_table :ideas do |t|
t.string :title
t.text :contents
t.boolean "anonymous", limit: 1, default: false
t.boolean "deleted", limit: 1, default: false
t.timestamps
end
end
end
routes.rb中:
Rails.application.routes.draw do
resources :ideas
end
编辑:本身没有错误跟踪,因为我没有看到任何内容 - 表单页面只是重新加载。但是,这是服务器日志的输出:
Started POST "/ideas" for ::1 at 2017-01-28 20:40:07 -0500
Processing by IdeasController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"5ooCTQZKBv1f1+SlZyl/7tkJzLx95EpckoPBpB710JrikRJqt8wOdE0GT3bjBl1m6wIxKQhzfXmKRrvkV+RHeA==", "idea"=>{"title"=>"sadfsd", "anonymous"=>"1", "contents"=>"sdafasdf"}, "commit"=>"Create Idea"}
(0.3ms) begin transaction
(0.1ms) rollback transaction
Redirected to http://localhost:3000/ideas/new
Completed 302 Found in 26ms (ActiveRecord: 0.4ms)
答案 0 :(得分:1)
您已经过验证,并且您违反了:user
状态。
您的ideas
表格没有user_id
列,这使得belongs_to :user
关系无效。
您需要更改迁移
class CreateIdeas < ActiveRecord::Migration[5.0]
def change
create_table :ideas do |t|
t.string :title
t.text :contents
t.boolean "anonymous", limit: 1, default: false
t.boolean "deleted", limit: 1, default: false
t.integer :user_id, index: true
t.timestamps
end
end
end
再次运行迁移,rake db:reset
或者,您可以创建另一个迁移以添加user_id
列。
在您的控制器create
中,确保您拥有current_user
,或以任何方式获取创建idea
的用户,并更改为
@idea = current_user.ideas.new(idea_params)