Trouble with the default view undefined method `name' for nil:NilClass - ruby-on-rails-3

I'm having trouble getting my view to render. I think I know why I'm getting the error:
undefined method `name' for nil:NilClass
Extracted source (around line #21): (showing 18-24)
<ul>
<%= form_tag(default_hero_user_path, :method=>'post') do %>
<%= label_tag "Name" %>
<%= text_field_tag "name", #user.default_hero.name %> #line 21
<%= submit_tag 'Set hero', class: "btn btn-large btn-primary" %>
<% end %>
</ul>
I know you can't call .name on a nil object but I don't understand why #user.default_hero is nil. I want the user to be able to set their 'hero' but having trouble setting the default obviously.
Here is the users controller:
# creates or updates the default hero
def default_hero
#user = User.find(params[:id])
hero = #user.default_hero
if hero.nil?
# we don't have a default hero so we need to add one'
hero = Hero.new
#user.heros << hero
end
hero.default = true
hero.name = params[:name]
hero.save
redirect_to #user # shows the user again to see any updates
end
And here is where I believe I am having the problem in setting a default view-
def show
#user = User.find(params[:id])
if #user.default_hero.nil?
name = params[:q]
else
name = #user.default_hero.name
end
Thanks for your guys' time and attention if you can point me in the right direction in how to solve this I would ppreciated.

Your show controller action doesn't make much sense. You are setting a local variable (which won't be accessible in the view) if #user.default_hero.nil?. This doesn't accomplish anything and #user.default_hero will still be nil in the view.
That being said, to get rid of your error you can simply do this:
<%= text_field_tag "name", #user.default_hero.nil? ? '' : #user.default_hero.name %>
Based on your comment I would does something like this:
Model
class User < ActiveRecord::Base
has_many :heros
def set_default_hero(name)
hero = self.heros.find_by_name(name) # check if the hero exists already
hero = self.heros.build(:name => name) if hero.nil? # new object if not
hero.default = true # set it as default
hero.save
end
end
Controller:
def default_hero
#user = User.find(params[:id])
#user.set_default_hero(params[:name])
redirect_to #user
end
def show
#user = User.find(params[:id])
#default_hero = #user.default_hero.nil? ? '' : #user.default_hero.name
end
View:
<%= text_field_tag "name", #default_hero %>
You ignored my question about params[:q], so I don't know what to do with that.

Related

Argument error when trying to create a mission

