Facing issue in fetching state names using carmen gem - ruby-on-rails

Hi I am using gem 'carmen-rails' in my app I am having this type of association
class Employee < ActiveRecord::Base
has_one :contact
has_one :permanent_address
accepts_nested_attributes_for :contact, :permanent_address
end
and in my employess_controller.rb I have define like this:
def subregion_options
render partial: 'subregion_select'
end
and in my routes :
resources :employees do
collection do
get 'subregion_options'
end
end
In my employees/new.html.erb
<%= form_for #employee do |f| %>
<label class="form_label">Email</label>
<%= f.text_field :email %>
<label class="form_label">Set Ex Employee</label>
<%= f.check_box :is_active %>
<%= f.fields_for :contact do |c| %>
<label class="form_label">Address 1</label>
<%= c.text_field :current_address1 %>
<%= c.country_select :country,:prompt => 'Select Country' %>
<%= render partial: 'subregion_select', locals: {parent_region: c.object.country} %>
<% end %>
<%= f.submit %>
<% end %>
in employees/_subregion_select.html.erb
<div id="order_state_code_wrapper">
<% parent_region ||= params[:parent_region] %>
<% country = Carmen::Country.coded(parent_region) %>
<% if country.nil? %>
<em>Please select a country above</em>
<% elsif country.subregions? %>
<%= subregion_select(:contact, :state_code, parent_region) %>
<% else %>
<%= text_field(:contact, :state_code) %>
<% end %>
</div>
In js file
$('select#employee_contact_attributes_country').change(function() {
selectWrapper = $('#order_state_code_wrapper');
countryCode = $(this).val();
url = "/employees/subregion_options?parent_region=" + countryCode;
selectWrapper.load(url);
});
But its not fetching any states. Please guide how to solve this. Thanks in advance.

Related

Rails - Couldn't find Student with 'id'=

