Rails - Conditional Routes - Multiple Posts - ruby-on-rails

I am not sure if this is possible in Rails, but I have an order form where I would like to allow updates to certain fields based on the status of the order. I can manage this with the form_for and the appropriate logic in the controller and such.
However on the same form I would like to have a button which allows the user to pay (via stripe checkout) if the order is complete.
Essentially this "Pay" button should post to a different route (Payments) than the Order
Is there a way to define the 2nd submit button so it posts to a different route?

Related

Rails 5 Dynamically Load Nested Form

I'm working on a basic Rails app that currently has two models, Payperiod and Expense. A Payperiod has many Expenses, and an expense belongs to one Payperiod. I have my expenses resourced nested under payperiods, like so:
resources :payperiods do
resources :expenses
end
I am attempting to create an expense on the Payperiod show page. Ideally, I'd like to have a button for adding individual expenses. This is turning out to be trickier than I thought it would be. I know I need to use ajax to dynamically add the form for each expense and submit it but I can't figure out exactly how to make it work. In my Payperiod show.html.erb page I have a button with an id that I can click and make an action happen via jquery. My next step is to make this button render a form. I'm not sure if I can do this via jquery inside of an html.erb file, or if I should convert my payperiod show view to be show.js.erb.

Render a form in a modal(bootstrap) and make it available on another controller's view

It's been a while since I've used Rails and I think I've gotten a little rusty. Is there a way to do this?
I'm trying to make a messaging feature that allows one user type to message another. I want the button to display on the User index page and the user show page. When the button is clicked a modal will popup with a form contained therein.
Currently I've made a Message model with three columns: user_type1_id, user_type2_id and message_body.
Should I make a distinct controller for this new model? Or should I put the logic in the controller of user_type1 (the usertype that will be messaged)?
Any other suggestions would be welcome.
Controllers are there primarily to get data from the database and get it ready for the views. So if you have user#index and user#show pages, then you should use the UsersController for all the logic associated with those views, even though it uses other modals. It really is the "Rails Way". If, however, you were to create a message#index page, then you should create the associated MessagesController.
Also, there is nothing wrong with creating a partial and sticking in the messages view directory (the filename would be, say, messages/_form.html.erb). Then, whenever you needed that form (throughout the entire site), all you would need to do was type:
<%= render 'messages/form' %>

How to create multiple forms for Single Model and all forms must be submitted by one submit button?

I need to Add multiple records of one Model on a single submit button.
For Example
I need user to create multiple tasks at the end of the day. Each of these tasks has few attributes and what I need is a "Add another task" link or button to add another task record.
In short User Experience is very Similar to Standard Nested Form procedure.
Task does not have any parent resource which I could use in real world.
Task belongs_to 'user' and 'project' but actually they are not parent to the Task as I am not creating any record of User or Project while creating Task.
I do not want to limit the recursion of the form controls with "10.times" etc. as that is not right approach.
If you want only a submit button, then it's only one form (this is how HTML forms work).
If I understood right your request, here is a suggestion:
use javascript on a button on the page, to create more and more tasks, with an incrementing parameter
the submit button should point to a controller's method which creates them all, identifying them by that incrementing parameter
Also, you could create the tasks using AJAX, but this means they will go to the database right away.

Ruby on Rails - Adding a second (extremely) simple Auth layer in app

Once a user logs into their account, they are presented with a list of 'Employees'.
As of right now, when you click an employee, it takes the user to the 'show' page of that specific employee, however I want to add a 'pin-protected' aspect to that list before it renders the show page.
I want to add a simple layer of authentication that would go like this:
When a user clicks their name on a list, a text-field appears that asks for the selected employee's pin.
The user types in the pin. On submit, it compares the inputted pin against the 'pin' column for that employees' record. If it's correct it grants access to the selected employee's show page.
Is this something that is easily done in RoR? This is the first real app I have worked on, so I am having trouble wrapping my mind around a couple concepts like these.
Thanks so much!
Take a look at devise, it's most definitely your best bet for Ruby on Rails 3 authentication layer.
You're best bet if you just want to add a little functionality to your existing model class would be to add a method along the lines of:
def validate_pin(pin_to_check)
self.pin == pin_to_check
end
And then you just need to modify your employee controller so that show method checks to see if the pin has been provided (ideally via a session variable), otherwise redirect and request the pin with an additional method and route Employee#request_pin in the controller which asks the user to enter the pin, on success redirecting to the Employee#show route.
Session handling in the controller
To write the session variable, you'd need an Employee#check_pin method (as a POST route) and you'd just use the code:
session[:pin_valid] = true
Then you'd check session[:pin_valid] in your Employee#show method

Ruby on rails form validation for user profiles advice needed

I have one model that holds validation rules for my edit_profiles page. On the edit profile page I'm using jquery accordion to split user edit_profile into different sections for users to edit information. Each section is a separate form.
e.g.
Basic info (form 1)
Personal Stats (form 2)
Favourite things (form 3)
About me (form 4)
My problem is successfully filling out information on one form and clicking update is unsuccessful because other validation rules that have been set are firing in because other forms are failing validation because they have not yet be filled in.
I've tried to use the validation_group gem but this seems to have no affect.
I'd like to know if there is an easy way to do this?
Can't I just bunch up validation rules for each form and put them in separate methods and only make them come into play when the update button from a matching form has been clicked?
So if the update button on form 1 is clicked the form_one_validations method would be fire for example and the unrelated validation methods won't.
I would really really appreciate an example of how to do this.
This is the action responsible for y edit_profile view:
def edit_profile
#profile = Profile.find_by_user_id(current_user.id)
end
It is based inside my profiles controller.
Kind regards
I ended up using :allow_blank
This way the fields don't have to be filled in but all other important validation rules are still enforced if they need to be.
I can propose you such approach: you can add additional fields to your model, something like "basic_info_completed" which will be set up once after user has filled all corresponding information. And make all necessary validations conditional and perform them only when such field is set to true. So before user fills all fields of profile section, they are all can be edited without validation, but after profile fields are completed, validation is turned on for that part of profile.

Resources