开发者

Rails 3 - Params question

开发者 https://www.devze.com 2023-03-30 18:53 出处:网络
I belive my question is rather simple but somehow I\'m not reaching any success. Let\'s say I have a model test and a form_for (@test), to create a new one.

I belive my question is rather simple but somehow I'm not reaching any success.

Let's say I have a model test and a form_for (@test), to create a new one.

In my controller create method, I have @test = Test.new(params[:test]). But what if I want to get in this params just the name of the test or something like that?

Ex:

 开发者_JAVA技巧@test_name = params[???]

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

And in my view:

<%= form_for(@test) do |t| %>
...
  <div class="field">
    <%= t.label :name %><br />
    <%= t.text_field :name %>
  </div>
  <div class="field">
    <%= t.label :field %><br />
    <%= t.text_area :field %>
  </div>
...
<% end %>

How can I do this?

Thanks for the future help.


You can access it directly like:

@test_name = params[:test][:name]

You can also do:

@test = Test.new(params[:test])
@test_name = @test.name

I am curious as to why you are assigning the name to an instance variable though.


You can just do:

@test = Test.new
@test.name = params[:test][:name]
@test.foo = params[:test][:foo]
# and so on...
0

精彩评论

暂无评论...
验证码 换一张
取 消