collection_select promt/default value in Rails 3 - ruby-on-rails-3

I have:
<%= collection_select(:name, :room_id, #room, :id, :name, class: "select optional", prompt: 'Add New Room') %>
On a user select, the ID of the room is passed to a script that makes the div with that ID holding the edit form visibile. I want to have an "Add New Room" option, which will show the form to create a new room. The problem is the "Add New Room" selector has no value associated with it, which means I have trouble building the div for that item. Is there anyway to give that a value or to more generically add an option to the select menu?

Related

Difference between rails label and label_tag

In rails both label and label_tag seem to work the same. Are there any internal differences on how they are rendered by rails ? And which one is a better to use ?
Use f.label when you are inside a form object created with form_for(...) do |f| and want to refer to a model-attribute. If your app is i18n-ed, Rails will use the translation to display the attribute name.
Use label_tag when you are not in a form object. (Or you are in a form object but want to create a dummy label for a non-model attribute.)
All form inputs have these two variants, with and without the _tag suffix, like select and select_tag, etc.
I'm assuming you mean just label and not f.label.
The difference I have seen between using only label and label_tag is that you cannot set custom labels while using only label i.e if you use
label :name, "My Name:
in the view, it will not render My Name but just Name.
But if you use
label_tag :name, "My Name:"
It will render My Name on the display.

Adding a new record that has a foreign key relationship using new_foo_path(#bar)

I'm trying to add a new location to a restaurant using Ruby on Rails.
In the restaurants#show action I run:
<%= link_to 'Add Location', new_location_path(#restaurant) %>
And I get a URL: http://localhost:3000/locations/new.1, where 1 is the id of the restaurant. But the new location form doesn't appear.
What is the rails way to handle this simple case? Is using new_FOO_path even the right thing to be doing?
In this case, I shouldn't have a drop down to select the restaurant because I want the end user to only be able to add a location to their own restaurant. I would somehow need to have a hidden input with the restaurant ID in the add location form, and also validate the id on the backend.
I would create a nested resource in your routes.rb. Example:
resources :restaurants do
resources :locations
end
Then your link target would be new_restaurant_location_path(#restaurant). In your controller, you can find the restaurant via Restaurant.find(params[:restaurant_id]).
Alternatively, set the restaurant id as a GET parameter with new_location_path(:restaurant_id => #restaurant.id).

rails where to put controller code

I have a select box on a new Workorder form so the user can select the Location.
This is the form input field:
<%= f.select :location_id, #loctree %>
I have some controller code that defines what #loctree should return.
But, I'm confused on where to put that code. Does it go into the Workorders_controller.rb in the "def new" area? Or does it go somewhere in the Locations_controller.rb?
Thbanks!
If the code you posted handles the new action for Workorders, then it should be in the new method in Workorders_controller. This method will be run before your template, and any member variables set there will be available for use in your template.

Show title instead of id in edit form

Say I got some lists and some tasks. Each list can have many tasks, a task belongs to a list. When I editing one of the tasks rails is showing the id of the corresponding list in the inputfield.
How is it possible to show the title of the list instead of the id?
You probably want to use a select input, something similar to this in your form code:
<%= f.select :list_id, List.all.collect { |l| [l.name, l.id] } %>
This will display the name of each List, but will actually assign the id to the task's list_id

Post of form in table

I have a #company known in my view and also #shops which is displayed on a table.
Each row of the table contains a button for the user to click on.
The click on that button should trigger a create in Clients and client belongs to both a Shop and a Company. The shop_id I want to send with a post request together with the company.
I need to generate a post request per line in the table. How do I do that? With a form_for? And how do I do it per row? And how do I send the shop_id (of that row) and company_id (in the view) to the post? I don't want to use params[:shop_id] because the user can change that right?
Found the solution. You need to use a hidden field:
<%= form_for '', :url => company_clients_url(shop), :html => {:method => :post} do %>
<%= hidden_field_tag 'shop_id', shop.id %>
Later in the controller you can pick it up with params[:shop_id]
Hope this helps somebody else out.