undefined method `model_name' for NilClass:Class on view - ruby-on-rails

My code is working on local server but it's not working on the production server. I can't figure it out what I'm doing wrong.Please help me.
This is where I'm getting error in my partial:
<%=form_for #shiftchange, :url => { :controller=>"schedulers" ,:action => "shift_change" },:validate=>true , :method => :post do |f|%>
<%= f.label :from_date , "From Date " %>
<%= f.text_field :from_date ,:class =>'datepicker' %>
<% end %>
To load the partial,this is what I'm doing this:-
<%= render "schedule_shift" %>
In the controller I have this:
#shiftchange = Shiftchange.new

If the form is included for multiple actions (pages) you need to set #shiftchange to something or the form_for directive will fail (it won't have anything to create a form for).
A much better approach (and the Rails default) is to have separate views for each action, each including only the elements required by that action, and loaded into your application.html.erb layout with <%= yield %>. So you would have a app/views/shiftchanges/new.html.erb view which has the form in it. You never very rarely need to define any load paths in Rails, they are all derived from the model, controller and action names - or from your routes.rb. This is a core part of the convention over configurationhttp://en.wikipedia.org/wiki/Convention_over_configuration paradigm which runs so deep in Rails.
If you do need to have a form for creating a new object on every page (often used for ShiftchangeSessions for example), you can rewrite your form_for so that it doesn't depend on an object being present:
form_for(Shiftchange.new)
or if you need to force it to post to the create method
form_for(Shiftchange.new, :url => { :action => "create" })
You can read more about resource driven form_for in the Ruby On Rails API docs http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for-label-Resource-oriented+style.

Change
form_for #shiftchange
for this
form_for :shiftchange
i guess it works

Related

Re-writing Rails form helper in simple form

Is the following line in Rails Simple form
<% form_for #user :url => {:action => "attempt_login"}, do |f| %>
The same as the following in Rails form helper?
<%= form_tag(:action => 'attempt_login') do %>
If not, can you tell me what it would be? I need to redo some form code and I would like write down the correct syntax before running the app...
For in the case of passing parameters (i.e. :action) the simple form documentation is rather ambiguous.
Thanks!
The form_for is usually used for a specific record in order to update or create it.
Example:
# view in HAML (not ERB)
= form_for #user do |f|
= f.text_field :username
# matched with the routes
resources :users
other example with nested resources:
# routes.rb
resources :users do
resources :posts
end
# view in HAML
= form_for [#user, #post] do |f|
= f.text_field :content
Since you gave a record as an argument to the form_for method, it can "pre-fill" some fields of your record, i.e. if the #user already has a value for username, the field will be populated with that username.
It is true that you can specify an action to the form_for, something like this:
= form_for #user, url: { action: :custom_action } do |f|
The form_tag is used for the "other forms", such as a login form or a specific controller's action to be done.
Example:
# view in HAML
- form_tag action: :login do
= text_field_tag :username
= password_field_tag :password
To conclude, I would (based on my opinion) use the form_for helper if you are actually using a model's instance in the form and trying to modify/create it. So in your case, I would not use the form_for helper but use the form_tag instead (because you want a login form).
I recently migrated my form_tag methods to form_for because form_for was used in a gem that formatted my form using bootstrap. This was for a login form with no model. Here's how I used form_for without a model:
<%= form_for(:login, :action => 'attempt_login') do %>
I assume this works for simple_form as well:
<%= simple_form_for(:login, :action => 'attempt_login') do %>
Instead of using a form_tag, because the gem didn't decorate it, I used a form_for with a :symbol instead of a #model

Rails fields_for causing model instance to appear in view

This is really strange, but when I use a fields_for helper in order to give users the ability to submit multiple instances of my model, the view displays the instance data on the page like so:
Embiggened: http://i.stack.imgur.com/Omjt0.png
The code I have which generates the form is like so:
<%= form_for :metric_request, url: '/metric_request', :html => {:multipart => true} do |metric_request| %>
<%= #metric_requests.each do |m| %>
<%= metric_request.fields_for m, index: m.id do |f| %>
It works fine in generating multiple copies of my form, but whenever it's included, the view page spits out the model as in the image above. What gives? When I remove the fields_for helper, the output model instance text disappears... ?!
Edit: as pointed out by Joel Brewer, this was caused by my stupid copy/paste fail. Didn't pay attention and included the <%= instead of <%
Try:
<% #metric_requests.each do |m| %>
instead of:
<%= #metric_requests.each do |m| %>

Dynamic setting of form_tag submit destination

Is it possible to set the location you want to submit a form to dynamically at runtime with a form_tag? What I have is a shared partial view that is used to filter data on several screens. The view contains several different form fields and a submit button, so the UI for these controls is the same for all the screens that use it, thus the shared partial view. The difference is that I want the submit to go back to a different location depending upon which page the partial view is contained in. Is there someway to pass the destination in via the render tag like the following?
<%= render 'shared/event_filter', :dest => event_path %>
and then consume it within the partial view as follows?
<%= form_tag(:dest, :method => "get") do %>
The above code doesn't work, it gives me a nomethod error on the :dest in the form_tag, but I was just putting in this question to help illustrate what I was trying to do.
Any help/suggestions would be appreciated.
I think you might be looking for something along these lines:
<%= render :partial => 'shared/event_filter', :locals => {:form_action => event_path} %>
Which just renders the partial named shared/_event_filter.html.erb and passes in a variable called form_action with value of event_path.
Then inside your partial:
<%= form_tag form_action, :method => "get" do %>
<!-- snip -->
<% end %>

Using the textarea helper in Rails forms

Why does this code show an error in text area?
<%= form_for(:ad, :url => {:action => 'create'}) do |f| %>
<%= f.text_field(:name) %>
<%= f.text_area_tag(:text, "", :size => "50x10") %>
<%= submit_tag("Submit") %>
<% end %>
The FormHelper method is text_area, not text_area_tag.
Use either of the following:
<%= f.text_area(:text, size: '50x10') %>
or:
<%= text_area_tag(:ad, :text, size: '50x10') %>
The f variable that you are creating in the first line is a reference to your FormBuilder. By default it references ActionView::Helpers::FormBuilder or you can create your own.
The FormBuilder helper for textareas is called text_area. FormBuilder helpers are smarter than regular HTML helpers. Rails models can be nested logically, and your forms can be written to reflect this; one of the primary things FormBuilder helpers do is keep track of how each particular field relates to your data model.
When you call f.text_area, since f is associated with a form named :ad and the field is named :text it will generate a field named ad[text]. This is a parameter convention that will be automatically parsed into a Hash on the server: { :ad => { :text => "value" } } instead of a flat list of parameters. This is a huge convenience because if you have a Model named Ad, you can simply call Ad.create(params[:ad]) and all the fields will be filled in correctly.
text_area_tag is the generic helper that isn't connected to a form automatically. You can still make it do the same things as FormBuilder#text_area, but you have to do it manually. This can be useful in situations that a FormBuilder helper isn't intended to cover.

Best way to reload parts of a form

I want to reload a part of a form generated with the form_for-helper via AJAX.
After reloading the part I still want to have access to the form object.
How can I do this?
Best regards
I am not sure if your are using different terminology than I've heard, but what do you mean "still want to have access to the form object"?
Do you mean access to it in JavaScript? That should still work as long as you don't overwrite the form tags.
Do you mean in the html.erb code generating the partial? That doesn't really make sense, because that form_for object has already generated its html tags and gone out of scope. You need to use to the regular form of the helpers that takes the name of the object as the first parameter. There is no problem with this working with the tags generated by the form_for version of the helpers.
So, in your main page:
<%= form_for :person, #person, :url => { :action => "create" } do |f| %>
<%= f.text_field :name %>
<div id="reloadable">
</div>
<% end %>
And in your partial that fills that div:
<%= text_field :person, :name %>
No step 3.

Resources