I have a form for creating new Missions with an Admin account. Each Mission is linked to an account. When I visit admin/missions/new I get an error "ArgumentError in Admin::Missions#new". Can anyone point to what I'm doing wrong?
When I checked the rails console a mission id and name does show up, but I guess I'm missing the Admin for the mission.
Here's my Controller
class Admin::MissionsController < Admin::ApplicationController
def index
#missions = missions
end
def new
#mission = current_account.missions.new(params[:mission])
#mission.save
end
def create
render plain: params[:mission].inspect
end
def edit
#mission = missions.find(params[:id])
end
private
def missions
#missions ||= current_account.missions
end
end
Here's my form
<%= form_with [:admin, #mission] do |f| %>
<div>
<label>Name</label>
<%= f.text_field :name %>
</div>
<div>
</div>
<div>
<%= f.submit %>
</div>
<% end %>
I'm expecting the url admin/missions/new to take me to the form, but I I get the argument error wrong number of arguments (given 1, expected 0)
You don't have params yet in the new action as the view (where the form which has to collect the params is) is rendered after the controller code. The object should be created in the create action:
def create
current_account.missions.create(params[:mission])
end
And the new action should be just:
def new
#mission = current_account.missions.new
end
Check the Binding a Form to an Object section of the Rails guide.
The form code seems also to be wrong, as form_with [:admin, #mission] do |f| is not a valid syntax. It should be:
<%= form_with model: [:admin, #mission] do |form| %>
Check form_with documentation for more details.
Ideally new method is just for creating a new instance and create method is for saving records. Changing to what Ana Maria is suggesting should fix your issue. Thanks.

Rails: form_for in application wide footer

I started learning Rails for about a month now and I am working on a fairly simple project to improve my skills. It's a blog where editors can add articles and users can subscribe to a newsletter by adding their email. The homepage is an index view and their is a footer that shows up across all pages.
Here is my problem: I would like to include the form_for the newsletter on the footer that exists inside the application layout. That form has a specific model: Subscriber, which stores users emails.
What I've done so far is include the following on the Articles controller:
def index
#articles = Article.order(created_at: :desc).all
#subscriber = Subscriber.new
end
def create
#subscriber = Subscriber.new(subscriber_params)
if #subscriber.save
redirect_to '/home'
else
render 'new'
end
end
def subscriber_params
params.require(:subscriber).permit(:email)
end
In application.html.erb :
<%= form_for(#subscriber) do |f| %>
<%= f.email_field :email, :placeholder => "email address" %>
<%= f.submit 'Sign up', :id => "signup" %>
<% end %>
The form is displayed correctly on the index page only and it doesn't save to the database (without error).
I have tried using a before filter on the application controller as well as rendering a partial without any success.
Edit
Subscribers controller code:
def new
#subscriber = Subscriber.new
end
def create
#subscriber = Subscriber.new(subscriber_params)
if #subscriber.save
redirect_to '/home'
else
render 'new'
end
end
private
def subscriber_params
params.require(:subscriber).permit(:email)
end
Application controller:
before_action :create_new_subscriber
def create_new_subscriber
#subscriber = Subscriber.new
end

Accessing non-current state_machine states

I've got six states on my order.rb file. I want to access each of the non-current states to populate a drop-down menu so the state can readily be changed. I've come up with this. The function doesn't work, obviously, and the states are written_like_this.
<% order.state_paths.to_states.each do |state| %>
<%= link_to(state.to_s, order.adjust(state)) %>
<% end %>
I'd like to write a catch-all method too that interprets the clicks from the above menu and transitions the record to the selected state. Something like (pseudo-code):
def adjust(state)
#order = Order.find(params[:id])
#order.state = state
end
Any thoughts would be great. Cheers!
I've figured it out.
def adjust
session[:return_to] ||= request.referer
#doc = Doc.find(params[:id])
state = params[:state]
respond_to do |format|
#doc.update_attribute(:state, state)
#doc.create_activity state.to_sym, owner: current_user
format.html { redirect_to root_url, notice: "#{#doc.title} has now been #{state}." }
format.json { render json: #doc }
end
end
And I've made the buttons like this:
<% activity.trackable.state_paths.events.each do |s| %>
<%= link_to(s.to_s.titlecase, adjust_path(s, activity.trackable)) %>
<% end %>
Boom.

rails form update method not called

I try to update my settings through a form but the update function is not called when I submit. It redirects to edit_settings_path when I submit and as per serve log update is not called. Why?
<%= form_tag settings_path, :method => :put do %>
<p>
<%= label_tag :"settings[:default_email]", "System Administrator" %>
<%= text_field_tag :"settings[:default_email]", Settings['default_email'] %>
</p>
<span class="submit"><%= submit_tag "Save settings" %></span>
<% end %>
Controller
class SettingsController < ApplicationController
def update
params[:settings].each do |name, value|
Settings[name] = value
end
redirect_to edit_settings_path, :notice => "Settings have been saved." }
end
end
** Update **
Update is now called properly (edited controller). Server log confirms Settings Load (0.2ms) SELECT "settings".* FROM "settings" WHERE "settings"."thing_type" IS NULL AND "settings"."thing_id" IS NULL AND "settings"."var" = ':default_email' LIMIT 1
UPDATE "settings" SET "value" = '--- 1111aaa2222...', "updated_at" = '2011-12-18 21:03:21.782075' WHERE "settings"."id" = 2
However it doesn't save to the Db and have no clue why. I'm using the Rails-settings gem 'git://github.com/100hz/rails-settings.git'
Don't know where to check since it says it updated record but in fact no.
why are you using the form_tag method?
If you are just trying to make a standard update form, use:
<%= form_for(#settings) do |f| %>
FORM CODE
<%= end %>
Your controller uses the edit method to render the view and the update method for the calback (to interact with the model)
If you insist on using
<%= form_tag setting_path, :method => :put do %>
Normally you would use the singular word if you are working on a member and the plural if you are working on an collection.
fyi: I dont know what your design is like, but i would have a model settings and a model settings_item...

undefined method `to_key' for #<Class:0x17a6408> -rails-3

I am experiencing problems with undefined method `to_key' for, on a form for polymorphic upload.
This is the form partial:
<%= form_for [#parent, Upload], :html => { :multipart => true } do |f| %>
<div class="field">
<%= f.label :document %><br />
<%= f.file_field :document %>
</div>
<div class="actions">
<%= f.submit "Upload"%>
</div>
<% end %>
This is the controller:
class UploadsController < ApplicationController
before_filter :find_parent
respond_to :html, :js
def index
#uploads = #parent.uploads.all unless #uploads.blank?
respond_with([#parent, #uploads])
end
def new
#upload = #parent.uploads.new unless #uploads.blank?
end
def show
#upload = #parent.upload.find(params[:upload_id])
end
def create
# Associate the correct MIME type for the file since Flash will change it
if params[:Filedata]
#upload.document = params[:Filedata]
#upload.content_type = MIME::Types.type_for(#upload.original_filename).to_s
#upload = #parent.uploads.build(params[:upload])
if #upload.save
flash[:notice] = "suceessfully saved upload"
redirect_to [#parent, :uploads]
else
render :action => 'new'
end
end
end
def edit
#upload = Upload.where(params[:id])
end
private
def find_parent
classes ||= []
params.each do |name ,value|
if name =~ /(.*?)_id/
#parent = classes << $1.pluralize.classify.constantize.find(value)
end
end
return unless classes.blank?
end
end
If i change
<%= form_for [#parent, Upload], :html => { :multipart => true } do |f| %>
to
<%= form_for [parent, Upload], :html => { :multipart => true } do |f| %>
I get a new error: undefined local variable or method `parent' for #<#:0x21a30e0>
This is the error trace:
ActionView::Template::Error (undefined method `to_key' for #<Class:0x2205e88>):
1: <%= render :partial => "uploads/uploadify" %>
2:
3: <%= form_for [#parent, Upload], :html => { :multipart => true } do |f| %>
4:
5:
6: <div class="field">
The "uploads/uploadify" partial is in this gist: https://gist.github.com/911635
Any pointers will be helpful. Thanks
From what I can see, your form_for should be something along the lines of
<%= form_for [#parent, #upload], :html => { :multipart => true } do |f| %>
as I'm assuming your upload object is nested within another object, similar to the following:
resources :posts do
resources :uploads
end
What form_for does when passed an array like this is construct the relevant path based on the class of the given objects and whether they are new records.
In your case, you create a new upload object in the new action of your controller, so form_for will inspect the array, get the class and id of #parent, then get the class and id of #upload. However, because #upload has no id, it will POST to /parent_class/parent_id/upload instead of PUTting to parent_class/parent_id/upload/upload_id.
Let me know if that doesn't work and we'll figure it out further :)
-- EDIT - after comments --
This means that one of #parent or #upload is nil. To check, you can put the following in your view
<%= debug #parent %>
and the same for #upload and see which is nil. However, I'm guessing that #upload is nil, because of this line in your controller:
# UploadsController#new
#upload = #parent.uploads.new unless #uploads.blank?
specifically the unless #uploads.blank? part. Unless you initialize it in the ApplicationController, #uploads is always nil, which means #uploads.blank? will always be true, which in turn means #upload will never be initialized. Change the line to read
#upload = #parent.uploads.new
and the problem will hopefully be resolved. The same is true of the other methods where you have used unless #uploads.blank?.
On a semi-related note, in UploadsController#find_parent, you have this line
classes ||= []
because the variable is local to the find_parent method, you can be assured that it is not initialized, and should rather write classes = [].
Also, you have this line of code
return unless classes.blank?
right before the end of the method. Did you add that in so that you return from the method once #parent has been initialized? If so, that line should be inside the each block.
Further, since classes isn't used outside of the method, why define it at all? The code could read as follows and still have the same behaviour
def find_parent
params.each do |name ,value|
#parent = $1.pluralize.classify.constantize.find(value) if name =~ /(.*?)_id/
return if #parent
end
end
Amongst other things, you'll see that this does a few things:
Avoids initializing a variable that is not needed.
Inlines the if statement, which helps readability for single line conditionals
Changes use of unless variable.blank to if variable. Unless your variable is a boolean, this accomplishes the same thing, but reduces the cognitive load, as the former is essentially a double negative which your brain has to parse.
-- EDIT - from email exchange about the issue --
You are correct - if #parent will return true if parent is initialized. As I mentioned on SO however, the exception to this is if #parent is initialized and set to false. Essentially what it means is that in Ruby, all values except nil and false are considered true. When an instance variable has not been initialized, it's default value is nil, which is why that line of code works. Does that make sense?
In terms of setting #parent in each action that renders form in the UsersController, which of these is the correct way to do this on the index action. I have tried all 3 but got errors
Remember that both #parent and #upload must be instances of ActiveRecord (AR) objects. In the first case, you set #parent to User.all, which is an array of AR objects, which will not work. Also, you try to call #parent.uploads before #parent is initialized, which will give a no method error. However, even if you were to swap the two lines around, you are calling #parent.uploads when parent is an array. Remember that the uploads method is defined on individual AR objects, and not on an array of them. Since all three of your implementations of index do similar things, the above caveats apply to all of them in various forms.
users_controller.rb
def index
#upload = #parent.uploads
#parent = #user = User.all
end
or
def index
# #user = #parent.user.all
#parent = #user = User.all
end
or
def index
#parent = #upload = #parent.uploads
#users = User.all
end
I'll quickly walk you through the changes I made. Before I start, I should explain that this
<%= render "partial_name", :variable1 => a_variable, :variable2 => another_variable %>
is equivalent to doing this
<%= render :partial => "partial_name", :locals => {:variable1 => a_variable, :variable2 => another_variable} %>
and is just a shorter (and somewhat cleaner) way of rendering. Likewise, in a controller, you can do
render "new"
instead of
render :action => "new"
You can read more about this at http://guides.rubyonrails.org/layouts_and_rendering.html Now on to the code.
#app/views/users/_form.html.erb
<%= render :partial => "uploads/uploadify" %>
<%= form_for [parent, upload], :html => { :multipart => true } do |f| %>
<div class="field">
<%= f.label :document %><br />
<%= f.file_field :document %>
</div>
<div class="actions">
<%= f.submit "Upload"%>
</div>
<%end%>
On the uploads form, you'll see that I changed #parent and #upload to parent and upload. This means you need to pass the variables in when you render the form instead of the form looking for instance variable set by the controller. You'll see that this allows us to do the following:
#app/views/users/index.html.erb
<h1>Users</h1>
<table>
<% #users.each do |user| %>
<tr>
<td><%= link_to user.email %></td>
<td><%= render 'uploads/form', :parent => user, :upload => user.uploads.new %></td>
</tr>
<% end %>
</table>
Add an upload form for each user in UsersController#index. You'll notice that because we now explicitly pass in parent and upload, we can have multiple upload forms on the same page. This is a much cleaner and more extensible approach to embedding partials, as it becomes immediately obvious what parent and upload are being set to. With the instance variable approach, people unfamiliar with the code base might struggle to determine where #parent and #upload are being set, etc.
#app/views/users/show.html.erb
<div>
<% #user.email %>
<h3 id="photos_count"><%= pluralize(#user.uploads.size, "Photo")%></h3>
<div id="uploads">
<%= image_tag #user.upload.document.url(:small)%>
<em>on <%= #user.upload.created_at.strftime('%b %d, %Y at %H:%M') %></em>
</div>
<h3>Upload a Photo</h3>
<%= render "upload/form", :parent => #user, :upload => user.uploads.new %>
</div>
This is similar to the changes above, where we pass in the parent and upload objects.
#config/routes.rb
Uploader::Application.routes.draw do
resources :users do
resources :uploads
end
devise_for :users
resources :posts do
resources :uploads
end
root :to => 'users#index'
end
You'll see that I removed uploads as a top level resources in the routes. This is because uploads requires a parent of some sort, and so cannot be top level.
#app/views/uploads/new.html.erb
<%= render 'form', :parent => #parent, :upload => #upload %>
I made the same changes as above, passing parent and upload through explicitly. You'll obviously need to do this wherever you render the form.
#app/controllers/users_controller.rb
class UsersController < ApplicationController
respond_to :html, :js
def index
#users = User.all
end
def show
#user = User.find(params[:id])
end
def new
#user = User.new
end
def create
#user = User.new(params[:user])
if #user.save
redirect_to users_path
else
render :action => 'new'
end
end
def update
#user = User.find_by_id(params[:id])
#user.update_attributes(params[:user])
respond_with(#user)
end
def destroy
#user = User.find_by_id(params[:id])
#user.destroy
respond_with(#user)
end
end
I've removed any mention of #parent from the user controller, as we pass it through explicitly.
Hopefully that all makes sense. You can extrapolate from these examples and pass through the parent and upload object wherever you want to render an upload form.
[#parent, Upload] => [#parent, :upload]
<%= form_for [#parent, :upload], :html => { :multipart => true } do |f| %>
UPD
You should change places :upload and #parent
<%= form_for [:upload, #parent], :html => { :multipart => true } do |f| %>