views:

28

answers:

1

The application I am creating in RoR has a model (Practicetest) and its controller has the standard CRUD actions then an Sat & Act action. The Sat & Act actions are essentially the index of practicetests but if the test_format value is set to "sattest" then the test is displayed on the practicetest/sat view and if the test_format value is set to "acttest" then the test is displayed under practicetest/act. On both the sat & act pages there is a link to create a new practicetest, but I am trying to find a way to set it up so that the sat page assigns the object being created with the value "sattest" for its test_format (and the same for act). Any ideas on how I can go about assigning a value depending on which action the create came from?

Thanks!

+1  A: 

If I understand this correctly, it sounds like you're rendering a new test form on the sat and act pages. If you're using form_for (which I hope you are), just assign your object's test_format before rendering your form, and include a hidden field so the value is passed with the POST request, so it would be something like this:

<!-- this can be done in the controller or template -->
<% @test = Test.new %>

<!-- assign test_format value -->
<% @test.test_format = "sattest" %>

<% form_for @test do |f| %>
  ...
  <%= f.hidden_field :test_format %>
  ...
<% end %>
Alex Reisner
Thank you so much, this works great.
Trevor Nederlof