I'm getting the error above when I try to create a a 'mark' for a 'student'. I can't figure out how to pass the :student_id when I create a new mark.
Routes
Rails.application.routes.draw do
resources :students do
resources :marks
end
resources :marks
root 'students#index'
Marks Controller
class MarksController < ApplicationController
def create
#student = Student.find(params[:student_id])
#mark = #student.marks.create(params[:input1, :input2, :input3, :weight1, :weight2, :weight3, :mark1, :mark2, :mark3, :final_mark].permit(:input1, :input2, :input3, :weight1, :weight2, :weight3, :mark1, :mark2, :mark3, :final_mark))
#mark.save
if #mark.save
redirect_to student_path(#student)
else
render 'new'
end
end
def new
#mark = Mark.new
end
private
def set_mark
#mark = Mark.find(params[:id])
end
end
Students Show View
<p id="notice"><%= notice %></p>
<p>
<strong>Student Number</strong>
<%= #student.StudentNumber %>
</p>
<p>
<strong>Project Title</strong>
<%= #student.ProjectTitle %>
</p>
<p>
<strong>Project PDF</strong>
<%= #student.ProjectTitle %>
</p>
<p>
<strong>Reader 1</strong>
<%= #student.Reader1 %>
</p>
<p>
<strong>Reader 2</strong>
<%= #student.Reader2 %>
</p>
<h3> <%= link_to 'Add Mark', new_student_mark_path(#student), class:"btn btn-warning"%> </h3>
<p>
<strong>Reader 3</strong>
<%= #student.Reader3 %>
</p>
<%= link_to 'Edit', edit_student_path(#student) %> |
<%= link_to 'Back', students_path %>
Marks Form
<%= form_for #mark, html: {multipart: true} do |f| %>
<% if #mark.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#mark.errors.count, "error") %> prohibited this grading from being saved:</h2>
<ul>
<% #mark.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label 'Introduction' %><br>
<%= f.text_area :input1 %>
<%= f.number_field :weight1 %>
<%= f.number_field :mark1 %>
</div>
<div class="field">
<%= f.label 'Main' %><br>
<%= f.text_area :input2 %>
<%= f.number_field :weight2 %>
<%= f.number_field :mark2 %>
</div>
<div class="field">
<%= f.label 'Conclusion' %><br>
<%= f.text_area :input3 %>
<%= f.number_field :weight3 %>
<%= f.number_field :mark3 %>
</div>
<div class="actions">
<%= f.submit class:"btn-xs btn-success"%>
</div>
<% end %>
Mark model
class Mark < ActiveRecord::Base
belongs_to :student
end
Student Model
class Student < ActiveRecord::Base
has_many :marks
has_attached_file :document
validates_attachment :document, :content_type => { :content_type => %w(application/pdf) }
end
It's probably something really stupid but if anyone could explain the problem I'd be really grateful.
Thanks
I don't suggest you using hidden fields for this purpose.
You should pass student together with mark into form_for helper and rails will generate proper url for you which will look like: /students/:student_id/marks
In this case it will be possible to extract student_id from params in your action later.
form_for [#student, #mark], html: {multipart: true} do |f|
More information about nested resources:
http://guides.rubyonrails.org/routing.html#nested-resources
http://www.informit.com/articles/article.aspx?p=1671632&seqNum=7
https://gist.github.com/jhjguxin/3074080
UPDATE:
Forgot to mention that in order to make this work you need to pass student instance into your template at new action:
def new
#student = Student.find(params[:student_id])
#mark = #student.marks.build
end

How to edit model on show view of other model

I have the model Customer, and binded by has_many model Location. This is customer and his locations (adresses). After creating a new customer application redirects on show view of Customer model (shows all data just entered). On show view I have form for location add. There are already entered locations shows on this view and each of them has the link to edit each separate location. I need, when user click on location edit link, he redirects to show view of Customer model, but the form already contains the data of clicked location. Show view code of Customer model:
<p>Customer info:<br>
<strong><%= #customer.name %></strong><br />
<%= #customer.kind.name %><br />
<%= #customer.adres %><br />
<%= #customer.phone %><br />
<%= #customer.comment %><br />
</p>
<h3>Delivery location:</h3>
<% if #locations %>
<ul>
<% #locations.each do |loc| %>
<li>
<%= loc.adres %>
<%= loc.phone %>
<%= loc.comment %>
</li>
<% end %>
</ul>
<% end %>
<%= form_for Location.new do |l| %>
<%= l.text_field :adres %><br>
<%= l.text_field :phone %><br>
<%= l.text_field :comment %><br>
<%= l.text_field :customer_id, type: "hidden", value: #customer.id %><br>
<%= l.submit "Add" %>
<% end %>
<%= link_to "Home", root_path %>
You'd just set #location depending on whether specific params have been sent:
#app/controllers/customers_controller.rb
class CustomersController < ApplicationController
def show
#customer = Customer.find params[:id]
#location = params[:location_id] ? #customer.locations.find(params[:location_id]) : #customer.locations.new
end
end
This would allow you to populate the form as follows:
#app/views/customers/show.html.erb
Customer info:<br>
<%= content_tag :strong, #customer.name %>
<%= #customer.kind.name %>
<%= #customer.adres %>
<%= #customer.phone %>
<%= #customer.comment %>
<h3>Delivery location:</h3>
<% if #customer.locations %>
<ul>
<% #customer.locations.each do |loc| %>
<%= content_tag :li do %>
<%= loc.adres %>
<%= loc.phone %>
<%= loc.comment %>
<%= link_to "Edit", customer_path(#customer, location_id: loc.id) %>
<% end %>
<% end %>
</ul>
<% end %>
<%= form_for #location do |l| %>
<%= l.text_field :adres %><br>
<%= l.text_field :phone %><br>
<%= l.text_field :comment %><br>
<%= l.text_field :customer_id, type: "hidden", value: #customer.id %><br>
<%= l.submit %>
<% end %>
--
To manage the routes (to pass location_id), you'll be best creating a custom route:
#config/routes.rb
resources :customers do
get ":location_id", on: :member, action: :show, as: :location #-> url.com/customers/:id/:location_id
end
This will mean you'll have to reference the other link in your view:
<%= link_to "Edit", customer_location_path(#customer, loc.id) %>

Rails update action does not update boolean field

In my Rails app I have a nested form_for in my show action. This form is the same as the one in the edit action, but it has different fields.
Category -> Task -> completed (boolean, check_box) is what I am trying to update, but it doesn't. Although, if I do Category -> Task -> name (string, text_field) it updates fine.
This does NOT work
<%= form_for check_list do |f| %>
<%= f.error_messages %>
<% count = 0 %>
<ol>
<%= f.fields_for :tasks do |task| %>
<li>
<%= task.label :completed, check_list.tasks[count].name %>
<%= task.check_box :completed %>
</li>
<% count += 1 %>
<% end %>
</ol>
<p><%= f.submit 'Update' %></p>
<% end %>
This works
<%= form_for check_list do |f| %>
<%= f.error_messages %>
<% count = 0 %>
<ol>
<%= f.fields_for :tasks do |task| %>
<li>
<%= task.label :name, check_list.tasks[count].name %>
<%= task.text_field :name %>
</li>
<% count += 1 %>
<% end %>
</ol>
<p><%= f.submit 'Update' %></p>
<% end %>
This is a partial, and check_list is a variable I'm passing
Edit:
Here is the source for my models:
class CheckList < ActiveRecord::Base
has_many :tasks, :dependent => :destroy
accepts_nested_attributes_for :tasks, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true
# Validations
validates :name, :presence => true
end
class Task < ActiveRecord::Base
belongs_to :check_list
end
I'd suggest adding the name as a hidden field on the form, as Rails probably updates all the fields that are passed into the Action.
<%= form_for check_list do |f| %>
<%= f.error_messages %>
<% count = 0 %>
<ol>
<%= f.fields_for :tasks do |task| %>
<li>
<%= task.label :completed, check_list.tasks[count].name %>
<%= task.check_box :completed %>
<%= task.hidden_field :name %>
</li>
<% count += 1 %>
<% end %>
</ol>
<p><%= f.submit 'Update' %></p>
<% end %>
<% end %>
</ol>
<p><%= f.submit 'Update' %></p>
<% end %>

Rails: has_many :through _ids nested?

On a single page I might have a single Video and then checkboxes to add multiple dogs to it. Easy enough (as follows)...
View:
<%= check_box_tag "video[dog_ids][]", dog.id %>
Controller:
params[:video][:dog_ids] ||= []
But what I can't figure out how to do is have multiple videos, each with multiple dogs.
I currently have this:
<% #videos.each do |video| %>
<%= fields_for "item[]", video do |f| %>
<%= f.hidden_field :id, :index => nil %>
<%= f.text_field :title, :index => nil%>
<%= f.text_area :body, :index => nil %>
<% video.dogs.each do |dog| %>
<%= check_box_tag "item[][video[dog_ids][]]", dog.id %>
<% end %>
<% end %>
<% end %>
But when I do that, dogs_ids is always nil when it's submitted.
Any idea what I'm doing wrong?
Such a setup with fields_for "item[]" and f.text_field :title, :index => nil produces:
<input id="item__name" name="item[][title]" size="30" type="text" value="vid1">
This indicates that the checkbox name should be item[][dog_ids][].
A working example:
<% #videos.each do |video| %>
<%= fields_for "item[]", video do |f| %>
<%= f.text_field :title, :index => nil %>
<% video.dogs.each do |dog| %>
<%= check_box_tag "item[][dog_ids][]", dog.id %> <%= Dog.name %>
<% end %>
<br>
<% end %>
<% end %>
This produces result in params:
{"item"=> [
{"title"=>"vid1", "dog_ids"=>["1", "2"]},
{"title"=>"vid2", "dog_ids"=>["2"]},
{"title"=>"vid3", "dog_ids"=>["2", "3"]}
]}

Rails 3 has_one/has_many question

I am writing an application that contains a database with several tables and joining tables and so forth... the two I am working with currently (and am stumped on) are my pages table and my templates table.
Now a page can contain only one template, but a template can have many pages.
Model for Page:
class Page < ActiveRecord::Base
has_one :template
accepts_nested_attributes_for :template
end
Model for Template:
class Template < ActiveRecord::Base
has_many :pages
end
When a user creates a page, I want them to be able to select a layout, but for some reason the select list is not showing up
HTML for show:
<%= form_for(#page) do |page| %>
<% if #page.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#page.errors.count, "error") %> prohibited this page from being saved:</h2>
<ul>
<% #page.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= page.label "Page title" %><br />
<%= page.text_field :slug %>
</div>
<div class="field">
<%= page.label :active %>?<br />
<%= page.check_box :active %>
</div>
<%= page.fields_for :category do |cat| %>
<%= cat.label :category %>
<%= select :page, :category_id, Category.find(:all).collect{|c| [c.name, c.id] } %>
<% end %>
<%= page.fields_for :template do |temp| %>
<%= temp.label :template %>
<%= select :page, :template_id, Template.find(:all).collect{|t| [t.content, t.id] } %>
<% end %>
<div class="actions">
<%= submit_tag %>
</div>
<% end %>
Any reasons why the last select wouldn't show up?
Thanks in advance for all the help!
Edit:
All I had to do to fix the problem was put the Model logic in my controller and then call that object in the view and it worked
Controller:
def new
#page = Page.new
#categories = Category.find(:all)
#templates = Template.find(:all)
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #page }
end
end
View:
<div class="field">
<%= page.label :template %>
<%= page.select("template_id", #templates.collect { |t| [t.content, t.id] }, :include_blank => 'None') %>
</div>
Hope this helps someone else!
First page could "belongs to" template:
class Page < ActiveRecord::Base
belongs_to :template
accepts_nested_attributes_for :template
end
And instead of:
<%= page.fields_for :template do |temp| %>
<%= temp.label :template %>
<%= select :page, :template_id, Template.find(:all).collect{|t| [t.content, t.id] } %>
<% end %>
I would use a simple collection_select:
<%= page.select("template_id", Template.all.collect {|t| [ t.contet, t.id ] }) %>

Resources