控制器操作中的实例变量是否可用于所有控制器视图?
我只是想打印已输入表单的参数。 基本上我创建一个新的赌注,然后显示参数:
MIGRATION
class CreateBets < ActiveRecord::Migration
def self.up
create_table :bets do |t|
t.integer :accepted ,:default => 0
t.integer :user_1_id #proposer
t.integer :user_2_id #receiver
t.integer :team_1_id #proposer's team
t.integer :team_2_id #receiver's team
t.integer :game_id
t.integer :winner
t.integer :amount
t.timestamps
end
end
def self.down
drop_table :bets
end
end
CONTROLLER
bets_controller.erb
class BetsController < ApplicationController
def index
redirect_to new_bet_path
end
def new
@b=Bet.new
end
def create
@points=params[:points]
@winner=params[:winner]
end
end
VIEWS New.erb
<% facebook_form_for Bet.new do |f| %>
<%= f.text_field :amount, :label=>"points" %>
<%= f.text_field :winner, :label=>"WinningTeam" %>
<%= f.buttons "Bet" %>
<% end %>
create.erb
points:<%= @points %>
<br>
winner:<%= @winner %>
我尝试使此代码与实例变量一起使用,但它也不起作用。问题出在哪里?
谢谢。
I am just trying to print the parameters that have been entered into my form.
Basically I create a new bet then I display the parameters:
MIGRATION
class CreateBets < ActiveRecord::Migration
def self.up
create_table :bets do |t|
t.integer :accepted ,:default => 0
t.integer :user_1_id #proposer
t.integer :user_2_id #receiver
t.integer :team_1_id #proposer's team
t.integer :team_2_id #receiver's team
t.integer :game_id
t.integer :winner
t.integer :amount
t.timestamps
end
end
def self.down
drop_table :bets
end
end
CONTROLLER
bets_controller.erb
class BetsController < ApplicationController
def index
redirect_to new_bet_path
end
def new
@b=Bet.new
end
def create
@points=params[:points]
@winner=params[:winner]
end
end
VIEWS
New.erb
<% facebook_form_for Bet.new do |f| %>
<%= f.text_field :amount, :label=>"points" %>
<%= f.text_field :winner, :label=>"WinningTeam" %>
<%= f.buttons "Bet" %>
<% end %>
create.erb
points:<%= @points %>
<br>
winner:<%= @winner %>
I tried to make this code work with instance variables but it didn't work either. Where is the problem?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为 params[:winner] 和 params[:point] 是空哈希。尝试将其添加到您的 create.erb 中:
它将显示您的
params
哈希值,以便您了解如何获取它。另一个提示,为什么您要在
new
操作中创建新对象,然后在表单中再次执行此操作?所以:还有一件事,保持命名约定确实很好,所以不要创建
@b
对象,而是创建@bet
。在
create
操作中,您应该有这样的行:在视图中:
如果您使用
,最好使用.
您的
index
操作完全没有用。如果您将所有行为从new
操作移至index
并完全删除new
操作,效果会更好。I think that
params[:winner]
andparams[:point]
is empty hash. Try adding this to your create.erb:It will display your
params
hash, so you will see how to get to it.Another hint, why you are creating new object in
new
action and then in form you are doing it again? So:And another thing, it is really good to keep naming conventions, so don't create
@b
object, but@bet
.In
create
action you should have line like this:And in view:
If you use
<br>
it's better to use<br/>
.Your
index
action is totaly useless. It would be better if you would move all behaviour fromnew
action toindex
and removenew
action completly.正如 klew 指出的,对我来说,你似乎得到了空的 params[:winner] 和 params[:point] 。您可以通过查看服务器日志来确定您所获得的内容。
你会看到一行像
As klew pointed, for me it seems that you're getting empty
params[:winner]
andparams[:point]
. You can make sure that of what you're getting by taking a look at your servers log.You will see a line like