How to save uncheked check boxes - ruby-on-rails

how i get to save unchecked chekboxes on rails?
I researched some links, but i as unable to find a working solution for me.
i got:
<% #book.each do |book| %>
<div>
<%= check_box_tag "orb[book_ids][]", book.id, #orb.books.include?(book) %>
<%= book.nome %>
</div>
<% end %>
when i uncheck all checkboxes, it didn't save :P
I tried to use a hidden field, but it gave me the error "there no book with id=0"

Add this:
<%= hidden_field_tag 'orb[book_ids][]', '' %>
You form should looks like:
<% #book.each do |book| %>
<div>
<%= check_box_tag "orb[book_ids][]", book.id, #orb.books.include?(book) %>
<%= book.nome %>
</div>
<% end %>
<%= hidden_field_tag 'orb[book_ids][]', '' %>

Related

Rails: how to create a list with conditional links

I am working on a RAILS application where I create a view which lists all available resources of a given model species.rb.
The view partial is:
<% i= #s
for species in #species %>
<%= species.name %>, <%= species.author.surname %> <%= species.author.initial_name %>
<% i -= 1
end %>
Some of the resources species have an related article, others have only the name. I would like to loop through the partial and add a link only to the entries which have an associated article.
Something like: add link if species.article is present else just put species.name without link + loop through it for all entries.
How can I do this?
UPDATE:
Thanks to #jvillian and #fool-dev I was able to progress. In my case I wanted to add a link if the resource has a description in a description row of its table.
<% #species.each do |species| %>
<div class="entry">
<p><i><%= link_to_if(species.txtlandscape.present?, "#{species.name}, #{species.author.surname}, #{species.author.initial_name}. 2014", :controller => 'projects', :action => 'show', :id => species) %></i></p>
</div>
<% end %>
Now that a link is added I was wondering if it can be used to load a partial to a target such as in, where ArticleRequest is a JS function I have:
<% # species.each do | species | %>
<div id="species-<%= species.id %>" class="species-entry">
<a onClick="ArticleRequest('/species/show/<%= species.id %>', 'species-<%= species.id %>');">
<p><%= species.name %></p>
</a>
</div>
<% end %>
Until I find a way to do this with link_to_if, I will use something like:
<% for species in #species %>
<% if species.txtlandscape.present? %>
<a onClick="ArticleRequest('/species/show/<%= species.id %>', 'species-<%= species.id %>');">
<p><%= species.name %>, <%= species.author.surname %> <%= species.author.initial_name %></p>
</a>
<% else %>
<p><%= species.name %>, <%= species.author.surname %> <%= species.author.initial_name %></p>
<% end %>
<% end %>
According to the docs, it seems you should be able to do:
<% #species.each do |specie| %>
<%= link_to_if(specie.article, specie.name, specie_article_path(specie.article)) %>
<% end %>
I made the path name up, you'll have to make that match your actual routes.
BTW, this:
for species in #species
Is super non-idiomatic.
You can do this, see the below
<% for species in #species %>
<% if species.article.present? %> #=> I thin it will be articles because table name is articles, anyway, you know better
<%= link_to species.name, link_path(species.article) %>, #=> on the link_path it will be your proper link just replace this
<% else %>
<%= species.name %>,
<% end %>
<%= species.author.surname %> <%= species.author.initial_name %>
<% end %>
You can do this with Rails each method like below
<% #species.each do |species| %>
<% if species.article.present? %> #=> I thin it will be articles because table name is articles, anyway, you know better
<%= link_to species.name, link_path(species.article) %>, #=> on the link_path it will be your proper link just replace this
<% else %>
<%= species.name %>,
<% end %>
<%= species.author.surname %> <%= species.author.initial_name %>
<% end %>
Or you can use link_to_if it is also most easier to understand
<% #species.each do |species| %>
<%= link_to_if(species.article.present?, "#{species.name},", link_path(species.article)) %>
<%= species.author.surname %> <%= species.author.initial_name %>
<% end %>
Hope it will help.

