如何在Rails中创建新记录时自动设置字段的值

时间:2021-11-17 06:47:19

Rails noob here. I have a rails application with (in this example) three tables. Users, Machines, and Tests.

Rails noob在这里。我有一个rails应用程序(在这个例子中)有三个表。用户,机器和测试。

Users have many Machines and Tests.

用户有许多机器和测试。

Machines belong to Users.

机器属于用户。

Tests belong to Machines.

测试属于机器。

When I create a new test, I want the field test.machine_id to automatically be set to the id of the machine that owns that test. I have been able to create a field with a drop-down menu showing all machines owned by current_user, but I don't want the user to have to set this field manually.

当我创建一个新测试时,我希望字段test.machine_id自动设置为拥有该测试的机器的id。我已经能够使用下拉菜单创建一个字段,显示current_user拥有的所有计算机,但我不希望用户必须手动设置此字段。

Tests can only be created by accessing a "create new test" link on a Machine Show page.

只能通过访问Machine Show页面上的“create new test”链接来创建测试。

*For example, User 1 has Machines 4 and 5. When viewing Machine 5's Show page, I want to create test 10. I want test(10).machine_id to be set to 5 without a user having to enter this manually.*

*例如,用户1有机器4和5.当查看机器5的显示页面时,我想创建测试10.我希望test(10).machine_id设置为5而无需用户手动输入。*

In my tests_controller.rb file, I have the following:

在我的tests_controller.rb文件中,我有以下内容:

 def new
@test = Test.new
@machines = current_user.machiness.all

respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml => @test }
end
end


 def create
@test = Test.new(params[:test])

respond_to do |format|
  if @test.save
    format.html { redirect_to(@test, :notice => 'Test was successfully created.') }
    format.xml  { render :xml => @test, :status => :created, :location => @test }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @test.errors, :status => :unprocessable_entity }
  end
end

end

I imagine I need something like:

我想我需要这样的东西:

def create
@test = current_machine.tests.build(params[:test])
...
end

...but I don't think current_machine is an actual object.

...但我不认为current_machine是一个实际的对象。

Tests can only be created by accessing a "create new test" link on a Machine Show page.

只能通过访问Machine Show页面上的“create new test”链接来创建测试。

Any suggestions?

2 个解决方案

#1


2  

Consider using a nested resource for your Test model. In your routes file, you would set up your resources like this:

考虑为Test模型使用嵌套资源。在您的路线文件中,您可以像这样设置资源:

resources :machines do
  resources :tests
end

This will make the route for a new Test look like /machines/:machine_id/tests/new. So now your link to the new Test page would look something like

这将使新测试的路由看起来像/ machines /:machine_id / tests / new。所以现在你到新测试页面的链接看起来像

<%= link_to "New Test", new_machine_tests_path(@machine) %>

and your new action in TestsController would be something like

你在TestsController中的新动作会是这样的

def new
  @test = Test.new
  @machine = Machine.find(params[:machine_id])
  ...
end

Finally, the form for your nested Test resource would be something like

最后,嵌套Test资源的表单就像

<%= form_for [@machine, @test] do |f| %>
...

That sets up the form to post to a path that will automatically include your machine_id, something /machines/123/tests.

这会将表单设置为发布到自动包含machine_id,某些/ machines / 123 / tests的路径。

So in the create action of your TestsController you can do something like

因此,在TestsController的创建操作中,您可以执行类似的操作

def create
  @test = Test.new(params[:test])
  @test.machine_id = params[:machine_id]
  ...
end

#2


1  

@test = Test.new(params[:test])
@test.your_field = 'default value'

or change filed in database to set default value:

或更改数据库中的字段以设置默认值:

change_column :tests, :your_column, :string, :default => 'default value'

#1


2  

Consider using a nested resource for your Test model. In your routes file, you would set up your resources like this:

考虑为Test模型使用嵌套资源。在您的路线文件中,您可以像这样设置资源:

resources :machines do
  resources :tests
end

This will make the route for a new Test look like /machines/:machine_id/tests/new. So now your link to the new Test page would look something like

这将使新测试的路由看起来像/ machines /:machine_id / tests / new。所以现在你到新测试页面的链接看起来像

<%= link_to "New Test", new_machine_tests_path(@machine) %>

and your new action in TestsController would be something like

你在TestsController中的新动作会是这样的

def new
  @test = Test.new
  @machine = Machine.find(params[:machine_id])
  ...
end

Finally, the form for your nested Test resource would be something like

最后,嵌套Test资源的表单就像

<%= form_for [@machine, @test] do |f| %>
...

That sets up the form to post to a path that will automatically include your machine_id, something /machines/123/tests.

这会将表单设置为发布到自动包含machine_id,某些/ machines / 123 / tests的路径。

So in the create action of your TestsController you can do something like

因此,在TestsController的创建操作中,您可以执行类似的操作

def create
  @test = Test.new(params[:test])
  @test.machine_id = params[:machine_id]
  ...
end

#2


1  

@test = Test.new(params[:test])
@test.your_field = 'default value'

or change filed in database to set default value:

或更改数据库中的字段以设置默认值:

change_column :tests, :your_column, :string, :default => 'default value'