0

I want to do something a bit outside of the typical rails conventions and I wanted to hear what you guys think.

I have a Test model, with nested Questions, and Question_responses, and I have a form (using simple_form_for) where a user selects answers for the questions in a test. That is all working fine, but I would like to add a Flag feature if a test taker thinks a question is bad. Right now, I am using a link_to method to take the user to the flag controller actions that allow them to create the flag, and I temporarily hold onto the test id variable to take them back to the test. That's all working.

However, as one might see, I am not saving the progress of the test before the user heads over to create a flag - so if you were halfway through a test and flagged a question, you would lose your progress. I have an idea, but I'm not sure if it can work with simple_form :

I would post the data to some other action within the tests_controller, save it, redirect to the create the flag, and then redirect back to the test (in an edit method or something like it), and allow the user to finish the test. This should work, but how would I get the view with the form to post the test data to some other url besides the one specified by the simple_form_for @test, url: at the top of the form? Is there a way to conditionally post the data to another url depending on where the user clicked, or override the :submit protocol for the simple form button object? I'm really not sure, but I feel like someone has to have done something like this before.

4

1 回答 1

0

In case anyone else is new to rails and trying to solve a similar problem, here is what I did.

First, I found that simple_form_for is designed to post to one URL, which is the one that you specify in the arguments in the simple_form_for function. However, you can conditionally change the url that the form posts to by doing some logic on the url before you call the simple_form_for function (see this post).

But, in my case I wanted to post to different URLs dependent upon which "submit" button in the form was clicked. However as I said above, this functionality simply isn't built into simple_form. In theory I guess it would be possible to have a javascript function that would change the html data in the form and update the URL that the form is posting to if the user hovers over a button or something, but that is probably unnecessarily complicated unless you're super familiar with JS (unlike me). Bottom line is, there is an easier way.

However, you can change the parameters that are associated with the submit button for the form (see this post). So what I did, and what I would encourage others to do is change the parameters that are associated with a particular button in your form, and then apply the logic as needed in the controller action that the form is being posted to.

In my case, I created two buttons in the form - a button that would submit the test data in the usual fashion and a button for a test submission where the user is redirected to a create a flag. Before I redirect them, I save whatever form data has already been input (inside the tests_controller action). The user is then redirected over to the flag controller actions, and they can create a flag in the typical rails fashion. After the flag has been created, I redirect them back to the test, but this time in the edit path for the test. The data for the test is reloaded, and the user can continue creating a test.

If anyone would like to see the code for how I did this, please let me know in the comment section and I'll edit this answer!

于 2015-10-12T03:47:25.267 回答