rails, creates an object - ruby-on-rails

Good Morning,
I have two scaffolds person and city.
rails g scaffold person :name, city_id
rails g scaffold city :cityname
and one formular view/people/
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :city_id %><br />
<%= f.text_field :city_id %>
</div>
<div class="field">
<%= f.label :cityname%><br />
<%= f.text_field :cityname%>
</div>
it doesnt go, i have to create an object of city and put the id in the city_id hidden_field and after that after create-button selected all should be saved in the database.
Not too hard or? Who would you make this?

No need to pass city_id.
<%= form_tag url_for(:controller => :your_controller, :action => :some_action, :method => :get do %>
<div class="field">
<%= label_tag :name %><br />
<%= text_field_tag :name %>
</div>
<div class="field">
<%= label_tag :cityname%><br />
<%= text_field_tag :cityname%>
</div>
<% end %>
In controller:
def some_action
city = City.find_or_create_by_cityname(params[:cityname])
person = Person.new(params[:name])
person.city_id = city.id
person.save!
end

Related

Type mismatch on association, f.select rails

I have a problem in my form, i want to create an entry in my "members" table, each member are connected to a "year" but I keep getting mismatches on the selection of the year.
This is my form:
<div class="field">
<%= f.label :name %><br>
<%= f.text_area :name %>
</div>
<div class="field">
<%= f.label :nickname %><br>
<%= f.text_area :nickname %>
</div>
<div class="field">
<%= f.label :year %>
<%= f.select :year, options_for_select(#years.all.map{|y| [y.year,y.id]}) %>
</div>
<div class="actions">
<%= f.submit %>
</div>
And here are the models:
class Member < ApplicationRecord
belongs_to :year
mount_uploader :image, ImageUploader
end
class Year < ApplicationRecord
has_many :members, dependent: :destroy
end
When I try to submit I get the following error:
Year(#70050157849460) expected, got "1" which is an instance of String(#9412380)
Where did I go wrong?
EDIT:
Here is the code for the controller
class MembersController < ApplicationController
def home
#members = Member.all
end
def new
#member = Member.new
#years = Year.all
end
def create
#member = Member.new(member_params)
if #member.save
flash[:success] = "Member Created"
redirect_to root_path
else
render 'form'
end
end
private
def member_params
params.require(:member).permit(:name,:nick,:position,:image,:year)
end
end
<div class="field">
<%= f.label :name %><br>
<%= f.text_area :name %>
</div>
<div class="field">
<%= f.label :nickname %><br>
<%= f.text_area :nickname %>
</div>
<div class="field">
<%= f.label :year %>
<%= f.select :year_id, options_for_select(#years.all.map{|y| [y.year,y.id]}) %>
</div>
<div class="actions">
<%= f.submit %>
</div>
or
<div class="field">
<%= f.label :name %><br>
<%= f.text_area :name %>
</div>
<div class="field">
<%= f.label :nickname %><br>
<%= f.text_area :nickname %>
</div>
<div class="field">
<%= f.label :year %>
<%= f.select :year, options_for_select(#years.all.map{|y| [y.year,y]}) %>
</div>
<div class="actions">
<%= f.submit %>
</div>
This second I did not check but first will work you need to say year_id because when you did map on collection you set id as parameter that is being passed
but on form select you basically told form to expect active record object.

Devise multiple step form for registration

I want the user to be able to register in two steps as I have many fields. Ideally first step would be to accept email and password. As user enter it, they can proceed to fill the next step.
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :first_name %>
<%= f.text_field :first_name, autofocus: true %>
</div>
<div class="field">
<%= f.label :last_name %>
<%= f.text_field :last_name %></br>
</div>
<div class="field">
<%= f.label :city %>
<%= f.text_field :city %>
</div>
<div class="field">
<%= f.label :address %>
<%= f.text_area :address %>
</div>
<div class="field">
<%= f.label :gender %>
<span class="option">Male</span><%= f.radio_button :gender, "m" %>
<span class="option">Female</span><%= f.radio_button :gender, "f" %></br>
</div>
<div class="field">
<%= f.label :mobile_no %></br>
<%= f.telephone_field :mobile_no %>
</div>
<div class="field">
<%= f.label :website %>
<%= f.url_field :website %>
</div>
<div class="field">
<%= f.label :email %>
<%= f.email_field :email %>
</div>
<div class="field">
<%= f.label :password %>
<% if #validatable %>
<em>(<%= #minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :skills %>
<%= f.text_field :skills %>
</div>
<div class="field">
<%= f.label :passion %>
<%= f.text_field :passion %>
</div>
<div class="field">
<%= f.label :description %>
<%= f.text_area :description %>
</div>
<% end %>
I tried the approach where user enters email and password and after that they are redirected to edit page where they can update other fields.
class RegistrationsController < Devise::RegistrationsController
def after_sign_up_path_for(resource)
redirect_to edit_user_path(resource)
end
end
routes.rb
devise_for :users, :pro, :amateur, controllers: { registrations: "registrations" }
However, the redirect doesn't seem to work. I also tried after_inactive_sign_up_path_for as I am using confirmable, still not working.
I can't seem to figure out why this could be also I would like to know if there are any other approach without using any gem?
You could use javascript if you're just interested in user interface. Or if it's important that you're saving all the info as you're going, you can have the form split into partials and override the devise registration controller to render those separate partials.
You will also need to add steps that link with the partials in your user model. I think if you used a gem, this would probably be main part that it will do for you.
this is a great railscast on how to have a multistep form without any gems. the only difference is that you'll need to override the create method in devise

Nested Form Error - ArgumentError (wrong number of arguments (0 for 1)):

I followed this screencast to make a nested model form.
http://railscasts.com/episodes/196-nested-model-form-part-1
Now, I am trying my application to include such a nested model form but I received a argument error (wrong number of arguments(0 for 1)).
I can't seem to figure out where I went wrong and would like to seek some advice on what I could try out and why some an error might have occurred.
The error happens on this line in the Subject Model.
has_many :lessons, :dependent => destroy
The other relevant codes:
Subjects Controller:
def new
#subject = Subject.new
#3 times one for lecture one for lab one for tut.
3.times{#subject.lessons.build}
respond_to do |format|
format.html # new.html.erb
format.json { render json: #subject }
format.js
end
end
Subject Model
class Subject < ActiveRecord::Base
has_many :lessons, :dependent => destroy
attr_accessible :lesson_attributes, :acad_unit, :cohort_size, :discipline, :remarks, :subject_code, :subject_name, :year_of_study
accepts_nested_attributes_for :lessons, :reject_if => lambda { |a| a[:lesson_type].blank? }, :allow_destroy => true
end
Lesson Model
class Lesson < ActiveRecord::Base
belongs_to :subject
attr_accessible :frequency, :lesson_type, :no_of_lesson, :possible_venues
end
_form.html.erb
<%= form_for(#subject,:remote=>true) do |f| %>
<% if #subject.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#subject.errors.count, "error") %> prohibited this subject from being saved:</h2>
<ul>
<% #subject.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :subject_code %><br />
<%= f.text_field :subject_code %>
</div>
<div class="field">
<%= f.label :subject_name %><br />
<%= f.text_field :subject_name %>
</div>
<div class="field">
<%= f.label :year_of_study %><br />
<%= f.text_field :year_of_study %>
</div>
<div class="field">
<%= f.label :discipline %><br />
<%= f.text_field :discipline %>
</div>
<div class="field">
<%= f.label :acad_unit %><br />
<%= f.text_field :acad_unit %>
</div>
<div class="field">
<%= f.label :cohort_size %><br />
<%= f.text_field :cohort_size %>
</div>
<div class="field">
<%= f.label :remarks %><br />
<%= f.text_field :remarks %>
</div>
<ol>
<%= f.fields_for :lessons do |builder| %>
<%= render "lesson_fields", :f => builder %>
<% end %>
</ol>
<% end %>
_lesson_fields.html.erb
<p>
<div class="field">
<%= f.label :lesson_type %><br />
<%= f.text_field :lesson_type %>
</div>
<div class="field">
<%= f.label :no_of_lesson %><br />
<%= f.text_field :no_of_lesson %>
</div>
<div class="field">
<%= f.label :frequency %><br />
<%= f.text_field :frequency %>
</div>
<div class="field">
<%= f.label :possible_venues %><br />
<%= f.text_field :possible_venues %>
</div>
</p>
I guess you wanted :
has_many :lessons, :dependent => :destroy
?
Here you'll find a nice discussion on topic.

Problem with Ruby Gem Paperclip

I am having trouble using the Ruby Gem paperclip. I followed the instructions in the ReadMe but I cannot seem to get it to actually load my images. Here is my edit form:
<% form_for :user, #user, :html => { :multipart => true } do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :expertise %><br />
<%= f.text_area :expertise, :class => "expertise" %>
</div>
<div class="field">
<%= f.label :occupation %><br />
<%= f.text_field :occupation %>
</div>
<div class="field">
<%= f.label :city %><br />
<%= f.text_field :city %>
</div>
<div class="field">
<%= f.label :state %><br />
<%= f.text_field :state %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation, "Confirmation" %><br />
<%= f.password_field :password_confirmation %>
</div>
<%end%>
Yet when I try to save I keep getting this error: No route matches "/users/4/edit"
What is the problem
The error is telling you that there's no /users/4/edit route. What does your config/routes.rb look like? If there's a line like:
resources :users
Then, try changing that first line to:
form_for #user
Instead of:
form_for :user, #user
Also, I don't see a file_field in there anywhere so I don't think this question is about Paperclip?

Rails - User Input for Multiple models on a single form - How

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

Resources