Ruby on Rails-数据未保存。索引显示空白值 [英] Ruby on Rails - Data not saved. Index showing blank values
问题描述
我正在创建Ruby on Rails应用程序,但遇到数据库问题。我具有控制器操作和新操作的视图,但是除非使用Rails Console,否则我的值永远不会保存到数据库中。我的Rails版本是4.0.4。这是我的GamesController
I'm creating a Ruby on Rails application but encountered a database problem. I have controller actions and views for the 'new' action but my values never get saved to the database unless I'm using Rails Console. My Rails version is 4.0.4. Here is my GamesController
class GamesController < ApplicationController
def index
@games = Game.all
end
def show
@game = Game.find(params[:id])
end
def new
@game = Game.new
end
def create # This is the important method here.
@game = Game.new(game_params)
if @game.save
redirect_to games_path, :notice => "Game was saved"
else
render 'new'
end
end
def edit
@game = Game.find(params[:id])
end
def update
@game = Game.find(params[:id])
if @game.save
redirect_to @game
else
render 'edit'
end
end
def destroy
@game = Game.find(params[:id])
@game.destroy
redirect_to :action => 'index'
end
def game_params #Probably should use something inlace of :tags
params.require(:game).permit(:tags)
end
end
这是我的新视图new.html.erb
Here is my 'new' view, new.html.erb
<h2>Add a new game!</h2>
<%= form_for @game do |f| %>
<p>
<%= f.label :title %></br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :console %></br>
<%= f.text_field :console %>
</p>
<p>
<%= f.label :genre %></br>
<%= f.text_field :genre %>
</p>
<p>
<%= f.label :released_on %></br>
<%= f.text_field :released_on %>
</p>
<p>
<%= f.submit "Submit" %>
</p>
<% end %>
当我将值传递到新表单中并单击提交(创建操作)时,我重定向到索引页面。不幸的是,我在每个字段中看到空白值,这意味着数据实际上并未保存。
When I pass in the values to the new form, and click on submit (create action), I get redirected to the index page. Unfortunately, I see blank values for every field, meaning the data wasn't actually saved.
我尝试通过验证进行测试,仅验证标题,但没有用因为未保存值。另一方面,当我运行rails console时,我可以创建游戏类的新实例,并将它们保存到数据库中。所以我不知道为什么它不能在表单上使用。
I tried testing with validations and validating only the title but it didn't work because the values were not saved. On the other hand, when I run rails console, I can create new instances of the games class and they get saved to the database. So I have no idea why it doesn't work on the form.
我尝试用params [:game]以及强大的参数调用create方法。它永远不会起作用,它只会返回空值
I've tried calling the create method with params[:game] and also with strong parameters. It never works, it just returns blank values
这是我的耙路文件
Games::Application.routes.draw do
get "static_pages/about"
get "static_pages/contact"
get "static_pages/help"
resources :games
end
根据请求,我将添加我的gemfile
By request, I will add my gemfile.
gem 'rails', '4.0.4'
gem 'sqlite3'
gem 'sass-rails', '~> 4.0.2'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 1.2'
group :doc do
gem 'sdoc', require: false
end
我的问题是提交表单数据时如何显示这些值?这和我的路线有关吗?我宣布了资源游戏,以将7条RESTFUL路线链接到一条线上。
My question is how do I get these values to show up when submitting form data? Does it have something to do with my routes? I declared resources games to link the 7 RESTFUL routes on one line.
我知道POST请求用于create方法。我感到困惑,它可以在Rails控制台中使用,但不能在表单上使用。
I know that POST requests are used for the create method. I find it puzzling, that it works in rails console, but not on the form.
我在StackOverflow上进行了广泛搜索,找到了一个关于strong_params的答案。不幸的是,我将其合并到控制器中,但仍然无法正常工作。我仍然认为我的问题在于我如何使用strong_params。
I searched extensively on StackOverflow and found one answer regarding strong_params. Unfortunately, I incorporated that into my controller, and it still didn't work. I still think my problem lies in how I'm using strong_params.
我基本上在该答案中剪切并粘贴了代码,因此在这种情况下可能不起作用。我也尝试阅读文档,但我听不懂。
I basically cut and pasted the code in that answer so it might not work in this case. I tried reading the documentation as well, but I couldn't understand it.
推荐答案
您应该放下 < 允许的参数
中的code>属性 。您的 game_params
方法应该是这样的
You should be putting your attributes
in the permitted params
.Your game_params
method should be like this
private
def game_params
params.require(:game).permit(:title,:console,:genre,:released_on)
end
这篇关于Ruby on Rails-数据未保存。索引显示空白值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!