How to let users attend events? - ruby-on-rails

So I have an app, sort of like meetup.com, users can create events, comment on the events, search the events by GPS radius.. However, I'd like to allow users to click on an 'Attend' button and then on the events/show.html.erb page show which users are attending etc...
How might I do this?
current event show.html.erb
<%= render 'shared/header' %>
<div class="container">
<div class="row">
<div class="span3">
<%= render 'sidebar' %>
</div>
<div class="span5">
<div class="new_event_form">
<div class="line1"><h4>Create event</h4></div>
<%= form_for current_user.events.new, remote: true do |f| %>
<h5>Event title:</h5>
<div><%= f.text_field :title, placeholder: "Event title", required: true, autocomplete: :off %></div>
<h5>Event description:</h5>
<div><%= f.text_area :description, placeholder: "Event description", required: true, autocomplete: :off %></div>
<h5>Event date:</h5>
<div><%= f.text_field :date %></div>
<h5>Event location:</h5>
<div><%= f.text_field :location, placeholder: "Event location", required: true, autocomplete: :off %></div>
<div>
<%= f.submit "Create event", class: 'btn btn-primary' %>
<%= link_to "Cancel", '#', class: 'btn cancel_event' %>
</div><br />
<% end %>
</div>
<div class="events_list">
<!-- look in events/event.hmlt.erb -->
<h4><%= #event.title %> at <%= #event.location %></h4>
<p><%= #event.description %></p>
<h5><i class="fa fa-calendar-o"></i> <%= #event.date.strftime("%A, %B %d, %Y") %></h5>
<h5><i class="fa fa-clock-o"></i> <%= #event.time %></h5>
<h5><i class="fa fa-map-marker"></i> <%= #event.location %></h5>
</div>
<div class="name"></div>
<%= form_for [#commentable, #comment], remote: true do |f| %>
...........and so on...
Thanks in advanced!

You need to define 'attend' action in your routes.rb and your Event controller, and then create attend.html.erb form for it.
Edit: in routes.rb
resources :events do
post 'attend', on: :member
end
in events_controller
def attend
#event.attendees << current_user
#event.save
end
in show.html.erb somewhere
<%= button_to 'Attend', attend_event_path(#event), method: :post, confirm: 'really?' %>
and make a nice attend.html.erb to say "thanks for signing up"

Related

Getting undefined method `[]' for nil:NilClass even when the instance variable is defined

I am trying to call a controller action at two different places.
The controller is as follows:
def create
#phone_number = PhoneNumber.find_or_create_by(phone_number: params[:phone_number][:phone_number])
#phone_number.generate_pin
#phone_number.send_pin
respond_to do |format|
format.js # render app/views/phone_numbers/create.js.erb
end
end
I am trying to call create on a link in view:
<%=link_to "Resend Pin", phone_numbers_path, method: :post %>
My rake routes output looks like this:
phone_numbers POST /phone_numbers(.:format) phone_numbers#create
new_phone_number GET /phone_numbers/new(.:format) phone_numbers#new
phone_numbers_verify POST /phone_numbers/verify(.:format) phone_numbers#verify
I have data in PhoneNumber activerecord. When I click on "Resend Pin" I am getting following error:
NoMethodError in PhoneNumbersController#create
undefined method[]' for nil:NilClass`
Can somebody please tell me what am I missing here ?
html.erb
<div id="send-pin">
<h3>What's your phone number?</h3>
<%= form_for #phone_number, remote: true do |f| %>
<div class="form-group">
<%= f.text_field :phone_number %>
</div>
<%= f.submit "Send PIN", class: "btn btn-primary", id: 'send-pin-link' %>
<% end %>
</div>
<div id="verify-pin">
<h3>Enter your PIN</h3>
<%= form_tag phone_numbers_verify_path, remote: true do |f| %>
<%= hidden_field_tag 'hidden_phone_number', '' %>
<div class="form-group">
<%= text_field_tag :pin %>
</div>
<%= submit_tag "Verify PIN", class: "btn btn-primary" %>
<% end %>
<%=link_to "Resend Pin", phone_numbers_path, method: :post %>
</div>
params[:phone_number][:phone_number] is null.
Try the below in view
<div id="send-pin">
<h3>What's your phone number?</h3>
<%= form_for #phone_number, remote: true, html: {id: "first_form"} do |f| %>
<div class="form-group">
<%= f.text_field :phone_number %>
</div>
<%= f.submit "Send PIN", class: "btn btn-primary", id: 'send-pin-link' %>
<% end %>
</div>
<div id="verify-pin">
<h3>Enter your PIN</h3>
<%= form_tag phone_numbers_verify_path, remote: true do |f| %>
<%= hidden_field_tag 'hidden_phone_number', '' %>
<div class="form-group">
<%= text_field_tag :pin %>
</div>
<%= submit_tag "Verify PIN", class: "btn btn-primary" %>
<% end %>
<%=link_to "Resend Pin",'#', id: "resend_pin_code" %>
</div>
<div id="status-box" class="alert alert-success">
<p id="status-message">Status: Haven’t done anything yet</p>
</div>
<script>
$("#resend_pin_code").click(function(){
$("#first_form").submit();
}
)
</script>

Rails check_box_tag params does not appear the first POST the second time they are available in the params

Newbie with RoR, who is going crazy!
When I tick a couple of the check_boxes and Click on the Submit button. The checked boxes (array of values) are NOT available in the Param. The page then redirects back to itself. When I then do the same thing again(tick a couple of check_boxes and click Submit), then the checked boxes are available in the params and I can save them. What is going wrong the first time around and how can I fix it. What am i doing wrong here?
Thanks alot!
<div class="row">
<div class="col-md-4">
<%= form_for(#user, url: registration_path(#user), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br>
<p><%= f.email_field :email, autofocus: true %></p>
</div>
<!-- <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<div class="field">
<%= f.label :password, "New password" %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation, "New password confirmation" %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :current_password %> <i>(to confirm changes)</i><br />
<%= f.password_field :current_password, autocomplete: "off" %>
</div> -->
<%= form_for (#user.musician) do |musician_form| %>
<%= musician_form.label "Naam" %>
<p><%= musician_form.text_field :name %></p>
<%= musician_form.label "Wat is je woonplaats?" %>
<p><%= musician_form.text_field :city %></p>
<%= musician_form.label "In welke Provincie woon je?" %><br>
<%= musician_form.select :province_id, Province.all.collect{|t| [t.description, t.id]}, { :include_blank => false } %><br><br>
<%= musician_form.label "Wat is je geboortedatum?" %>
<p><%= musician_form.date_select :birthday %></p>
<%= musician_form.label "Beschrijving" %><br>
<%= musician_form.text_area :description, class: "form-control", size: "40x5" %>
<div class="actions">
<br>
<%= f.submit "Wijzigingen opslaan", class: "btn btn-primary btn-xs" %>
</div>
<div class="col-md-4">
<b>Welk type muzikant ben je?</b><br><br>
<% for musician_type in MusicianType.all %>
<div>
<%= check_box_tag "musician[musician_type_ids][]", musician_type.id, #user.musician.musician_types.include?(musician_type) %>
<%= musician_type.description %>
</div>
<% end %>
<br>
<b>In welke genre(s) ben je geinteresseerd?</b><br><br>
<% for genre in Genre.all %>
<div>
<%= check_box_tag "musician[genre_ids][]", genre.id, #user.musician.genres.include?(genre) %>
<%= genre.description %>
</div>
<% end %>
<br>
<% end %>
<% end %>
<div class="col-md-4">
<p>Niet tevreden? <%= button_to "Account verwijderen", registration_path(resource_name), data: { confirm: "Weet je zeker dat je je account en bijbehorende band(s) en vacature(s) wilt verwijderen? Het account kan niet meer worden teruggehaald!" }, method: :delete, class: "btn btn-danger btn-xs" %></p>
</div>
And here is part of my controller code:
def edit
#user = User.find(current_user.id)
end
def update
account_update_params = devise_parameter_sanitizer.sanitize(:account_update)
#user = User.find(current_user.id)
params[:musician][:musician_type_ids] ||= []
params[:musician][:genre_ids] ||= []
# unless params[:musician][:musician_type_ids].present?
# params[:musician][:musician_type_ids] = []
# logger.info "DEBUG INFO: PARAM IS EMPTY #{#musician}"
# end
#
# unless params[:musician][:genre_ids].present?
# params[:musician][:genre_ids] = []
# end
#musician = current_user.musician
#musician.update(musician_params)
if needs_password?
successfully_updated = #user.update_with_password(account_update_params)
else
account_update_params.delete('password')
account_update_params.delete('password_confirmation')
account_update_params.delete('current_password')
successfully_updated = #user.update_attributes(account_update_params)
end
if successfully_updated
set_flash_message :notice, :updated
sign_in #user, :bypass => true
redirect_to edit_user_registration_path
else
render 'edit'
end
end
Check out the documentation at apidock:
http://apidock.com/rails/ActionView/Helpers/FormTagHelper/check_box_tag
in the comments:
<% #roles.each do |role| %>
<li>
<%= check_box_tag 'role_ids[]', role.id -%>
<%= h role.name -%>
</li>
<% end %>
Thus, if you want to permit genre_ids you'd have to write:
<%= check_box_tag "genre_ids[]", genre.id, #user.musician.genres.include?(genre) %>

search for select in rails

I have a rails app where user can assign task to another user by creating task action. At the moment I have the f.collection_select, but as the number of users grows it won't be a good solution. I would like to change it into kinda search form. My problem is that I don't see how I can do this. I have other search forms, but those are for show/index action. How can I do this for the create action?
here is my current code:
<%= form_for ([#user, #task]) do |f| %>
<%= render 'layouts/error_messages', object: f.object %>
<div class="field form-group">
<%= f.label :executor_id %>
<%= f.collection_select(:executor_id, User.all, :id, :email, class: "form-control") %>
</div>
<div class="field form-group">
<%= f.label :content %>
<%= f.text_area :content, class: "form-control" %>
</div>
<div class="field form-group">
<%= f.label :deadline %>
<%= f.date_select :deadline, class: "form-control" %>
</div>
<div class="actions">
<%= f.submit "Create Task", class: 'btn btn-primary', "data-sid" => current_user.id, "data-rip" => :executor_id %>
</div>
<% end %>

My submit button won't work in rails and I'm not sure why

I'm creating a simple form in rails and the submit button doesn't work. I think I'm missing something obvious here but the html seems to be fine (I'm far froma front end expert). Any advice or pointers?
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h2>Apply</h2>
<hr class="star-primary">
</div>
</div>
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<div class="row control-group">
<% if #user.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(#user.errors.count, "error") %>.
</div>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="col-xs-12 floating-label-form-group controls">
<%= form_for #user, url: users_path(#user), :html => {:method => :post} do |f| %>
<%= f.label :name %>
<%= f.text_field :name, class: "form-control", placeholder: 'Name' %>
<%= f.label :email%>
<%= f.text_field :email, class: "form-control", placeholder: "Email" %>
<%= f.label :address%>
<%= f.text_field :address, class: "form-control", placeholder: "Address" %>
<%= f.label :phone %>
<%= f.text_field :phone, class: "form-control", placeholder: "Phone Number" %>
<%= f.submit "Apply"%>
<%end%>
</div>
</div>
</div>
</div>
</div>
Also, when the submit button fails, nothing happens. No error messages, no console errors. Nothing.
Take method out from the html hash?
form_for #user, url: users_path(#user), :method => :post do |f|
Try this:
<%= form_for #user, :url => {:controller => "your-controller-name", :action => "your-action-name"} do |f| %>
Can you try this one:
<%= form_for #user, url: {action: "create"} do |f| %>
...
...
<%= f.submit "Apply" %>
<% end %>
But I strongly suggest you to use simple_form gem.

submit button in rails app is not using twitter bootstrap styling

I have a view with a form_for and it has a submit button. I would like the submit button to be styled using twitter bootstrap rails gem (which I have successfully installed into the rails project.) However the the button isn't being styled for whatever reason, and the text in the button appears different from I specified in the argument.
The source for the view, new.html.erb
<div id="sign_up">
<h1>Sign Up</h1>
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% #user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<!-- <%= f.label :email %> --><br />
<%= f.text_field :email, :class => 'input-xlarge', placeholder: 'email' %>
</div>
<div class="field">
<!-- <%= f.label :password %>--><br />
<%= f.password_field :password, :class => 'input-xlarge', placeholder: 'Password' %>
</div>
<div class="field">
<!-- <%= f.label :password_confirmation %>--><br />
<%= f.password_field :password_confirmation, :class => 'input-xlarge', placeholder: 'Password confirm' %>
</div>
<div class="actions"><%= f.submit nil, class: 'btn btn-large' "Sign Up" %></div>
<% end %>
</div>
Right now the form looks like the following,
<%= f.submit nil, class: 'btn btn-large' "Sign Up" %>
Should be:
<%= f.submit "Sign Up", class: 'btn btn-large' %>

Resources