控制器操作中的实例变量是否可用于所有控制器视图?

发布于 2024-08-28 10:01:22 字数 1225 浏览 7 评论 0原文

我只是想打印已输入表单的参数。 基本上我创建一个新的赌注,然后显示参数:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愛上了 2024-09-04 10:01:22

我认为 params[:winner] 和 params[:point] 是空哈希。尝试将其添加到您的 create.erb 中:

params: <%= params.inspect %>

它将显示您的 params 哈希值,以便您了解如何获取它。

另一个提示,为什么您要在 new 操作中创建新对象,然后在表单中再次执行此操作?所以:

<% facebook_form_for @b   do |f| %>

还有一件事,保持命名约定确实很好,所以不要创建 @b 对象,而是创建 @bet

create 操作中,您应该有这样的行:

@bet = Bet.new(params[:bet])

在视图中:

<p>
  points:<%= @bet.points %>
</p>
<p>
  winner:<%= @bet.winner %>
</p>

如果您使用
,最好使用
.

您的 index 操作完全没有用。如果您将所有行为从 new 操作移至 index 并完全删除 new 操作,效果会更好。

I think that params[:winner] and params[:point] is empty hash. Try adding this to your create.erb:

params: <%= params.inspect %>

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:

<% facebook_form_for @b   do |f| %>

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:

@bet = Bet.new(params[:bet])

And in view:

<p>
  points:<%= @bet.points %>
</p>
<p>
  winner:<%= @bet.winner %>
</p>

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 from new action to index and remove new action completly.

清醇 2024-09-04 10:01:22

正如 klew 指出的,对我来说,你似乎得到了空的 params[:winner] 和 params[:point] 。您可以通过查看服务器日志来确定您所获得的内容。

你会看到一行像

Processing BetsController#create (for 127.0.0.1 at 2010-04-11 20:56:51) [POST]
Parameters: {"your"=>"parameters", "should"=>"apper in this hash"}

As klew pointed, for me it seems that you're getting empty params[:winner]and params[: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

Processing BetsController#create (for 127.0.0.1 at 2010-04-11 20:56:51) [POST]
Parameters: {"your"=>"parameters", "should"=>"apper in this hash"}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文