下面是我的控制器代码和视图代码。该视图显示游戏列表,我希望在同一视图中将游戏添加到现有的表中。我在post过程中遇到了一些问题。有什么建议或者谁能指出我可能做错了什么吗?
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 发布于 2013-04-03 01:05:15
您没有定义create操作。当您为资源创建RESTful路由时,发送到/games的POST不会查找index操作,它会查找create操作。
搭建资源的脚手架是了解Rails希望您做什么的好方法。
rails g scaffold game name:string
这将创建您的RESTful路由,并在您的控制器中创建与它们相对应的操作。请注意,您可能希望在尝试搭建之前丢弃已有的内容,否则搭建可能无法创建所需的所有内容。如果您想在以后将视图代码粘贴回临时文件,请将其保存到临时文件中。
下面是我大概会怎么做的:
# 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 :gameshttps://stackoverflow.com/questions/15769899
复制相似问题