Remove array from showing under my rails erb-form - ruby-on-rails

I have a checkbox form where you can select many customers to attend a single event. The form works but there is an array of all customers under it and I can't figure out how to remove it.
events.rb
def addcustomer
#event = Event.find(params[:id])
#customer = Customer.all
end
routes.rb
resources :events do
get 'addcustomer', on: :member, as: 'add'
end
addcustomerform.html.erb
<%= form_for(#event) do |f| %>
<%= hidden_field_tag "event[customer_ids][]", nil%>
<%= #customer.each do |customer| %>
<%= check_box_tag "event[customer_ids][]", customer.id,
#event.customer_ids.include?(customer.id), id:dom_id(customer) %>
<%= label_tag dom_id(customer), customer.id %>
<%= label_tag dom_id(customer), customer.name %> --
<%= label_tag dom_id(customer), customer.email %> --
<%= label_tag dom_id(customer), customer.phone %>
<br>
<% end %>
<br>
<%= f.submit%>
<% end %>
Here is a photo of what the issue looks like:
Here is the repo
https://github.com/robbiesoho/fanfactory
I hope someone can help. Thank you

it is not the params that are shown there but all the customers.
If you replace <%= #customer.each do |customer| %> with <% #customer.each do |customer| %>
The difference is that I remove the =. The = means that the line should be added to the HTML as text. on that line is the #customer array, and the result f #customer.to_s is what you see there.
For more information, please read: What is the difference between <%, <%=, <%# and -%> in ERB in Rails?

Related

rails: how to update multiple records using "accepts_nested_attributes"

Let's say I have a cat model and a life model. And let's say a cat (#cat) has_many lives, and a cat accepts_nested_attributes for a life.
Now, if I wanted to update 7 lives (#lives) at once, using one form_for(#cat), how would that form look like? This is what I've tried, but in this form only the attributes for the last life are passed to the params hash:
<%= form_for(#cat) do |f| %>
<% #lives.each do |life| %>
<%= f.fields_for(life) do |l| %>
<%= l.input :date_of_birth, as: :date %>
<% end %>
<% end %>
<%= f.submit %>
<% end %>
You need to build the attributes in your controller
#cat = Cat.find(<criteria>)
#cat.lives.build
In your example, you have a loop inside a loop. Try this:
<%= form_for(#cat) do |f| %>
<%= f.fields_for(:lives) do |l| %>
<%= l.input :date_of_birth, as: :date %>
<% end %>
<%= f.submit %>
<% end %>

How do i route a particular controller action to a particular form in rails?

Hi I am new to RoR and I was making a simple math_app. The addition function works fine. Now I'm trying to do a simple subtraction.The subtract controller is invoked and the subtract form is displayed but when I click on subtract the add controller gets invoked and an addition is performed. Where have I gone wrong?
This is my routes.rb:
Rails.application.routes.draw do
get 'subtract/form'
post 'subtract/result'
get 'add/form'
post 'add/result'
end
result.html.erb:
<%= #first %> - <%= #second %> = <%= #result %>
<br/>
<%= link_to 'back', subtract_form_path %>
form.html.erb: (This is the subtraction form)
<%= form_tag subtract_result_path do %>
<%= number_field_tag :first %>
-
<%= number_field_tag :second %>
<%= submit_tag "subtract" %>
<% end %>
subtract_controller.rb:
class SubtractController < ApplicationController
def form
end
def result
#first = params[:first].to_i
#second = params[:second].to_i
#result = #first - #second
end
end
You have problem in views/substract/form.html.erb. It now reads:
<%= form_tag add_result_path do %>
<%= number_field_tag :first %>
-
<%= number_field_tag :second %>
<%= submit_tag "subtract" %>
<% end %>
But it should be:
<%= form_tag substract_result_path do %>
<%= number_field_tag :first %>
-
<%= number_field_tag :second %>
<%= submit_tag "subtract" %>
<% end %>
You should probably also fix route
post '/subtract/result' => 'subtract#result', as: 'substract_result'
Also in views/substract/result.html.erb:
<%= #first %> - <%= #second %> = <%= #result %>
<br/>
<%= link_to 'back', subtract_form_path %>

Limit User to 1 Like/Dislike per Post

I have a listing model (which allows comments) and users can like (thumbs up) or dislike (thumbs down) the listing. It works at the moment but I want to iterate over the likes for a specific listing, and if any like's user_id listing.likes.user_id matches the current users ID current_user.id then remove the form to like (users can add a reason why they are liking the listing)
<%= form_for([#listing, #listing.likes.build]) do |f| %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.text_field :body %>
<%= f.submit %>
<% end %>
I have something halfway there that checks if the current likes user_id matches the current user id, if so provide a link to delete (remove/unlike) the like.
<% if current_user.id == like.user_id %>
<%= link_to '[ Delete Like ]', [like.listing, like],
method: :delete,
data: { confirm: 'Are you sure?' } %>
<% end %>
How would I go about using the code to remove the like form if the user has already created a like/if the user's ID matches the user_id of a like created for a specific listing(something like this?)
<% if current_user.id == listing.likes.any.user_id %>
<% else %>
<%= form_for([#listing, #listing.likes.build]) do |f| %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.text_field :body %>
<%= f.submit %>
<% end %>
<% end %>
I think you had the right idea with the any method, but were not using it correctly. any can accept a block in which you can do your user_id comparison, like so:
<% if listing.likes.any{|like| like.user_id == current_user.id } %>
In this case, any will return true the instant the condition in the block evaluates to true.
Let me know whether this works.
Side Note
If my method works for you, you may like to further clean up your view code by moving that check in to the Listing model as a helper method. Something like this:
class Listing < ActiveRecord::Base
# ... other stuff
def has_comment_from?(target_user)
likes.any{|like| like.user_id == target_user.id }
end
end
And then you can simply call it from the view like so:
<% if listing.has_comment_from? current_user %>
Found a partial solution but it stops the user from commenting after 1 comment, not 1 comment per listing.
<% #listing.likes.each do |like| %>
<% if current_user.id == like.user_id %>
<% else %>
<%= form_for([#listing, #listing.likes.build]) do |f| %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.text_field :body %>
<%= f.submit %>
<% end %>
<% end %>
<% end %>

How do I separate elements by their type with an each method?

I created two scaffolds: announce_sections and announcements. The announce_sections are the types of announcements there are (i.e. games, tryouts, etc) and when I create an announcement I specify what type of announce_sections it is. I'm trying to display it so that each announce_section is viewed, with each announcement and its information under the announce_section. This is what I came up with:
<% #announce_sections.each do |announce_section| %>
<%= announce_section.name %>
<% #announcements.each do |announcement| %>
<%= announcement.announcement_title %>
<%= announcement.information %>
<%= announcement.additional_information %>
<%= announcement.type %>
<% end %>
<% end %>
However, this code only displays the announce_sections with the all announcements under it. The announcements don't get separated into their respective announce_sections. How do I change it so that it does?
<% #announce_sections.each do |announce_section| %>
<%= announce_section.name %>
<% #announcements.where(type: announce_section).each do |announcement| %>
<%= announcement.announcement_title %>
<%= announcement.information %>
<%= announcement.additional_information %>
<%= announcement.type %>
<% end %>
<% end %>
Use the name of the field you are using to assign the announcement type instead of 'type'
There are many ways to solve this, but one simple one is to build a hash where the key is the type of announcement_section and the value is an array (or Set) of the announcement. One way to build that hash is to use the Hash.new {|hash, key| ... } form of the constructor.
#hash = Hash.new {|hash, section| hash[section] = Array.new }
#announcements.each do |a|
# for each announcment append it to the array under the hash
#hash[a.section] << a
end
And then, in the view
<% #hash.keys.each do |section| %>
<%= section %>
<% #hash[section].each do |announcement| %>
<%= announcement.announcement_title %>
<%= announcement.information %>
<%= announcement.additional_information %>
<%= announcement.type %>
<% end %>
<% end %>

" undefined method `enumerable_enumerator_path' " error

I'm using basic scaffold structure. What I need, is to add 'moderate' action and view by changing published to true. In my idea, on moderate.html I should get the list of all unpublished entries with the ability to change and save their parameters.
Here are parts of my code:
#names_controller.rb
def moderate
#name = Name.find(:all, :conditions => {:published => false} )
respond_to do |format|
format.html
format.xml
end
end
#moderate.html.erb
<% form_for #name.each do |f| %>
<%= f.error_messages %>
<%= f.text_field :which %>
<%= f.text_field :what %>
<%= f.check_box :published %>
<%= f.submit %>
</p>
<% end %>
Instead I'm getting this error:
NoMethodError in Names#moderate
Showing app/views/names/moderate.html.erb where line #1 raised:
undefined method `enumerable_enumerator_path' for #<ActionView::Base:0x1042c3e90>
Extracted source (around line #1)
So, can you help to newbie please?
ruby 1.8.7 (2009-06-12 patchlevel 174)
[universal-darwin10.0] Rails 2.3.5
If you want to update each name in a separate form, then all you need to do is move the loop above form_for:
<% #name.each do |n| %>
<% form_for n do |f| %>
<%= f.error_messages %>
<%= f.text_field :which %>
<%= f.text_field :what %>
<%= f.check_box :published %>
<%= f.submit %>
</p>
<% end %>
<% end %>
But if you'd like to do it all in one submit (a single form) then I guess you can't use form_for. I'd use form_tag to create a custom form to update multiple instances. This should work both for create and edit form:
<%= form_tag moderate_names_path do %>
<% #names.each do |name| %>
<fieldset>
<%= fields_for "name[#{name.id}]", name do |name_fields| %>
<p><%=name_fields.label(:this)%>: <br /><%= name_fields.text_field :this %></p>
<p><%=name_fields.label(:that)%>: <br /><%= name_fields.text_field :that %></p>
<p><%= name_fields.check_box :published %> <%=name_fields.label(:published)%></p>
<% end %>
</fieldset>
<br />
<% end %>
<%= submit_tag %>
<% end %>
NOTICE: I changed #name to #names in the second example

Resources