89200

Ruby on Rails - Data not saved. Index showing blank values

Question:

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>permitted params</strong>.Your game_params method should be like this

private def game_params params.require(:game).permit(:title,:console,:genre,:released_on) end

Answer2:

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>

Recommend

  • function is undefined or private while should be accessible
  • Delphi - Simple TCP client / server using Indy to check clients status
  • rails tutorial: cookie doesn't match remember token
  • importing java classes in matlab classdef
  • How do I add each price to the current price?
  • How can you perform functional tests on JavaScript responses?
  • Rails ajax form not submitting from within another
  • what is the advantage of EventMachine
  • Rails 3 Two Fields Uniqueness Validation when combined
  • Reflection in ruby?
  • ActiveRecord: abort datetime parsing if value is invalid
  • Powershell Transcript is empty when running script from SQL Agent Job in 2005 SQL Server
  • How to write an RSpec test for Ryan Bates' helper from screencast #228?
  • current user id saved from _form or controller
  • I can't create model objects using accepts_nested_attributes_for. It won't create the nest
  • Rails has_many :through association: save instance into join table
  • firebase unauth with google doesn't allow change of user
  • how to add semantic ui in a rails app?
  • Single virtual attribute definition for multiple fields
  • Rails db:seed error “undefined method `finder_needs_type_condition?' for nil:NilClass”
  • Contact form problem - I do receive messages, but no contents (blank page)
  • How to Add Polymorphic Comments to Feed?
  • Best practice to eliminate magic numbers within a member function
  • How can I make this modal persistent?
  • Loading .coffee files via a view in Rails
  • Swift: Switch statement fallthrough behavior
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • java.lang.NoClassDefFoundError: com.parse.Parse$Configuration$Builder on below Lollipop versions
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Cannot Parse HTML Data Using Android / JSOUP
  • How to get icons for entities from eclipse?
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • Getting Messege Twice Using IMvxMessenger
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • JaxB to read class hierarchy
  • UserPrincipal.Current returns apppool on IIS
  • How to load view controller without button in storyboard?