- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
这是关于 Rails 4: new instance is created and saved to database but not displayed in view 的后续问题.
在我们的 Rails 4 应用程序中,有四种模型:
class User < ActiveRecord::Base
has_many :administrations, dependent: :destroy
has_many :calendars, through: :administrations
end
class Administration < ActiveRecord::Base
belongs_to :user
belongs_to :calendar
end
class Calendar < ActiveRecord::Base
has_many :administrations, dependent: :destroy
has_many :users, through: :administrations
end
class Post < ActiveRecord::Base
belongs_to :calendar
end
以下是相应的迁移:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :first_name
t.string :last_name
t.string :email
t.integer :total_calendar_count
t.integer :owned_calendar_count
t.timestamps null: false
end
end
end
class CreateAdministrations < ActiveRecord::Migration
def change
create_table :administrations do |t|
t.references :user, index: true, foreign_key: true
t.references :calendar, index: true, foreign_key: true
t.string :role
t.timestamps null: false
end
end
end
class CreateCalendars < ActiveRecord::Migration
def change
create_table :calendars do |t|
t.string :name
t.timestamps null: false
end
end
end
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.references :calendar, index: true, foreign_key: true
t.date :date
t.time :time
t.string :focus
t.string :format
t.string :blog_title
t.text :long_copy
t.text :short_copy
t.string :link
t.string :hashtag
t.string :media
t.float :promotion
t.string :target
t.integer :approval
t.text :comment
t.timestamps null: false
end
end
end
我们有以下表单来创建帖子:
<h2>Create a new post</h2>
<%= form_for(@post) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<tr>
<%= f.hidden_field :calendar_id, value: @calendar.id %>
<td class="field"><%= f.date_field :date, placeholder: "When do you want to publish this post?" %></td>
<td class="field"><%= f.time_field :time, placeholder: "What time do you want to publish this post?" %></td>
<td class="field"><%= f.text_field :focus, placeholder: "What is this post about?" %></td>
<td class="field"><%= f.text_field :format, placeholder: "What type of post is this?" %></td>
<td class="field"><%= f.text_field :blog_title, placeholder: "If this post is about a blog post, what is the title of the blog post?" %></td>
<td class="field"><%= f.text_area :long_copy, placeholder: "What is the copy of the post?" %></td>
<td class="field"><%= f.text_area :short_copy, placeholder: "What is the short copy of the post (to be used on Twitter for instance)?" %></td>
<td class="field"><%= f.url_field :link, placeholder: "Which link to you want to embed in this post?" %></td>
<td class="field"><%= f.text_field :hashtag, placeholder: "Which hashtag(s) do you want to you in this post?" %></td>
<td class="field"><%= f.text_field :media, placeholder: "Which media file (image, video) do you want to include in this post?" %></td>
<td class="field"><%= f.number_field :promotion, placeholder: "What advertising budget should be allocated to this post?" %></td>
<td class="field"><%= f.text_field :target, placeholder: "Who do you want to target with this post?" %></td>
<td class="field"><%= f.select(:approval, %w['Approved' 'Needs edits' 'To be deleted'], {prompt: 'How does this post look?'}) %></td>
<td class="field"><%= f.text_area :comment, placeholder: "Any comment?" %></td>
<td><%= f.submit "Create", class: "btn btn-primary" %></td>
</tr>
<% end %>
此表单嵌入到 Calendars#Show
View 中,因此一旦创建帖子,它就会出现在相应的日历中。
这是我们的 PostsController
:
class PostsController < ApplicationController
def create
@post = Post.create(post_params)
if @post.save
redirect_to root_url
else
render root_url
end
end
end
目前,无论何时我们提交表单,都没有任何反应:
编辑:这里是服务器日志:
Started GET "/calendars/1" for ::1 at 2015-07-07 22:21:07 -0700
Processing by CalendarsController#show as HTML
Parameters: {"id"=>"1"}
Calendar Load (0.1ms) SELECT "calendars".* FROM "calendars" WHERE "calendars"."id" = ? LIMIT 1 [["id", 1]]
Post Exists (0.1ms) SELECT 1 AS one FROM "posts" WHERE "posts"."calendar_id" = ? LIMIT 1 [["calendar_id", 1]]
Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."calendar_id" = ? [["calendar_id", 1]]
Rendered posts/_post.html.erb (0.7ms)
Rendered shared/_error_messages.html.erb (0.0ms)
Rendered shared/_post_form.html.erb (5.1ms)
Rendered calendars/show.html.erb within layouts/application (13.7ms)
Rendered layouts/_shim.html.erb (0.1ms)
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Rendered layouts/_header.html.erb (0.9ms)
Rendered layouts/_footer.html.erb (0.4ms)
Completed 200 OK in 145ms (Views: 144.0ms | ActiveRecord: 0.4ms)
编辑 2:这是新的服务器日志,在实现 @miler350 在他的回答中推荐的内容之后:
Started POST "/posts" for ::1 at 2015-07-08 09:09:58 -0700
Processing by PostsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"+LS+B2/KiUq09cypZSQnzwj5xHbNnkYR6EC5hfjNCwc3/NfFkbWi3FJe0Zw3Mumsdnubnip5piQHXZ00eV1W2A==", "post"=>{"calendar_id"=>"", "date"=>"2015-08-20", "time"=>"12:04", "focus"=>"Test", "format"=>"Test", "blog_title"=>"Test", "long_copy"=>"Test", "short_copy"=>"Test", "link"=>"http://www.google.com", "hashtag"=>"Test", "media"=>"Test", "promotion"=>"50", "target"=>"Test", "approval"=>"'Approved'", "comment"=>"Test"}, "commit"=>"Create"}
Unpermitted parameter: calendar_id
(0.1ms) begin transaction
SQL (0.7ms) INSERT INTO "posts" ("date", "time", "focus", "format", "blog_title", "long_copy", "short_copy", "link", "hashtag", "media", "promotion", "target", "approval", "comment", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [["date", "2015-08-20"], ["time", "2000-01-01 12:04:00.000000"], ["focus", "Test"], ["format", "Test"], ["blog_title", "Test"], ["long_copy", "Test"], ["short_copy", "Test"], ["link", "http://www.google.com"], ["hashtag", "Test"], ["media", "Test"], ["promotion", 50.0], ["target", "Test"], ["approval", 0], ["comment", "Test"], ["created_at", "2015-07-08 16:09:58.605712"], ["updated_at", "2015-07-08 16:09:58.605712"]]
(0.6ms) commit transaction
(0.0ms) begin transaction
(0.0ms) commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 9ms (ActiveRecord: 1.5ms)
Started GET "/" for ::1 at 2015-07-08 09:09:58 -0700
Processing by StaticPagesController#home as HTML
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Rendered static_pages/home.html.erb within layouts/static_pages (1.8ms)
Rendered layouts/_shim.html.erb (0.0ms)
Rendered layouts/_static_header.html.erb (1.0ms)
Rendered layouts/_footer.html.erb (0.3ms)
Completed 200 OK in 148ms (Views: 147.5ms | ActiveRecord: 0.1ms)
Started GET "/users/1" for ::1 at 2015-07-08 09:10:02 -0700
Processing by UsersController#show as HTML
Parameters: {"id"=>"1"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Rendered shared/_error_messages.html.erb (0.7ms)
Rendered shared/_calendar_form.html.erb (5.0ms)
Administration Exists (0.5ms) SELECT 1 AS one FROM "administrations" WHERE "administrations"."user_id" = ? LIMIT 1 [["user_id", 1]]
Administration Load (0.5ms) SELECT "administrations".* FROM "administrations" WHERE "administrations"."user_id" = ? [["user_id", 1]]
Calendar Load (0.1ms) SELECT "calendars".* FROM "calendars" WHERE "calendars"."id" = ? LIMIT 1 [["id", 1]]
Calendar Load (0.1ms) SELECT "calendars".* FROM "calendars" WHERE "calendars"."id" = ? LIMIT 1 [["id", 2]]
Calendar Load (0.1ms) SELECT "calendars".* FROM "calendars" WHERE "calendars"."id" = ? LIMIT 1 [["id", 3]]
Calendar Load (0.1ms) SELECT "calendars".* FROM "calendars" WHERE "calendars"."id" = ? LIMIT 1 [["id", 4]]
Calendar Load (0.1ms) SELECT "calendars".* FROM "calendars" WHERE "calendars"."id" = ? LIMIT 1 [["id", 5]]
Rendered administrations/_administration.html.erb (4.1ms)
Rendered users/show.html.erb within layouts/application (32.1ms)
Rendered layouts/_shim.html.erb (0.3ms)
Rendered layouts/_header.html.erb (0.9ms)
Rendered layouts/_footer.html.erb (0.7ms)
Completed 200 OK in 150ms (Views: 135.3ms | ActiveRecord: 1.8ms)
Started GET "/calendars/1" for ::1 at 2015-07-08 09:10:04 -0700
Processing by CalendarsController#show as HTML
Parameters: {"id"=>"1"}
Calendar Load (0.1ms) SELECT "calendars".* FROM "calendars" WHERE "calendars"."id" = ? LIMIT 1 [["id", 1]]
Post Exists (0.1ms) SELECT 1 AS one FROM "posts" WHERE "posts"."calendar_id" = ? LIMIT 1 [["calendar_id", 1]]
Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."calendar_id" = ? [["calendar_id", 1]]
Rendered posts/_post.html.erb (1.1ms)
Rendered shared/_error_messages.html.erb (0.0ms)
Rendered shared/_post_form.html.erb (5.3ms)
Rendered calendars/show.html.erb within layouts/application (13.9ms)
Rendered layouts/_shim.html.erb (0.1ms)
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Rendered layouts/_header.html.erb (0.8ms)
Rendered layouts/_footer.html.erb (0.3ms)
Completed 200 OK in 113ms (Views: 111.2ms | ActiveRecord: 0.4ms)
编辑 3:这是我将 :calendar_id
添加到 PostsControllers
中允许的 post_params 时的服务器日志:
Started GET "/calendars/1" for ::1 at 2015-07-08 11:07:39 -0700
Processing by CalendarsController#show as HTML
Parameters: {"id"=>"1"}
Calendar Load (0.1ms) SELECT "calendars".* FROM "calendars" WHERE "calendars"."id" = ? LIMIT 1 [["id", 1]]
Post Exists (0.2ms) SELECT 1 AS one FROM "posts" WHERE "posts"."calendar_id" = ? LIMIT 1 [["calendar_id", 1]]
Post Load (0.2ms) SELECT "posts".* FROM "posts" WHERE "posts"."calendar_id" = ? [["calendar_id", 1]]
Rendered posts/_post.html.erb (0.7ms)
Rendered shared/_error_messages.html.erb (0.0ms)
Rendered shared/_post_form.html.erb (5.2ms)
Rendered calendars/show.html.erb within layouts/application (17.9ms)
Rendered layouts/_shim.html.erb (0.0ms)
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Rendered layouts/_header.html.erb (0.8ms)
Rendered layouts/_footer.html.erb (0.3ms)
Completed 200 OK in 160ms (Views: 147.4ms | ActiveRecord: 0.8ms)
问题可能来自哪里:模型、表单、 Controller 还是其他地方?
最佳答案
您必须创建您的帖子参数。
class PostsController < ApplicationController
def create
@post = Post.create(post_params)
if @post.save
redirect_to root_url
else
render root_url
end
end
protected
def post_params
params.require(:post).permit(:date, :time, :focus....) #put all columns that you want to submit via form here.
end
end
您得到的很可能是验证错误,因为您可能需要为您的帖子至少提交一个字段。当 rails 没有收到它时,您告诉它 render
而不是 redirect
所以您可能不会注意到重新加载。
关于ruby-on-rails - rails 4 : new instance creation through form_for not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31281446/
我使用的是基于 Eclipse 3.x 的 WID 6.1。 尝试右键单击文件夹时,新建-> 类,键入类的名称,然后单击完成,出现错误 "Creation of element failed. org
假设我想创建一个浏览器插件,每当某人单击我网页上的按钮时,该插件就会打开他们的软盘驱动器。 (你还记得那个老笑话吗?-别担心,我真的不想这么做。) 创建可在大多数浏览器和大多数操作系统上运行的浏览器插
我目前正在尝试在 Azure 容器实例上创建一个新容器,以在其上部署 .net core 应用程序镜像。 (我是这项技术的新手)。 我在 Azure 上创建了一个容器注册表,并在其上推送了一个镜像(.
我不确定我问的是否正确,但我知道你们都很聪明,能够弄清楚:)。我在压缩一些 python 类中的一些重复代码时遇到了麻烦。这是我的意思的一个例子...... class Parent: PAT
我想要扩展这些 C 预处理器行: #define _POUND_ #define _POUND_ _FALSE 0 _FALSE 展开,最后一行(即_FALSE)展开为0。我知道递归CPP不可能直接实
我需要创建 generic list可以充当List或 List因为我有以下情况。 if(e instanceof A) return A; else return B 但我想填充一个 List Re
我正在尝试构建一个共享对象,以便稍后在其他项目中使用共享对象中的函数 DoSomethingUseful()。它使用外部库以及我在多个项目中使用的一堆 header 。 使用 CMake,我创建了一个
void foo() { //some code MyClass m(); //some more code } C++ 标准是否确保 MyClass 类的构造函数将在 /
我正在尝试在 2D 游戏中创建斜坡。这个斜坡应该有一个 X 度。当现在一个球“滚过”这个斜坡时。这个坡度的效果应该改变,然后球的行为因此向后滚动,向前滚动或在其他地方初始化倾斜度。我认为我可以使用具有
我有一个开发分支,我想看看“分支创建”和现在的区别。实际上,我更感兴趣的是创建分支之后的提交,这更像是“创建分支”+1。 我知道我可以在 Git 命令中使用像 HEAD 和 HEAD~1 这样的简写。
在JavaScript: Understanding the Weird Parts讲师解释说,变量的内存是在所谓的创建阶段设置的(并且 undefined 已分配);然后执行阶段发生。但是,当我们不
我需要NodeJS代码来创建POST请求,以便从URL创建一个FB页面。必须代码有错误,我找不到它。错误消息为请求失败,状态代码为400。我没有包含accesToken字符串,但它存在于我的实际代码中
我开发了一个具有以下特点的 Fortran 代码: 全局变量 13 个带有多个子程序的模块 独立子程序 将英特尔 MKL 库用于 LAPACK 库(线性代数) 读写文本文件 代码变得相当大。即使在这个
我想创建一个数组 ArrayList .我尝试了以下方法: static ArrayList[] displayBlocks = new ArrayList[3]; 但是,我收到编译时错误: gene
云信息supports通过“堆栈创建选项”设置超时。但是这个部分在 serverless.yml 中?有什么解决方法吗? 最佳答案 查看actual code ,TimeoutInMinutes 参数
我们希望只有部分人能够在 Azure DevOps 云(?)存储库中创建“release/*”分支。 不幸的是,我在用户界面中找不到这个选项。我这里也找不到 https://learn.microso
我使用 Azure AD Graph API 差异查询来检测 Azure AD 中的更改。 对于 User 或 Group 类型的每个结果节点,如何区分该对象是新的还是仅更新的? 我使用ocp-aad
我正在发布简单的数据,例如 { "title" : "test Title" } 到(顺其自然)/posts uri,例如类似 RestAssured.baseURI = "http://localh
我正在尝试为撇号创建一个产品引擎。我在扩展“页面设置”表单时遇到问题,目前我想添加一个简单的文本区域来向页面添加概要 - 最终我想添加“产品”设置,但我需要先完成基础知识。 我创建了一个表单和一个设置
我使用 Azure AD Graph API 差异查询来检测 Azure AD 中的更改。 对于 User 或 Group 类型的每个结果节点,如何区分该对象是新的还是仅更新的? 我使用ocp-aad
我是一名优秀的程序员,十分优秀!