The following is my controller code and the view code . The view displays a list of games and i would like to add a game to the existing table in the same view. I'm having some issues with the post proceeding. Any suggestions or could anyone point out what I might me doing wrong?
class GamesController < ApplicationController
# GET /games
def index
@games = []
client = Games::Client.new
@games =client.get_games
if @games.blank?
render_404
end
if params[:name]
client.create_games params[:name]
end
end
end
%nav.navigation-bar.clearfix{:role => "navigation"}
%h3.reader-details Game tools
%a.home{:href => root_path, :role => "link", :title => "Home"}Home
%body
%section.form
%form.standard{:action => "/games", :method => "post"}
%input{type: 'text', name: 'name', class: 'text'}
%input.primary-action{type: 'submit', value: 'Add', class: 'button'}
.content
- if @games.present?
%table.games
%thead
%tr
%th type
%th id
%tbody
- @games.each do |game|
%tr
%td= game['type']
%td= game['id']
%a= link_to "Back", root_path
You don't have a create
action defined. When you create the RESTful routes for a resource, the POST to /games
doesn't look for the index
action, it look for the create
action.
Scaffolding your resource is a good way to see what Rails wants you to do.
rails g scaffold game name:string
This will create your RESTful routes and it will create the actions in your controller that correspond to them. Note, you may want to scrap what you have before attempting a scaffold, or the scaffold might not create everything that's needed. Save your view code to a temporary file if you want to paste it back in afterward.
Here's more or less how I'd go about it:
# View
= form_for Game.new do |f|
= f.text_field :name
= f.submit 'Add'
# Controller
def create
if Game.create(params[:game])
redirect_to :action => 'index'
else
flash[:notice] = "There was a problem creating your new game"
render :action => 'index'
end
end
# Routes
resources :games
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.