Pushing a Locale Variable to Partial in Rails 4.2

I have a partial that pulls the list of games someone has in their Steam Library
<h1><%= header %></h1>
<% player = SteamWebApi::Player.new(steamId)
owned_games = player.owned_games(include_appinfo: true)%>
<% owned_games.each do |game| %>
<%= game['playtime_2weeks'] %>
<% if game['img_icon_url'].blank? %>
<%= image_tag 'http://www.readyicons.com/IconSets/Sky_Light_(Basic)/32x32-no.png' %>
<% else %>
<%= image_tag "http://media.steampowered.com/steamcommunity/public/images/apps/#{game['appid']}/#{game['img_icon_url']}.jpg" %>
<% end %>
<%= game['name'] %><br>
<% end %>
And it's base view feeds off of current_user.player_steam_id - A field in SQL.
Originally this was nice to show only your present games, but I want to do a few things with it.
A) When you check someones profile, you see their list.
B) You can search with a users ID, to pull the query results.
My initial issue is using (XXXXXX is actually a number, so I don't stringify it(Do I?)
<%= render :partial => 'game_list', :locals => {:header => "Welcome to His Page!", :steamId => XXXXXXXXXX } %>
And the "Welcome to His Page!" is getting passed, but I'm having a lot of issues getting :steamId to render.
In the partial view I've tried rendering it with <%= steamId %> <% steamId %> and :steamId
The current and :steamId are the only two that don't break the page, but it doesn't render anything. I'm fairly new to this so trying to figure it out, but I've found numerous posts that say I'm doing it right.
I'm welcome to any input.
Also - Since I plan to use this widely, should I look into making it a Controller? Or would the partial suffice?
(P.S. I'm typically a Node guy, so I tried debug+console.log(steamId) a lot to see how it's rendering, but I didn't get results in console.log, and debug didn't do anything. How do you normally debug something like this in RoR?
Thanks for any input!
EDIT:
Additional Info:
The issue with this was partially in the partial.
<h1><%= header %></h1>
<% player = SteamWebApi::Player.new(steamId)
owned_games = player.owned_games(include_appinfo: true)%>
<% owned_games.each do |game| %>
<%= game['playtime_2weeks'] %>
<% if game['img_icon_url'].blank? %>
<%= image_tag 'http://www.readyicons.com/IconSets/Sky_Light_(Basic)/32x32-no.png' %>
<% else %>
<%= image_tag "http://media.steampowered.com/steamcommunity/public/images/apps/#{game['appid']}/#{game['img_icon_url']}.jpg" %>
<% end %>
<%= game['name'] %><br>
<% end %>
By modifying it to owned_games definition, and player definition are on separate lines, it resolved successfully. This was just a not-reviewing-what-I-copied/pasted fail on my part.
Resolved code:
<h1><%= header %></h1>
<% puts steamId %>
<% player = SteamWebApi::Player.new(steamId) %>
<% owned_games = player.owned_games(include_appinfo: true)%>
This is my steam ID <%= steamId %>
<% owned_games.each do |game| %>
<%= game['playtime_2weeks'] %>
<% if game['img_icon_url'].blank? %>
<%= image_tag 'http://www.readyicons.com/IconSets/Sky_Light_(Basic)/32x32-no.png' %>
<% else %>
<%= image_tag "http://media.steampowered.com/steamcommunity/public/images/apps/#{game['appid']}/#{game['img_icon_url']}.jpg" %>
<% end %>
<%= game['name'] %><br>
<% end %>
However, I am only able to have it render if it pulls the player_steam_id from the DB.
So for instance if I have
:steamId => 76561198017108025
It won't work, but if I have
:steamId => User.player_steam_id it does work.
(Again, 'User' has a column in SQL that stores their player_steam_id)
I want to be able to call this on the 'Submit' of a search button, which is why I'm trying to see how to get the render to work with a specific number.
Why could that be the case? I have tried logging steamId with the view, and I see it being passed through as a normal integer. My only logical assumption is that the view loads, calls the API with 'steamId' THEN renders steamId but I'm just guessing?
Update Trial-n-Error
So I tried out using something like
<h1><%= header %></h1>
<% appCall = steamId %>
<% player = SteamWebApi::Player.new(appCall) %>
<% owned_games = player.owned_games(include_appinfo: true)%>
<%= appCall %>
<%= owned_games.success %>
<% owned_games.each do |game| %>
<%= game['playtime_2weeks'] %>
<% if game['img_icon_url'].blank? %>
<%= image_tag 'http://www.readyicons.com/IconSets/Sky_Light_(Basic)/32x32-no.png' %>
<% else %>
<%= image_tag "http://media.steampowered.com/steamcommunity/public/images/apps/#{game['appid']}/#{game['img_icon_url']}.jpg" %>
<% end %>
<%= game['name'] %><br>
<% end %>
I am able to get an output with the puts, and also be using the 'player.success' function of the API. Which returns "True", and puts steamId and app both render out the ID.
I also tested printing out the entire array before the 'for', and did notice it gives me all of the data.
true #<OpenStruct count=31, games=[{"appid"=>300, "name"=>"Day of Defeat: Source", "playtime_forever"=>48,
So the issue lies in the 'for' section of the partial, I'm thinking. Would I need to run the for in the main document, and feed it data from the partial? That seems of...
Is game_list a partial or is it a regular view? A partial will begin with an underscore as the file name. _game_list.html.erb
Try this
<%= render 'game_list', :header => "Welcome to His Page!", :steamId => XXXXXXXXXX %>
Then you can render steam ID like this <%= steamId %> inside of the partial.
Edit:
If you are not referencing steamId in direct output area you don't need to wrap it in <%= %>
Try SteamWebApi::Player.‌​new(steamId) instead of SteamWebApi::Player.‌​new(<%= steamId %>)
Another Edit
Try this
<h1><%= header %></h1>
<% player = SteamWebApi::Player.new(steamId)
owned_games = player.owned_games(include_appinfo: true)%>
This is my steam ID <%= steamId %>
<% owned_games.each do |game| %>
<%= game['playtime_2weeks'] %>
<% if game['img_icon_url'].blank? %>
<%= image_tag 'http://www.readyicons.com/IconSets/Sky_Light_(Basic)/32x32-no.png' %>
<% else %>
<%= image_tag "http://media.steampowered.com/steamcommunity/public/images/apps/#{game['appid']}/#{game['img_icon_url']}.jpg" %>
<% end %>
<%= game['name'] %><br>
<% end %>
I added
This is my steam ID <%= steamId %>
The issue ended up being the Partial I had used as a template. It never modified the array of 'games' into 'game', so when it was iterating through the games 'each' it never returned any results.
My final partial works as intended.
<h1><%= header %></h1>
<% player = SteamWebApi::Player.new(steamId) %>
<% owned_games = player.owned_games(include_appinfo: true)%>
<% game = owned_games.games %>
<% games.each do |game| %>
<%= game['playtime_2weeks'] %>
<% if game['img_icon_url'].blank? %>
<%= image_tag 'http://www.readyicons.com/IconSets/Sky_Light_(Basic)/32x32-no.png' %>
<% else %>
<%= image_tag "http://media.steampowered.com/steamcommunity/public/images/apps/#{game['appid']}/#{game['img_icon_url']}.jpg" %>
<% end %>
<%= game['name'] %><br>
<% end %>
Once I added the additional modifier, it was able to render the list successfully.

Change checkbox to change in db in rails?

I have a table "User" with "admin" is "true" or "false". I show it with checkbox_feild. How can i change db if i clicked in checkbox? Thanks all.
<% #users.each do |user| %>
<%= form_for user, do |f| %>
<div class="field">
<%= f.check_box :admin%>
</div>
<% end %>
<% end %>
One way is to write a jquery script that submits the form when checkbox is clicked
<% #users.each do |user| %>
<%= form_for user,:html => {class: 'form'} do |f| %>
<div class="field">
<%= f.check_box :admin%>
</div>
<% end %>
<% end %>
Jquery script would be:
$('.form input[type=checkbox]').change(function(){
$(".form").submit();
});

Rails form_for check_box

I'm trying to create a basic survey app. On my take survey page I'm looping through and displaying each answer option as a radio button or checkbox as a form_for to create a user's choice. The choices are working great for the questions that are single choice (or radio buttons), but they aren't saving for multi select questions. I'm pretty sure this has to do with the form I have for the checkbox.
It seems like I should do
<%= f.check_box :answer_id, answer.id %> <%= answer.title %> <br>
similar to how I'm creating the radio button but that throws an error
undefined method `merge' for 14:Fixnum
Here's my code that displays:
<h3>Questions:</h3>
<ul><% #survey.questions.each do |question| %>
<li><p><%= question.title %></p></li>
<% choice = question.choices.build %>
<% if question.single_response == true %>
<%= form_for [question, choice] do |f| %>
<% question.answers.each do |answer| %>
<%= f.radio_button :answer_id, answer.id %> <%= answer.title %><br>
<% end %>
<%= f.hidden_field :survey_id, value: #survey.id %>
<%= f.submit %>
<% end %>
<br />
<% else %>
<%= form_for [question, choice] do |f| %>
<% question.answers.each do |answer| %>
<%= f.check_box :answer_id %> <%= answer.title %> <br>
<%= f.hidden_field :survey_id, value: #survey.id %>
<% end %>
<%= f.submit %>
<% end %>
<br />
<% end %>
<% end %>
</ul>
Any idea what I need to do to get it to save the answer_id to the choice so that it actually creates the choice?
Thanks!
This question is a few years old but I think it deserves a better answer. Since you are using form_for (a model backed form), then you probably want to use the form_for check_box method that you originally tried to use. In your case, it would look like this:
<%= f.check_box :choice, { :multiple => true }, answer.id, false %>
Here is the doc on this.
For checkboxes, you actually want to return an array as the parameter. There is a little funny syntax to this because we don't actually want to use the form builder methods. It should look something like this (adapt to your specific model names and methods)
<%= check_box_tag 'choice[answer_ids][]', answer.id %>
Using this syntax should tell Rails to compile all of the checked checkbox values into an array.
This Railscast goes over the topic.

Using a Form_for with an each loop

I'm trying to create a form in Rails that allows a user to select certain photos from a larger list using checkboxes. Unfortunately, I haven't found any similar examples and most posts out there are unhelpful. Any ideas on how to solve this?
<div>
<%= form_for :photos, url: trip_path, method: "PUT" do |f| %>
<% #photos.each_with_index do |image, index|%>
<img src="<%= image.url %>" ><br>
<span> <%=image.caption%> | <%=image.lat %> | <%= image.long %>
<%= f.hidden_field "url", :value => image.url %>
<%=check_box_tag('photo') %>
</span>
<% end %>
<%= f.submit 'Submit' %>
<% end %>
</div>
API docs states that a form_for
creates a form and a scope around a specific model object
so, you cannot use it with a collection.
A possible way to do it, is to use the form_tag instead of form_for and check_box_tag (which you already have).
The behavior you've depicted is categorically impossible using form_form. However, if you're willing to forgo form_for (and there's no reason why you shouldn't, given your criteria), you can imitate the behavior depicted by nesting a foreach loop – each loop containing a form_for block – within a form_tag:
<div>
<%= form_tag trip_path, method: "PUT" do |f| %>
<% #photos.each do |photo|%>
<img src="<%= photo.url %>" ><br>
<span> <%= photo.caption%> | <%= photo.lat %> | <%= photo.long %>
<%= fields_for "photos[#{photo.id}]", photo do |p| %>
<%= p.hidden_field 'url', :value => photo.url %>
<%= p.check_box 'photo'
<% end %>
</span>
<% end %>
<%= f.submit 'Submit' %>
<% end %>
</div>

Resources