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
Here is my 'new' view, new.html.erb
<h2>Add a new game!</h2> <%= form_for @game do |f| %>
<%= f.label :title %></br> <%= f.text_field :title %>
<%= f.label :console %></br> <%= f.text_field :console %>
<%= f.label :genre %></br> <%= f.text_field :genre %>
<%= f.label :released_on %></br> <%= f.text_field :released_on %>
<%= f.submit "Submit" %><% 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.
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.
I've tried calling the create method with params[:game] and also with strong parameters. It never works, it just returns blank values
Here is my rake routes file
Games::Application.routes.draw do get "static_pages/about" get "static_pages/contact" get "static_pages/help" resources :games end
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
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.
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.
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.Answer1:
You should be putting your <strong>
attributes</strong> in the <strong>
game_params method should be like this
private def game_params params.require(:game).permit(:title,:console,:genre,:released_on) endAnswer2:
Add all of your attributes on the new form to the strong params
def game_params params.require(:game).permit(:title, :console, :released_on, :genre) end
see the guides for more info: <a href="http://guides.rubyonrails.org/action_controller_overview.html#strong-parameters" rel="nofollow">http://guides.rubyonrails.org/action_controller_overview.html#strong-parameters</a>