Ruby on Rails: Form won't create hidden_field - ruby-on-rails

I'm trying to create a form with a helper in Rails but all my fields aren't created. I have a helper that I include in my view (I'll include them both). But since I'm new to rails I'm not even sure I'm doing this the right way.
When i write it like this it at first looks like it works since the button is created but when i click it no values are passed and an empty row is created in the DB (except for id and timestamp).
users_helper.rb
module UsersHelper
def sub_button(u)
#current_user = User.find session[:user_id]
#temp_user = u
#sub = Subscription.where("userID = ? AND followingID = ?", #current_user.id, #temp_user.id)
if #sub.blank?
#following = false
else
#following = true
end
if(u.username != #current_user.username)
if #following
form_for(:subscription, :url => { :controller => "subscriptions", :action => "unsubscribe" }) do |s|
s.hidden_field(:userID, :value => #current_user.id)
s.hidden_field(:followingID, :value => u.id)
s.submit "Unfollow"
end
else
form_for(:subscription, :url => { :controller => "subscriptions", :action => "subscribe" }) do |s|
s.hidden_field(:userID, :value => #current_user.id)
s.hidden_field(:followingID, :value => u.id)
s.submit "Follow"
end
end
end
end
end
index.html.erb
<h2>All users</h2>
<table>
<tr>
<th>Username</th>
<th>Email</th>
</tr>
<% #user.each do |u| %>
<tr>
<td><%= u.username %></td>
<td><%= u.email %></td>
<td><%= sub_button(u) %></td>
</tr>
<% end %>
</table>
So I was thinking if I'm missing something to create the fields.. any clues?

I'm not sure, but I think this is how it's supposed to be organized:
module UserHelper
def sub_button u
#current_user = User.find session[:user_id]
#temp_user = u
#sub = Subscription.where("userID = ? AND followingID = ?", #current_user.id, #temp_user.id)
if #sub.blank?
#following = false
else
#following = true
end
if(u.username != #current_user.username)
if #following
render partial: 'shared/unfollow', locals: { current_user: #current_user, u: u }
else
render partial: 'shared/follow', locals: { current_user: #current_user, u: u }
end
end
end
views/shared/_unfollow.html.erb
<%= form_for(:subscription, url: { controller: "subscriptions", action: "unsubscribe" }) do |s| %>
<%= s.hidden_field(:userID, value: current_user.id) %>
<%= s.hidden_field(:followingID, value: u.id) %>
<%= s.submit "Unfollow" %>
<% end %>
views/shared/_follow.html.erb
<%= form_for(:subscription, url: { controller: "subscriptions", action: "subscribe" }) do |s| %>
<%= s.hidden_field(:userID, :value => current_user.id) %>
<%= s.hidden_field(:followingID, :value => u.id) %>
<%= s.submit "Follow" %>
<% end %>

Related

Rails has_and_belongs_to_many AND collection_check_boxes

So there isn't an error, my join table just isn't being populated with any information... #lead.districts returns District::ActiveRecord_Associations_CollectionProxy rather than the expected text...
Where have I made my mistake?
new.html.erb
<%= link_to("<< Back to List", {:action => 'index'}, :class => 'back-link') %>
<div class="subjects new">
<h2>Create Lead</h2>
<%= form_for(:lead, :url => {:action => 'create'}) do |f| %>
<table cellspacing="0">
<tr>
<th>First Name</th>
<td><%= f.text_field(:first_name) %></td>
</tr>
<tr>
<th>Last Name</th>
<td><%= f.text_field(:last_name)%></td>
</tr>
<tr>
<th>Phone Number</th>
<td><%= f.text_field(:phone_number)%></td>
</tr>
<tr>
<th>Email</th>
<td><%= f.text_field(:email)%></td>
</tr>
<tr>
<th>Move-in Date</th>
<td><%= f.date_field(:move_in_date, min: Date.today)%></td>
</tr>
<tr>
<th>Bedrooms</th>
<td><%= f.text_field(:beds)%></td>
</tr>
<tr>
<th>Bathrooms</th>
<td><%= f.text_field(:baths)%></td>
</tr>
<tr>
<th>Maximum Price Considered</th>
<td><%= f.text_field(:maxprice)%></td>
</tr>
<tr>
<th>Preferred Neighborhoods</th>
<td>
<%= collection_check_boxes(:lead, :district_ids, District.all.order("districts.name ASC"), :id, :name )%>
</td>
</tr>
<tr>
<th>Have you ever broken a lease?</th>
<td>
<%= f.label :broken_lease, "Yes", :value => true %>
<%= f.radio_button :broken_lease, true %>
<%= f.label :broken_lease, "No", :value => false %>
<%= f.radio_button :broken_lease, false %>
</td>
</tr>
<tr>
<th>Have you ever been convicted of a felony?</th>
<td>
<%= f.label :felon, "Yes", :value => true %>
<%= f.radio_button :felon, true %>
<%= f.label :felon, "No", :value => false %>
<%= f.radio_button :felon, false %>
</td>
</tr>
<tr>
<th>Do you have any pets?</th>
<td>
<%= f.label :pets, "Yes", :value => true %>
<%= f.radio_button :pets, true %>
<%= f.label :pets, "No", :value => false %>
<%= f.radio_button :pets, false %>
</td>
</tr>
<tr>
<th>If so, please provide a brief description</th>
<td><%= f.text_area(:pets_description)%></td>
</tr>
</table>
<div class="form-buttons">
<%= submit_tag("Create Lead") %>
</div>
<% end %>
</div>
leads_controller.rb
class LeadsController < ApplicationController
layout false
def index
#leads = Lead.order("leads.created_at DESC")
end
def show
#lead = Lead.find(params[:id])
end
def new
#lead = Lead.new
#districts = District.all.order("districts.name ASC")
end
def create
#lead = Lead.new(lead_params)
if #lead.save
redirect_to(:action => 'index')
else
render('new')
end
end
def edit
#lead = Lead.find(params[:id])
end
def update
#lead = Lead.find(params[:id])
if #lead.update_attributes(lead_params)
redirect_to(:action => 'show', :id => #lead.id)
else
render('edit')
end
end
def delete
#lead = Lead.find(params[:id])
end
def destroy
lead = Lead.find(params[:id]).destroy
flash[:notice] = "Lead '#{lead.id}' deleted successfully."
redirect_to(:action => 'index')
end
private
def lead_params
params.require(:lead).permit(:first_name, :last_name, :phone_number, :email, :move_in_date, :beds, :baths, :maxprice, :broken_lease, :felon, :pets, :pets_description, :district => [])
end
end
districts_controller.rb
class DistrictsController < ApplicationController
layout false
def index
#districts = District.order("districts.name ASC")
end
def show
#district = District.find(params[:id])
end
def new
#district = District.new
end
def create
#district = District.new(district_params)
if #district.save
flash[:notice] = "District created successfully."
redirect_to(:action => 'index')
else
render('new')
end
end
def edit
#district = District.find(params[:id])
end
def update
#district = District.find(params[:id])
if #district.update_attributes(district_params)
flash[:notice] = "District updated successfully."
redirect_to(:action => 'show', :id => #district.id)
else
render('edit')
end
end
def delete
#district = District.find(params[:id])
end
def destroy
district = District.find(params[:id]).destroy
flash[:notice] = "District '#{district.name}' deleted successfully."
redirect_to(:action => 'index')
end
private
def district_params
params.require(:district).permit(:name)
end
end
district.rb
class District < ActiveRecord::Base
has_and_belongs_to_many :leads
end
lead.rb
class Lead < ActiveRecord::Base
has_and_belongs_to_many :districts
end
Join table migration:
class CreateDistrictsLeadsJoin < ActiveRecord::Migration
def change
create_table :districts_leads, :id => false do |t|
t.integer "district_id"
t.integer "lead_id"
end
add_index :districts_leads, ["district_id", "lead_id"]
end
end
My error was in mass assigning my params (used :district should have been :district_ids)
Correct params function:
# leads_controller.rb
private
def lead_params
params.require(:lead).permit(:first_name, :last_name, :phone_number, :email, :move_in_date, :beds, :baths, :maxprice, :broken_lease, :felon, :pets, :pets_description, :district_ids => [])
end
You will need to add leads to district and districts to leads in both create methods.
def create # in LeadsController
#lead = Lead.new(lead_params)
if #lead.save
district = District.find(...) #find district to associate
district.leads << #lead #add lead to district
redirect_to(:action => 'index')
else
render('new')
end
end
#leads_controller.rb
def create
#lead = Lead.new(lead_params)
if #lead.save
selected_dis = params['lead_district_ids'] #[3,4,5]
districts = District.where(id: selected_dis).first
#check how you getting district params and use that to fetch orders.
districts.each do |d|
#lead.districts << d
#assumming your one lead can have many districts.
end
redirect_to leads_path
else
render :new
end
end

Error ActiveRecord::RecordNotFound (Couldn't find Planner without an ID):

There is something wrong with my code, and I couldn't figure out what is it. It's already one day I'm trying to solve this by googling and searching on Stack Overflow.
Errors:
Processing ClassTimingsController#show (for 127.0.0.1 at 2014-02-05 15:27:19) [POST]
Parameters: {"authenticity_token"=>"sRTUkx7slXEX+kmzNl4GbYSIFSyf1WGSP5fRB5+rPzY=",
"batch_id"=>"13", "_"=>"", "action"=>"show", "controller"=>"class_timings"}
[FedenaRescue] AR-Record_Not_Found Couldn't find Planner without an ID
ActiveRecord::RecordNotFound (Couldn't find Planner without an ID):
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1567:in `
find_from_ids'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:616:in `find'
app/controllers/class_timings_controller.rb:103:in `show'
I guess there is some problem with the "class_timings_controller.rb" in show.
def show
#planner = nil
if params[:planner_id] == ''
#class_timings = ClassTiming.find(:all, :conditions=>["planner_id is null and is_deleted = false"])
else
#class_timings = ClassTiming.active_for_planner(params[:planner_id])
#planner = Planner.find params[:planner_id] unless params[:id] == ''
end
respond_to do |format|
format.js { render :action => 'show' }
end
end
What is my mistake?
Below is the code for MVC.
Model (class_timing.rb)
class ClassTiming < ActiveRecord::Base
belongs_to :batch
belongs_to :planner
validates_presence_of :subject_title
validates_uniqueness_of :subject_title, :scope => [:planner_id , :batch_id, :is_deleted]
named_scope :for_batch, lambda { |b| { :conditions => { :batch_id => b.to_i, :is_deleted=>false, :checklist => false} } }
named_scope :default, :conditions => { :batch_id => nil, :checklist => false, :is_deleted=>false }
named_scope :active_for_batch, lambda { |b| { :conditions => { :batch_id => b.to_i, :is_deleted=>false} } }
named_scope :active, :conditions => { :batch_id => nil, :is_deleted=>false }
named_scope :for_planner, lambda { |p| { :conditons => { :planner_id => p.to_i, :is_deleted=>false, :checklist => false} } }
named_scope :default, :conditions => { :planner_id => nil, :checklist => false, :is_deleted=>false }
named_scope :active_for_planner, lambda { |p| { :conditions => { :planner_id => p.to_i, :is_deleted=>false} } }
named_scope :active, :conditions => { :planner_id => nil, :is_deleted=>false }
end
View
index
<div id="content-header">
<%= image_tag("/images/show_timetable.png") %>
<h1><%= t('teaching_schedule') %></h1>
<h3><%= t('create_new_teaching_schedule') %></h3>
<div id="app-back-button">
<%= link_to_function image_tag("/images/buttons/back.png", :border => 0), "history.back()" %>
</div>
</div>
<div id="page-yield">
<div id="flash_box"></div>
<% unless flash[:notice].nil? %>
<p class="flash-msg"> <%= flash[:notice] %> </p>
<% end %>
<div class="label-field-pair">
<label ><%= t('select_a_batch') %>:</label>
<div class="text-input-bg">
<%= select :batch, :id,
#batches.map {|b| [b.full_name, b.id] },
{:prompt => "#{t('common')}"},
{:onchange => "#{remote_function(
:url => { :action => 'show' },
:with => "'batch_id='+value",
:before => "Element.show('loader')",
:success => "Element.hide('loader')"
)}"} %>
<label ><%= t('select_a_module') %>:</label>
<div class="text-input-bg">
<%= select :planner, :id,
#planners.map {|p| [p.name, p.id] },
{:prompt => "#{t('common')}"},
{:onchange => "#{remote_function(
:url => { :action => 'show' },
:with => "'planner_id='+value",
:before => "Element.show('loader')",
:success => "Element.hide('loader')"
)}"} %>
<%= image_tag("loader.gif", :align => "absmiddle", :border => 0, :id => "loader", :style =>"display: none;" ) %>
</div></div></div>
<div id="class-timings-list"><%= render :partial => "show_batch_timing" %></div>
<div id="modal-box" style="display:none;"></div>
<div class="extender"></div>
</div>
show
<div class="linker">
<%= link_to_remote "#{t('add')}", :url => { :action => 'new', :id => #planner , :id => #batch} %>
</div>
<% unless #class_timings.empty? %>
<table id="class-timings-list" width="100%">
<tr class="tr-head">
<td><%= t('subject_title') %></td>
<td><%= t('page_no') %></td>
<td><%= t('duration') %></td>
<td><%= t('checklist') %></td>
<td><%= t('tutor_name') %></td>
<td><%= t('operations') %></td>
</tr>
<% #class_timings.each do |class_timing| %>
<tr id="class-timing-<%= class_timing.id %>" class="tr-<%= cycle('odd','even') %>">
<td class="col-2"><%= class_timing.subject_title %></td>
<td class="col-5" style="text-align:right"><%= class_timing.page_no %></td>
<td class="col-5" style="text-align:right"><%= class_timing.duration %></td>
<td class="col-5" style="text-align:center"><%= check_box_tag(nil, class_timing.checklist, class_timing.checklist, :disabled => true) %></td>
<td class="col-3"><%= class_timing.tutor_name %></td>
<td class="col-3"><small><%= link_to_remote("#{t('edit_text')}",
:url => edit_class_timing_path(class_timing), :method => 'get' ) %> |
<% #tt = PeriodEntry.find_all_by_id(class_timing_id ) %>
<!-- <% #tt = PeriodEntry.find_all_by_class_timing_id(class_timing.id ) %> -->
<% if #tt.empty? %>
<%= link_to_remote("#{t('delete_text')}",
:url => class_timing_path(class_timing),
:method => 'delete',
:confirm => "#{t('confirm_msg')}",
:update => "class-timing-#{class_timing.id}") %>
<% else %>
<s><%= t('delete_text') %></s>
<% end %></small></td>
</tr>
<% end %>
</table>
<% else %>
<h4><%= t('set_in_common') %></h4>
<% end %>
new
<label class="head_label"><%= t('create_new_teaching_schedule_for') %> <br>
<span>
<% if #planner.nil? and #batch.nil? %>
<%= t('common') %>
<% else %>
<%= #planner.name and #batch.full_name %>
<% end %>
</span></label>
<div id="ajax-create">
<% form_remote_for :class_timing,
:url => { :action => 'create'} do |f| %>
<% planner_id = (#planner.nil? ? nil : #planner.id) %>
<% batch_id = (#batch.nil? ? nil : #batch.id) %>
<%= f.hidden_field :planner_id, :value => planner_id %>
<%= f.hidden_field :batch_id, :value => batch_id %>
<div id="form-errors"></div>
<div class="label-field-pair">
<label for="name"><%= t('subject_title') %></label>
<div class="input-field"><%= f.text_field :subject_title %></div>
</div>
<div class="label-field-pair">
<label for="name"><%= t('page_no') %></label>
<div class="input-field"><%= f.text_field :page_no %></div>
</div>
<div class="label-field-pair">
<label for="name"><%= t('duration') %></label>
<div class="input-field"><%= f.text_field :duration %></div>
</div>
<div class="label-field-pair">
<label for="name"><%= t('Completed') %></label>
<div><%= f.check_box :checklist, :checked => false %></div>
</div>
<div class="label-field-pair">
<label for="name"><%= t('tutor_name') %></label>
<div class="input-field"><%= f.text_field :tutor_name %></div>
</div>
<br>
<br>
<br>
<%= f.submit "? #{t('save')}", :class => 'submit-button' %>
<% end %>
</div>
edit
<label class="head_label"><%= t('edit_teaching_schedule_for') %> <br>
<span>
<% if #planner.nil? and #batch.nil? %>
<%= t('common') %>
<% else %>
<%= #planner.name and #batch.name %>
<% end %>
</span></label>
<div id="ajax-edit">
<% form_remote_for #class_timing do |f| %>
<div id="form-errors"></div>
<div class="label-field-pair">
<label for="name"><%= t('subject_title') %></label>
<div class="text-input-bg"><%= f.text_field :subject_title %></div>
</div>
<div class="label-field-pair">
<label for="name"><%= t('page_no') %></label>
<div class="text-input-bg"><%= f.text_field :page_no %></div>
</div>
<div class="label-field-pair">
<label for="name"><%= t('duration') %></label>
<div class="text-input-bg"><%= f.text_field :duration %></div>
</div>
<div class="label-field-pair">
<% if #class_timing.checklist %>
<label for="name"><%= t('Completed') %></label>
<div><%= f.check_box :checklist, :checked => true %></div>
<% else %>
<label for="name"><%= t('Completed') %></label>
<div><%= f.check_box :checklist, :checked => false %></div>
<% end %>
</div>
<div class="label-field-pair">
<label for="name"><%= t('tutor_name') %></label>
<div class="input-field"><%= f.text_field :tutor_name %></div>
</div>
<br>
<br>
<br>
<%= submit_tag "? #{t('save')}", :class => 'submit-button' %>
<% end %>
</div>
Controller (class_timings_controller.rb)
class ClassTimingsController < ApplicationController
before_filter :login_required
filter_access_to :all
def index
#batches = Batch.active
#planners = Planner.active
#class_timings = ClassTiming.find(:all,:conditions => { :planner_id => nil, :batch_id => nil, :is_deleted=>false})
end
def new
#class_timing = ClassTiming.new
#batch = Batch.find params[:id] if request.xhr? and params[:id]
#planner = Planner.find params[:id] if request.xhr? and params[:id]
respond_to do |format|
format.js { render :action => 'new' }
end
end
def create
#class_timing = ClassTiming.new(params[:class_timing])
#planner = #class_timing.planner
#batch = #class_timing.batch
respond_to do |format|
if #class_timing.save
#class_timing.planner.nil? and #class_timing.batch.nil? ?
#class_timings = ClassTiming.find(:all,:conditions => { :planner_id => nil,:batch_id => nil,:is_deleted=>false}) :
#class_timings = ClassTiming.for_batch(#class_timing.batch_id)
#class_timings = ClassTiming.for_planner(#class_timing.planner_id)
# flash[:notice] = 'Teaching schedule was successfully created.'
format.html { redirect_to class_timing_url(#class_timing) }
format.js { render :action => 'create' }
else
#error = true
format.html { render :action => "new" }
format.js { render :action => 'create' }
end
end
end
def edit
#class_timing = ClassTiming.find(params[:id])
respond_to do |format|
format.html { }
format.js { render :action => 'edit' }
end
end
def update
#class_timing = ClassTiming.find params[:id]
respond_to do |format|
if #class_timing.update_attributes(params[:class_timing])
#class_timing.planner.nil? and #class_timing.batch.nil? ?
#class_timings = ClassTiming.find(:all,:conditions => { :planner_id => nil, :batch_id => nil}) :
#class_timings = ClassTiming.for_batch(#class_timing.batch_id)
#class_timings = ClassTiming.for_planner(#class_timing.planner_id)
# flash[:notice] = 'Teaching schedule updated successfully.'
format.html { redirect_to class_timing_url(#class_timing) }
format.js { render :action => 'update' }
else
#error = true
format.html { render :action => "new" }
format.js { render :action => 'create' }
end
end
end
def show
#batch = nil
if params[:batch_id] == ''
#class_timings = ClassTiming.find(:all, :conditions=>["batch_id is null and is_deleted = false"])
else
#class_timings = ClassTiming.active_for_batch(params[:batch_id])
#batch = Batch.find_by_id params[:batch_id] unless params[:batch_id] == ''
end
#planner = nil
if params[:planner_id] == ''
#class_timings = ClassTiming.find(:all, :conditions=>["planner_id is null and is_deleted = false"])
else
#class_timings = ClassTiming.active_for_planner(params[:planner_id])
#planner = Planner.find_by_id params[:planner_id] unless params[:planner_id] == ''
end
respond_to do |format|
format.js { render :action => 'show' }
end
end
def destroy
#class_timing = ClassTiming.find params[:id]
#class_timing.update_attribute(:is_deleted,true)
end
end
I provide all the code to help you figure out better, although I knew some of the code is irrelevant to show.
params[:planner_id] or params[:id]
is not passed properly I guess..
Use Planner.find_by_id params[:planner_id]
You're not passing params[:planner_id] into your controller, therefore it is nil, not ''.
So the else on your conditional is being invoked, and you're calling Planner.find nil.
Change this by calling if params[:planner_id].present? instead.
Try this out:
#planner = Planner.find params[:planner_id] if params[:id].present?
present? will take care of nil, blank, etc.

NoMethodError at /members undefined method `friend_id' for nil:NilClass

Now I am trying to add "follow button" on my users' index page.
My goal is like... when users push the button once, the message beside the button turns "following". And he or she push it again, the message turns "not following". Default message is "not following".
And then I wrote the codes below but got an error message.Could you tell me how to solve this problem?
NoMethodError at /members undefined method `friend_id' for nil:NilClass
☆index.html.erb(members_controller)
<table class="table">
<tr>
<th>写真</th>
<th>名前</th>
<th>分野</th>
<th>場所</th>
<th>経験</th>
<th>Follow</th>
<%# if Member.find(session[:user_id]).admin %>
<%# end %>
</tr>
<% #members.each do |member| %>
<tr>
<td>
<% if member.provider %>
<%=image_tag member.image ,:size=>'30x30'%>
<% elsif member.avatar_file_name %>
<%= image_tag member.avatar.url(:thumb), :width =>'30px', :height =>'30px' %>
<% else %>
<%= image_tag "love.png", :size=>'30x30' %>
<% end %>
</td>
<td>
<%= member.name %>
<% if member.provider == "facebook" %>
<a target="_blank" href="http://www.facebook.com/<%=member.uid %>"> <%=image_tag "fb.png" ,:size=>'20x20'%> </a>
<% elsif member.provider == "twitter" %>
<a target="_blank" href="http://www.twitter.com/<%=member.name %>"> <%=image_tag "twitter.png" ,:size=>'20x20'%> </a>
<% end %>
</td>
<td><%= member.field %></td>
<td>
<% if member.url.present? %>
<%=link_to member.place ,member.url ,:target=>["_blank"] %>
<% else %>
<%= member.place %>
<% end %>
</td>
<td><%= member.experience %></td>
<td>
<div class="onoff">
<% unless session[:user_id] == member.id %>
<% if #isFr.friend_id == member.id %>
<%= link_to'off', {:controller => 'members', :action => 'index', :id =>
member.id}, class: "btn btn-midium btn-primary"%><br/>
※following
<% else %>
<%= link_to'on', {:controller => 'members', :action => 'index', :id =>
member.id}, class: "btn btn-midium btn-primary"%><br/>
※not following
<% end %>
<% end %>
</div>
<%#= member.friends.count %>
</td>
<% if Member.find(session[:user_id]).admin %>
<td><%= link_to 'Destroy', member, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% end %>
</tr>
<% end %>
</table>
☆members_controller
def index
if !checklogin? then return end
#members = Member.where("id >=1").order("created_at desc").paginate(:page => params[:page], :per_page => 10).scoped
if params[:name].present?
#members = #members.where("name like ?" , "%" + params[:name] + "%")
end
if Friend.where(:member_id => session[:user_id], :friend_id => params[:id]).exists? then
Friend.where(:member_id => session[:user_id], :friend_id => params[:id]).each do |f|
f.destroy
end
else
Friend.new({:member_id => session[:user_id], :friend_id =>params[:id].to_i}).save
end
#members.each do |m|
#isFr = Friend.where(:member_id => session[:user_id], :friend_id =>
m.id ).first
end
respond_to do |format|
format.html # index.html.erb
format.json
end
end
☆sessions_controller #session[:user_id]
auth = request.env["omniauth.auth"]
member = Member.find_by_provider_and_uid(auth["provider"], auth["uid"])
session[:user_id] = member.id
Below query must be returning blank AR
#members = Member.where("id >=1")
.order("created_at desc")
.paginate(:page => params[:page], :per_page => 10).scoped
And #isFr is eventually resulting in nil
#members.each do |m|
#isFr = Friend.where(:member_id => session[:user_id], :friend_id =>
m.id ).first
end
Just a word of refactor you can do
Friend.where(:member_id => session[:user_id], :friend_id => #members.map(&:id)) .limit(1)
But would be better if you think on the associations between your models. That way it will optimize with join queries
Thanks to those who gave me hints, I made it with the code below.
☆index.html.erb(members_controller)
<td>
<div class="onoff">
<% unless session[:user_id] == member.id %>
<% if #Frids.include? member.id%>
<%= link_to'Off', {:controller => 'members', :action => 'index', :id =>
member.id}, class: "btn btn-midium btn-warning"%><br/>
※既follow
<% else %>
<%= link_to'On', {:controller => 'members', :action => 'index', :id =>
member.id}, class: "btn btn-midium btn-primary"%><br/>
※未follow
<% end %>
<% end %>
</div>
<%#= member.friends.count %>
</td>
☆members_controller
def index
if !checklogin? then return end
#members = Member.where("id >=1").order("created_at desc").paginate(:page => params[:page], :per_page => 10).scoped
if Friend.where(:member_id => session[:user_id], :friend_id => params[:id]).exists? then
Friend.where(:member_id => session[:user_id], :friend_id => params[:id]).each do |f|
f.destroy
end
else
Friend.new({:member_id => session[:user_id], :friend_id =>params[:id].to_i}).save
end
#Frids =Friend.where(:member_id => session[:user_id]).map{|x| x.friend_id}
respond_to do |format|
format.html # index.html.erb
format.json
end
end
The problem here is that the instance var #isFr in your index action returns nil. That means the code Friend.where(:member_id => session[:user_id], :friend_id => m.id ).first is returning nil.
You could do <% if #isFr.try(:friend_id) == member.id %> in your members#index view so that that you won't get the NoMethodError.
There's something wrong with the below code I found in your index action:
#members.each do |m|
#isFr = Friend.where(:member_id => session[:user_id], :friend_id => m.id ).first
end
The problem with the code above is that #isFr will only retain value of the last element in #members array. It's better to add a method in the Member model for that behavior like this:
#in app/models/friend.rb
scope :get_member_id, ->(member, user_id) { where(:member_id => user_id, :friend_id => member.id ).first.try(:friend_id) }
Then use it in your view like:
<% unless session[:user_id] == member.id %> # below this line of code
<% if Friend.get_member_id(member, session[:user_id]) == member.id %>
Hope this helps!

Why this won't pass previous input data when it returns error and redirect?

First of all, I input 'hello' into comment input box and submit it.
It succeeds without any problem.
Then if I try re-do again within 10 seconds, I designed it to returns 'spamming warning'
It does the action as I wished. But the problem is, all the input data I typed in at previous page is gone... so I have to type them all again.
How can I leave the previous input data?
def show
...
#comment_input = #user.comment_threads.build
...
end
def add_comment
#user = User.find_by_username(params[:id])
#post = #user.comment_threads.last
if #post
last_time = #post.created_at
if Time.now - last_time <= 10.second
redirect_to :controller => 'users', :action => 'show', :id => #user.username
flash[:notice] = "You cannot spam!"
return
end
end
#user_who_commented = current_user
#comment = Comment.build_from( #user, #user_who_commented.id, params[:comment][:body] )
#comment.comment_icon = params[:comment][:comment_icon]
#comment.save
redirect_to :controller => 'users', :action => 'show', :id => #user.username
flash[:notice] = "comment added!"
end
_form.html.erb
<%=form_for #comment_input, url: url_for( :controller => :users, :action => :add_comment ) do |f| %>
<div class="field">
<%= f.label :body %><br />
<%= f.text_field :body %>
</div>
<div class="field">
<%= f.file_field :comment_icon %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
UPDATE:
users_controller.rb #show
def show
#user = User.find_by_username(params[:id])
#comments = #user.comment_threads.order("updated_at DESC").page(params[:page]).per(5)
#comment_input = #user.comment_threads.build
respond_to do |format|
format.html # index.html.erb
format.json { render json: #user }
end
end
end
views/users/show.html.erb
.....
<table>
<tr>
<th>ID</th>
<th>PIC</th>
<th>Body</th>
<th>Subject</th>
<th>Posted by</th>
<th>Delete</th>
</tr>
<% #comments.each do |comment| %>
<tr>
<td><%= comment.id %></td>
<td>
<% if comment.comment_icon? %>
<ul class="thumbnails">
<%= image_tag(comment.comment_icon.url(:thumb),:height => 100, :width => 100, :style => 'border:3px double #545565;' ) %>
</ul>
<% end %>
</td>
<td><%= comment.body %></td>
<td><%= comment.subject %></td>
<td><%= comment.user.user_profile.nickname if comment.user.user_profile %></td>
<td>
<%= button_to 'destroy', delete_comment_user_path(#user,comment.id), confirm: 'Are you sure?', :disable_with => 'deleting...' if current_user && current_user.id == comment.user_id %>
</td>
</tr>
<% end %>
</table>
<%=form_for #comment_input, url: url_for( :controller => :users, :action => :add_comment ) do |f| %>
<div class="field">
<%= f.label :body %><br />
<%= f.text_field :body %>
</div>
<div class="field">
<%= f.file_field :comment_icon %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
......
def add_comment
#user = User.find_by_username(params[:id])
#post = #user.comment_threads.last
#user_who_commented = current_user
#comments = #user.comment_threads.order("updated_at DESC").page(params[:page]).per(5)
#comment = Comment.build_from( #user, #user_who_commented.id, params[:comment][:body] )
#comment.comment_icon = params[:comment][:comment_icon]
if #post && (Time.now - #post.created_at) <= 10.second
flash[:notice] = "You cannot spam!"
render :action => "show"
elsif #comment.save
flash[:notice] = "comment added!"
redirect_to :controller => 'users', :action => 'show', :id => #user.username
else # if model's errors
render :action => "show"
end
end

Rails Import CSV - All Values 'Nil'

I'm learning how to import csv to a rails application. However, when i run the application, all the values turned out to be 'nil'. May I know what is wrong with my codes below?
Controller
def import_csv
#list = List.find(params[:list_id])
#lists = List.all
respond_to do |format|
#csv=params[:file]
#n=0
CSV.parse(#csv).each do | row |
#user_new = User.new
#user_new.first_name = row[0]
#user_new.last_name = row[1]
#user_new.email = row[2]
#user_new.address = row[3]
#user_new.city = row[4]
#user_new.state = row[5]
#user_new.zipcode = row[6]
#user_new.country = row[7]
#user_new.notes = row[8]
#user_new.birthday = row[9]
#user_new.home_number = row[10]
#user_new.mobile_number = row[11]
#user_new.list_id = #list.id
#user_new.save
#n=#n+1
GC.start if #n%50==0
flash[:notice] = "CSV Imported Successfully, with #{#n} records"
end
format.html { redirect_to lists_url }
format.json { head :no_content }
end
end
View
<%= form_for(:list, :url => list_import_csv_path(#list), :method => :get, :html => {:multipart => true}) do |f| %>
<table>
<tr>
<td><label for="dump_file">Select a CSV File :</label></td>
<td ><%= file_field_tag :file %></td>
</tr>
<tr>
<td colspan='2'><%= submit_tag 'Submit' %></td>
</tr>
</table>
<% end %>
I think you got problems on you form sending it in the url try something like this. (I got a custom action on my controller called import)
<%= form_tag #list, :action => 'import', :multipart => true do |f| %>
<label class="control-label" for="fileInput" style="width: 160px">Select file: </label>
<%= file_field_tag 'file', :accept => 'text/html', :size => 60%>
<div class="form-actions">
<%= submit_tag 'SUbmit', :class=>"btn" %>
</div>
<% end %>
Try to follow this tutorial it has an excellent example of importing and exporting.Check it out

Resources