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...
Related
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.
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' %>
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
Given the Ruby-on-Rails MVC framework, what is the best way to handle a rating api/function.
If I want to give users the ability to rate "stories", without actually editing them, what would be the best route to take. I don't want to put the story into edit mode, I just want to grab the story's ID and add a record in a Ratings table.
Would it be correct just to create a VIEW, in which the Ratings table is open for edit, but the linked stories are just in display mode???
Obviously I'm a beginner, when it comes to MVC and RonR, but I just want to get the proper way of doing this straight in my head :)
You would have a ratings model which would store the association between all the ratings and all the stories.
From there you would have a ratings controller that would be there to mainly create a rating(as in a user rates a story).
When a user creates a rating, the request should go to the RatingsController#create.
On the story view page, you would link to the RatingsController#create by passing in the paramater of the story id through the GET paramaters(by using nested resources).
I have a basic two tiered model structure: Articles -> Comments with one Article having many comments.
What is the best way to add a "Add a comment" form to the bottom of the Articles show page?
nested_attributes is overkill as I don't want to be able to edit all of the comments on the page, just to add one more.
Is the best way even with Rails 2.3 still to make a separate controller and embed a form_for pointing to the other controller into the Articles show view? If so, how do I get validation errors to return to the article display page?
I don't want to make a separate comment page/view...
thanks
Lay some code out SWR, and this will also help!
http://railscasts.com/episodes/196-nested-model-form-part-1