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.
Related
I created this file: views/lendings/terms.html.erb
lendings controller works fine for the other views.
I just want to write a link from views/lendings/show to terms.html.erb view
Do i have to write an action in lendings controller just to do that?
How do I configure the routes file?
How do I create the link in show view?
<%= link_to 'read terms', lendings_terms_path %> ?
Thanks!
Yes. you need to create an action called terms inside LendingsController. If you don't need anything special in the view, then you can just leave the method empty. Then in the routes file add this:
resources :lendings do
collection do
get :terms
end
end
I tried this and it didn't work
resources :lendings do
collection do
get :terms
end
end
Then it worked with:
resources :lendings do
member do
get 'terms'
end
end
As explained in: Adding More RESTful Actions/2.10.1 Adding Member Routes.
Check: http://guides.rubyonrails.org/routing.html
I'm trying to make
<%= link_to #company.name, current_company %>
work. It does work, as long as I stay within the same profile page. As soon as I'm trying to leave it, I get the NoMethodError in StaticPages#about - undefined method `name' for nil:NilClass. I understand the problem lies within the controller (I guess?), but that's it. Can anybody point me in the right direction please.
UPDATED
Updating methods in my satic_pages controller to
def home
#company = Company.new
end
def about
#company = Company.new
end
helps with skipping the error, but instead of the company name the links start to look like
/companies/2
Update: Since you want your link to simply display the company name, you can just do something like <%= link_to #company.name, #company%>
In your controller, you want to reference which company you're linking to, so you can do something like #company = Company.last. This would take the last record that you created. Now, when you load your page, the link should take you to the show page for that company with the last company's name as the text for the hyperlink.
I have made a resource.
resources :dashboards
I have a partial file which contains a form and I want to use this partial (as the form elements won't change) to update and create. So here is what I have:
Controller
class DashboardsController < ApplicationController
def new
#dashboard = Dashboard.new
end
end
View
/dashboards/new.html.erb
<%= render :partial => "form", :locals => { :dashboard => #dashboard } %>
Partial Form
/dashboards/_form.html.erb
<%= form_for(#dashboard) do |form| %>
.....
<% end %>
Ruby Guide
The Ruby Guide states:
The Article model is directly available to users of the application, so — following the best practices for developing with Rails — you should declare it a resource. When dealing with RESTful resources, calls to form_for can get significantly easier if you rely on record identification. In short, you can just pass the model instance and have Rails figure out model name and the rest. For example:
## Creating a new article
# long-style:
form_for(#article, :url => articles_path)
# same thing, short-style (record identification gets used):
form_for(#article)
## Editing an existing article
# long-style:
form_for(#article, :url => article_path(#article), :html => { :method => "put" })
# short-style:
form_for(#article)
Result
I thought I have followed the Rails Guide correctly. Because I made #dashboard a resource. I could just pass it into the form and have it handle the action, method and the rest. Instead I'm getting this:
<form accept-charset="UTF-8" action="/dashboards" class="new_dashboard" id="new_dashboard_" method="post">
According to the docs. Shouldn't the action of my form now be "/dashboards/new" because we are on the new action? And should it be passing an extra field declaring the method to be put when I use the same code in the /edit action??
My result is always the same no matter what. The form never changes.
What am I doing wrong?
EDIT
Here is my router info from rake routes
GET /dashboards(.:format) dashboards#index
POST /dashboards(.:format) dashboards#create
GET /dashboards/new(.:format) dashboards#new
GET /dashboards/:id/edit(.:format) dashboards#edit
GET /dashboards/:id(.:format) dashboards#show
PUT /dashboards/:id(.:format) dashboards#update
DELETE /dashboards/:id(.:format) dashboards#destroy
You are correct that you should be able to "pass #dashboard into the form and have it handle the action, method and the rest." The issue here is what new is in the context of RESTful actions.
When you declare a set of resources with resources :dashboards, you are creating a set of routes which map requests to controller actions:
GET /dashboards index
GET /dashboards/new new
POST /dashboards create
GET /dashboards/:id show
GET /dashboards/:id/edit edit
PUT /dashboards/:id update
DELETE /dashboards/:id destroy
You can check this if you run rake routes.
The issue here is that the new action is defined as a GET request to the path /dashboards/new, i.e. this is the route for the form itself. The URL in the action attribute of the actual form is something else: this is where the form will post the data to with a POST request, which on the server (rails) side will map to the create controller action.
When you use the form helper with form_for(dashboard), a form is created with a route corresponding to what dashboard is: if it is a new record (i.e. it does not yet exist in the database), then the form action will be create (and point to /dashboards), whereas if it already exists it will point to the actual URL for the record (e.g. /dashboards/123). This is what makes the form helpers so useful.
So, to sum up, /dashboards is the correct URL, not for the new action but for the create action, which the form helper uses because dashboard is a new record. new is the route to the page where the form resides, i.e. /dashboards/new.
Hope that makes sense.
p.s. as a side note, you shouldn't be accessing #dashboard in the partial if you are passing it in as a local (:locals => { :dashboard => #dashboard }). Just use dashboard.
I was given this ruby code to overview. I am still new to ruby on rails. I come from a java background.
in User.rb:
def last_name=name
require 'debugger'; debugger
self[:last_name] = name
end
And told me that this is a setter method. They told me that this get executed in the "form" in this line:
<%= f.label :last_name%>
<%= f.text_field :last_name %>
Ok. Could somebody clarify how this ridiculous syntax can be valid?
1) An instance of the class "User" is never initialized. How is the method even called?
2) Where does the variable "name" comes from? what is the value of it? (the variable name is called nowhere else) And what does this syntax stand for? "def last_name=name" ?? Pass to the method a variable that has not been initialized? It is a short-cut for another syntax just to save typing 2 more symbols?
3) How can this method be called, in the form? I dont see a "User.last_name("David") or anything similar.
Could somebody clarify those piece of code please?
And please dont post links to tutorial or anything else. Just clarify this piece of code
The code you described:
<%= f.label :last_name%>
<%= f.text_field :last_name %>
..is used by the default rails template engine. It is view code.
1) A User instance is likely initialized and populated when the form is submitted.
The form action corresponds to an appropriate controller action, which likely accepts :last_name as a parameter. When you submit the form, the controller action probably instantiates the User instance. Without more code, however, I can't be 100% certain this is the case with your application.
2) The variable name comes from the argument accepted by the last_name method.
Perhaps, to help you understand the method, let's rewrite it:
def last_name= (name)
require 'debugger'; debugger
self[:last_name] = name
end
Either last_name=('John Doe') or last_name = 'John Doe' will execute this method.
3) I think my previous descriptions should help you make sense of this..
MVC. In the action new a new instance of User is created and assigned to #user, which is what will be used for the form.
name comes from the method declaration def last_name=name
The form helpers does last_name=name when it assigns a value to that variable and uses the setter to do that. When you edit the object it will use the getter to display its value in the text field.
You can understand better point #2 with this syntax:
def last_name=( name )
end
The = is part of the function's name.
C equivalent would be
void last_name_equals( char *name ) {}
The rest is a Rails tutorial's job.
I have a dropdownmenu which populates from the database. I'm using the following code to do it:-
<%= collection_select(:abc, :SkillSetName, #technologies, :id, :Topic) %>
I have no idea what :abc and :SkillSetName are doing here. I just know that the drop down is being populated with :Topic from my #technologies variable. I want to save the selection made by the user from the drop down menu and send it to the next controller action. I don't want to use f.collection_select
If you don't know what :abc and :SkillSetName are, how are we supposed to know?
Anyway, the user's selection will be in:
params[:abc][:SkillSetName]
If you want to understand what you are doing, have a look at the API for collection_select.
See my answer - RoR: collection_select not setting the value in the DB
:abc stand for your object and :SkillSetName stand for your method.
when you want to save its value you can get it using params[:abc][:SkillSetName]