Rails 5 Dynamically Load Nested Form - ruby-on-rails

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.

Related

Rails - Conditional Routes - Multiple Posts

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?

Ruby on rails one model created as much i want

I want to be able to put a number in the form section and that amount of models will be created.
I want to create a page with players. i want the option to submit for example 50 players. And on the second page will be 50 text area's where i can submit the names of each player.
I tried scaffolding but it did not work.
I couls not find a topic about this. Can anyone help me?
Your problem requires something a little more custom than scaffolding.
You will most likely need to create a few actions on your controller:
One to render the view where you will pick the number of models [GET]
One to submit the number of models you want to create and to render the form with 50 fields [POST]
One to submit the new models [POST]
Basically, you will have one field on the first view to select the number of new models you are trying to create and based on that you will render the next view with as many fields as that first number you picked.
Hope this helps...

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' %>

Copy a dynamic page in a view

My knowledge of Rails is pretty basic, but I have to fix a problem in a Rails project and the programmer can not be reached. So I'm trying to fix it myself, but I'm stuck.
The project revolves around user being able to add pictures to competitions, and to be able to vote on those pictures. The plan was to have to voting on the same page as the images, but this gives a few bugs in the JS and slow performance. So I want to have the voting and the overview on two different pages.
The problem is that I can't figure out how to create another page inside the views > competitions folder and link it up with the rest of the project. The easiest solution for me would be to copy the show.html.haml and paste it like votepage.html.haml but obviously that isn't so easy.
in the view > competitions folder there's an index.html.haml file, this displays a list of current competitions and gives a admin the ability to remove, add or edit certain competitions. When a user clicks on a link to a competition this gets rendered in the show.html.haml. On this page all the images that have been uploaded in that competition are shown. On that page I want a link that refers to the voting section. When a user clicks that link it should go to the votepage.html.haml (which is 100% the same as the show.html.haml but with different styling and added javascript). For now there's no need to actually make the voting work, "faking" it through front-end is good enough.
TLDR: I want to copy/paste a page in a view, but I don't know how to hook it up to the project.
Update1. I've used the console command rails generate controller competitions votepage which created a votepage for me. I could visit this one as well on http://localhost:3000/competitions/votepage
With the following code
- #competitions.each do |competition|
#container.js-masonry
.painting.item
= link_to competition do
- competition.pictures.shuffle.each do |picture|
= image_tag(picture.image_url)
I can insert images from the competitions in the page. But the problem is that I gets images from all competitions. And not so much competitions/1 , competitions/2 etc.
What you're missing is updating the routes file so that Rails knows what you want
Views:
competitions/
show.html.haml
vote.html.haml
...
Routes:
resources :competitions do
get :vote, on: :member
end
Member makes it behave like the show action, requiring a format like competitions/:id/vote
Edit:
You want to do the routes like above, but in the controller, make sure you get the competition from the id that will get passed
Controller:
def vote
#competition = Competition.find(params[:id])
end
And then instead of looping through all the competitions, you can just take the loop out and reference #competition
The basic answer is that you also need to copy the show method from app/controllers/competitions and make a votepage method with the contents in the same file.
This guide will help explain how views get wired (by the controller) to models: http://guides.rubyonrails.org/action_controller_overview.html

Rails has_many ajax

Lets say I have a recipe that has_many ingredients, I want the recipe creating page to have forms for each ingredient. How can I make it where a user can click a button to add a new ingredient then save all the ingredients and make them belong to the recipe?
The main idea here is to have a link on your recipe creation page that says something like "Add Ingredient" and give it a unique class. Then use jQuery to add an AJAX action to the click method of those links that will get the page used to create an ingredient. When you you hit Submit, it will come in the params as well. You may have to fiddle around with the IDs of stuff in order to make the the params hash work out nicer.
Let me know if that's enough to get you going or if you want some code examples too.
You're going to want to use accepts nested attributes and build. See Ryan Bates Railscast and just add ajax to it.
Here
And Here

Resources