Hi i have a simple form for updating a user's account here is my controller
def edit
#users = User.find(params[:id])
end
here is my view
<%= form_for :user do |f| %>
<%= f.label :first_name %> <br />
<%= f.text_field :name ,:value => #users.name%><br />
<%= f.label :last_name %> <br />
<%= f.text_field :lname ,:value => #users.lname%><br />
<%= f.label :email %> <br />
<%= f.email_field :email ,:value => #users.email%><br />
<%= f.label :id %> <br />
<%= f.text_field :id ,:value => #users.id%><br />
<br />
<%= f.submit ({:confirm => "Are you sure?"}) %>
<% end %>
when i click submit i get the following mongoid error
Mongoid::Errors::InvalidFind in UsersController#edit
Problem:
Calling Document.find with nil is invalid.
Summary:
Document.find expects the parameters to be 1 or more ids, and will return a single document if 1 id is provided, otherwise an array of documents if multiple ids are provided.
Resolution:
Most likely this is caused by passing parameters directly through to the find, and the parameter either is not present or the key from which it is accessed is incorrect.
I'm fairly novice with rails and this is my first stab with mongoDB. Any input would help, Thank you.
Try to use this form
<%= form_for(#users) do |f| %>
...
...
<% end %>
The error comes because you are using :user that invalid find in your user controller
Related
I have the following simple rails page:
<h1> New User </h1>
<%= form_for :user, url:users_path do |f| %>
<p>
<%= f.label :email %>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %>
<%= f.text_field :password %>
</p>
<%= f.submit %>
<% end %>
The create action gets executed and I want to access the :email attribute.
def create
render text: params[:email].inspect
end
The above always displays nil.
form_for :user will place all parameters beneath a :user key
render text: params[:user][:email].inspect
In creating a new application I have used marklocklear example (https://github.com/marklocklear/devise_multi_tentant) for assistance in creating a multi-tenant environment. It works well.
The next thing I wanted to do I thought would be simple but has turned into quite the niggle for me.
All I wanted to do is rather than having the initial admin user have to enter an organization name (which is not necessary in my application), I wanted to hide that field.
Sign Up Form (that works):
<h2>Sign up</h2>
<% resource.organization ||= Organization.new %>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<%= f.fields_for :organization do |org| %>
<div><%= 'Organization or Company Name' %><br />
<%= org.text_field :name %></div>
<% end %>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render :partial => "devise/shared/links" %>
So, I did this to try to hide the field:
<%= f.fields_for :organization do |org| %>
<%= f.hidden_field :name %>
<% end %>
But I get an error.
The baffling thing to me is that if I leave the field in, but do not enter a value everything still works properly -- this is unexpected. Since (as part of my troubleshooting) I removed the field completely -- and got an error message stating the form could not be processed because the organization part was blank.
Any assistance would be appreciated.
Max: Thank you. As you know, that worked like a charm. The field is now hidden and the form submits properly. Thanks.
<%= f.fields_for :organization do |org| %>
<%= org.hidden_field :name %>
<% end %>
My nested model form is now working, but I am having trouble displaying the data in a view. How do I display nested model data with a one-to-many relationship? Any help will be greatly appreciated.
Here's my form and controller:
<%= form_for #account do |f| %>
<%= f.label :account_type %><br />
<%= f.text_field :account_type %><br />
<%= f.fields_for :organizations do |builder| %>
<%= builder.label :name %><br />
<%= builder.text_field :name %><br />
<%= builder.label :website %><br />
<%= builder.text_field :website %><br />
<%= builder.fields_for :locations do |lb| %>
<%= lb.label :phone %><br />
<%= lb.text_field :phone %><br />
<%= lb.label :toll_free_phone %><br />
<%= lb.text_field :toll_free_phone %><br />
<%= lb.label :fax %><br />
<%= lb.text_field :fax %><br />
<%= lb.fields_for :addresses do |ab| %>
<%= ab.label :address1 %><br />
<%= ab.text_field :address1 %><br />
<%= ab.label :address2 %><br />
<%= ab.text_field :address2 %><br />
<%= ab.label :city %><br />
<%= ab.text_field :city %><br />
<%= ab.label :state %><br />
<%= ab.text_field :state %><br />
<%= ab.label :zip %><br />
<%= ab.text_field :zip %><br />
<% end %>
<% end %>
<% end %>
<%= f.submit "Add account" %>
<% end %>
class AccountsController < ApplicationController
def show
#account = Account.find(params[:id])
#organization = #account.organizations
end
def new
#account = Account.new
organization = #account.organizations.build
location = organization.locations.build
location.addresses.build
#header = "Create account"
end
def create
#account = Account.new(params[:account])
if #account.save
flash[:success] = "Account added successfully"
render 'show'
else
render 'new'
end
end
end
In general, how do I reference nested model data in a view when there is one-to-many relationship? Do I need to specify the child with some type of "where clause" like method?
Here is a simple example show.html.erb where I am trying to display the Name of the Organization that I just created. It doesn't work.
<h1><%= #organization.name %></h1>
The render 'show' action after creating an Account with the above form results in this error:
NoMethodError in Accounts#create
Showing C:/Documents and Settings/Corey Quillen/My
Documents/rails_projects/shop_manager/app/views/accounts/show.html.erb where line #1
raised:
undefined method `name' for nil:NilClass
Extracted source (around line #1):
1: <h1><%= #organization.name %></h1>
Rails.root: C:/Documents and Settings/Corey Quillen/My
Documents/rails_projects/shop_manager
Application Trace | Framework Trace | Full Trace
app/views/accounts/show.html.erb:1:in
`_app_views_accounts_show_html_erb__790921876_14235864__946051513'
app/controllers/accounts_controller.rb:21:in `create'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"y59rGAhS+kqfH3v3axhlYuxvBbBxIWXg0yucCFwfBq8=",
"account"=>{"account_type"=>"dfdf",
"organizations_attributes"=>{"0"=>{"name"=>"dfdf",
"website"=>"dfdf",
"locations_attributes"=>{"0"=>{"phone"=>"dfdf",
"toll_free_phone"=>"dfd",
"fax"=>"",
"addresses_attributes"=>{"0"=>{"address1"=>"",
"address2"=>"",
"city"=>"",
"state"=>"",
"zip"=>""}}}}}}},
"commit"=>"Add account"}
You say #organization = #account.organizations in your show action.
Just what are you expecting #organization to contain?
Think about it. It's going to be an array not a single organization so loop through that and get the name for each organization
Actually I think you haven't thought your relationships through properly surely an account belongs_to an organisation. Do you really want an account to be associated with more than one organisation ?
UPDATE - Ref comment below
That is totally possible to do but you need to decide what the business logic is that determines which organisation needs to be displayed here.
If you can explain exactly how your relationships are supposed to work it shouldn't be too difficult to show you how to apply your logic
UPDATE - How to get the primary organisation
This is simply a matter of setting up a new association on the Account model
has_one :primary_organization.
:class_name => 'Organization',
:conditions => ['primary = ?', true]
Then in your show action just write
#account.primary_organization.first #because anything use on a find other than the primary key will always return an array even if there is only one record.
You might also want to check that primary_organization is not empty?
Consider refactoring that has_one into a class method. May not be necessary depending on your needs
Read more here http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
As your relation is one-to-many, you have to use
#organization.first.name
it will show first organization.
as #organization is a array you have to collect all names and then show.
Hallo
rails version 2.3.5
I'm learning rails and I run into a problem.
I'm doing some nesting forms from the railscasts tutorials. I changed the text area into a data field to upload photos and everything is working.
Now i have to display the uploaded pictures and i simply can't do it. I Tried everything I could find on the net but nothing worked.
PROBLEM
I have the Article controller which handles the article CRUD.
inside the article new form there is nested a form for uploading images.
article controller
def code_image
#image_data = Photo.find(params[:id])
#image = #image_data.binary_data
send_data(#image, :type => #image_data.content_type,
:filename => #image_data.filename,
:disposition => 'inline')
end
photo model
def image_file=(input_data)
self.filename = input_data.original_filename
self.content_type = input_data.content_type.chomp
self.binary_data = input_data.read
end
articles/show.html.erb
<%=h #article.title %>
<%=h #article.body %>
<% for photos in #article.photos %>
<%= image_tag(url_for({:action => 'code_image',
:id => #article.photos.id})) -%>
<% end %>
articles/_formnew.html.erb
<% form_for (:article, #article,
:url => {:action=>'create'}, :html=>
{:multipart=>true}) do |f| %>
<%= f.label :title %><br />
<%= f.text_field :title %><br /><br />
<%= f.label :body %><br />
<%= f.text_area :body, :style => 'width: 600px;' %><br /><br />
<% f.fields_for :photos do |builder|%>
<%= builder.label :content, "Photo"%><br />
<%= builder.file_field :image_file %><br />
<% end %>
<br />
<%= f.submit "Create" %>
Thanks
Old question I know, but if somebody stumbles across it, this page helped me:
http://sleekd.com/general/adding-multiple-images-to-a-rails-model-with-paperclip/
Also, to display the image, you can just use:
image_tag [model].[paperclip_attachment].url
eg:image_tag trip_image.photo.url
This is basically a nested form question, albeit with only one field that belongs to a parent model. My data entry form collects data for a model - however I also need to collect one other a data element/value (UserID) that actually goes into a parent record that will be created with the detail record.
AFAIK Rails expects each form field to map to a model and I need to create an unbound data input field that I will use separately.
How can I override this default behaviour and create a'free form/unbound field'?
TIA,
BC
Heres something from my own app:
Access it by:
params[:company] and params[:user]
Controller:
#company = Company.new
#user = User.new
View:
<% form_for #company, :url => companies_path do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :website %><br />
<%= f.text_field :website %>
</p>
<hr />
<% fields_for #user do |u| %>
<p>
<%= u.label :email %><br />
<%= u.text_field :email %>
</p>
<p>
<%= u.label :username %><br />
<%= u.text_field :username %>
</p>
<p>
<%= u.label :password %><br />
<%= u.password_field :password %>
</p>
<p>
<%= u.label :password_confirmation %><br />
<%= u.password_field :password_confirmation %>
</p>
<% end %>
<p>
<%= f.submit "Submit" %>
</p>
<% end %>
For the "magic" form <=> model mapping form_for is used. (http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html)
If you need something out of the current model try using http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html
With that you can add tags separate from the model, eg
radio_button_tag
inside the form_for block