Form_for with render list of check_box - ruby-on-rails

I have functionality search above tags. I want to render all tags with check_box in form_for that I will filter those tags and render all items again with new parameters.
= form_for #tags do |f|
- #tags.each do |tag|
f.check_box(#{tag}, "yes")
f.label_tag "#{tag}"
f.submit 'Filter'
end

I don't know what you are asking because your question is not much clear, but trying to understand your question. If I understood your question then you need to add a filter with tags on the search page, right? If yes then try the following steps.
Create a form for search tag like
<%= form_tag search_path, method: :get do %>
<% for tag in Model.order("name") %> #=> Model means which stored your tags like "Tag"
<%= check_box_tag("#{tag.name}[]", "#{tag.name}") %> tag.name
OR
<%= check_box_tag("tags[]", "#{tag.name}") %> tag.name
<% end %>
<%= submit_tag "Filter" %>
<% end %>
Or If you need filter separately clicking link and keep current search then
<% for tag in Model.order("name") %> #=> Model means which stored your tags like "Tag"
<%= link_to tag.name, request.query_parameters.merge({tag: tag.name}) %>
<% end %>
See the below image for keep current search and filter by clicking on the link
These all are view functionality, you need to update your SQL query.
I think it will help.

It's work:
= form_tag({controller: 'searches', action: 'filter'}, method: 'get') do
ul
- #tags.each do |tag|
li
input type='checkbox' name="tags[]" checked="checked" id="#{tag}" value="#{tag}"
label_tag for="#{tag}" #{tag}
button Filter

Related

Linking to acts_as_taggable_on tags within a Post

I've got a very simple setup of posts with associated tags. When I 'show' a post I want to be able to link to each one of those tags BUT it seems to only link to the tag :id that shares the :id of the post I'm showing.
My code:
<% #post.tag_list.each do |tag| %>
<%= link_to tag, tag_path() %>
<% end %>
Let's say I'm looking at post number 2, the above will only link me to /tags/2 , no matter which tag I click on. I'm sure the answer is embarrassingly simple but it's driving me crazy. Thanks so much.
Pass tag to your route helper:
<% #post.tag_list.each do |tag| %>
<%= link_to tag, tag_path(tag) %>
<% end %>
Update:
Change tag_list to tags:
<% #post.tags.each do |tag| %>
<%= link_to tag, tag_path(tag) %>
<% end %>

Ruby on Rails: How to print contents of variable in view and make checkbox?

I am trying to create a checklist in rails using form_for. This checklist is taken from a table which I gained in the create action of my sign_ins controller:
#content = OrientationContent.where(site_id: session[:site_id])
In my view I want to use the form_for helper to iterate through the list in #content:
<%= form_for(:sign_ups ) do |f| %>
<% #content.each do |c| %>
<%= f.check_box nil %> <%= c %> <br>
<% end %>
<% end %>
However this is not working and it produces two square brackets on the page: [].
How do I go through the list and print the name while creating a check box on the left of it? The check box does not have any meaning or data, I just need it present for reference.
Solved:
In the controller, need to pluck an individual field:
#content = OrientationContent.where(site_id: 1).pluck(:content)
In the view, structure as so:
<%= form_for(:sign_ups) do |f| %>
<% #content.each do |c| %>
<%= f.check_box nil %> <%= c %> <br>
<% end %>
<% end %>

Rails: Update only when checkbox is checked

I want to update a model - only lines where the checkox is clicked and insert a remark
View:
<%= form_tag update_fb_instruction_users_path, :method => :put do %>
<% #user_wishes.each do |u| %>
<%= u.user.name %>
<%= fields_for "instruction[]", u do |f| %>
<%= f.text_field :remark_tl %>
<% end %>
<%= check_box_tag "instruction_user_ids[]", u.id %>
<% end %>
Controller:
def update_fb
params[:instruction_user_ids].each do
#check = InstructionUser.update(params[:instruction].keys, params[:instruction].values).reject { |p| p.errors.empty? }
end
The issue there is that they all have the same name. So whatever value the last one is, that's what it will be in the request params.
It's a bit old, but you might want to check out the railscast here: http://railscasts.com/episodes/73-complex-forms-part-1. The basic idea is to use fields_for on top of each user object. I haven't done it myself before, otherwise i'd write a full solution :).

Add interval text if previous element exists

Using Rails 3. This is a front-end design question.
Goal:
Contact | Email | URL
show.html.erb:
<% if !#shop.contact.blank? %>
<%= #shop.contact %>
<% end %>
<% if !#shop.email.blank? %>
<%= #shop.email %>
<% end %>
<% if !#shop.url.blank? %>
<%= link_to #shop.url, #shop.url, :target => "_blank" %>
<% end %>
How do I put in | only when the previous and after element has values? At current stage, if there is no value, nothing is output.
Many thanks.
<% url = link_to(#shop.url, #shop.url, :target => "_blank") if #shop.url.present? %>
<%= [#shop.contact, #shop.email, url].select(&:present?).join(" | ") %>
This creates an array of all your elements, selects those which have a value (as present? is the opposite of blank?) and then joins each element of the remaining array by putting a pipe between them.
That said, if you have more complex logic, you should probably create a helper method. The ERB templates are not a good place for complex logic. This above is bordering acceptable.

Style tag_list class from Acts as Taggable On

I'm undoubtedly missing something obvious, but how should I style the output of individual tags produced by <%= user.tag_list %>?
<%= user.tag_list, :class => 'tags' %> doesn't work...
Note, I want to style the individual results, not the whole block.
Thanks!
This should do the trick:
<% user.tag_list.each do |tag| %>
<span class="tags"><%= tag %></span>
<% end %>

Resources