Save returning true, object not being saved - ruby-on-rails

Save returning true, object not being saved. I can't really tell why. I suspect I need to explicitly tell rails it is modifiable but I am not sure how. Any idea what can be wrong?
view
<%= form_tag(set_email_settings_path) do %>
<div class="form-group">
<% if #logged_in_profile.email_settings != 'off' %>
<%= submit_tag 'Turn off emails', class: 'btn btn-default btn-about', name: 'email_notification' %>
<% else %>
<%= submit_tag 'Turn on emails', class: 'btn btn-default btn-about', name: 'email_notification' %>
<% end %>
</div>
<% end %>
controller
if params[:email_notification] == 'Turn off emails'
logged_in_profile.email_settings = 'off'
else
logged_in_profile.email_settings = 'normal'
end
if logged_in_profile.save
redirect_to email_settings_path
else
redirect_to settings_path
end
route
post "set_email_settings" => "profiles#set_email_settings", :as => 'set_email_settings'

Try this as the form instead, because I do not think you are submitting any values in the form...
<%= form_tag(set_email_settings_path) do %>
<div class="form-group">
<% if #logged_in_profile.email_settings == 'normal' %>
<%= hidden_field_tag 'email_notification', 'Turn off emails' %>
<%= submit_tag 'Turn off emails', class: 'btn btn-default btn-about' %>
<% else %>
<%= hidden_field_tag 'email_notification', 'Turn on emails' %>
<%= submit_tag 'Turn on emails', class: 'btn btn-default btn-about' %>
<% end %>
</div>
<% end %>
You will see above, I changed removed the '!='; just good practice to avoid '!='. If there was not an 'else', 'unless' could also be used <% unless #logged_in_profile.email_settings == 'off' %>.

Related

syntax error, unexpected keyword_ensure, expecting keyword_end in my app

Please i need help validating this code.
Using Ruby 2.3.3 & Rails 5.2
i think all ending anchors are given though
<% if current_user.id != user.id %>
<div class="panel panel-default">
<div class="pane-body">
<center>
<% if !current_user.following?(user) %>
<%= form_for(current_user.active_relationships.build) do |f| %>
<div><%= hidden_field_tag :followed_id, user.id %></div>
<%= f.submit "Follow", class: "btn btn-primary" %>
<% end %>
<% else %>
<%= form_for(current_user.active_relationships.find_by(followed_id: user.id),
html: {method: :delete}) do |f| %>
<%= f.submit "Unfollow", class: "btn btn-default" %>
<% end %>
</center>
</div>
</div>
<% end %>
Here
<% else %>
<%= form_for(current_user.active_relationships.find_by(followed_id: user.id), html: {method: :delete}) do |f| %>
<%= f.submit "Unfollow", class: "btn btn-default" %>
<% end %>
</center>
You have the end to close the block for form_for but not for ending the if-else
<% else %>
<%= form_for(current_user.active_relationships.find_by(followed_id: user.id), html: {method: :delete}) do |f| %>
<%= f.submit "Unfollow", class: "btn btn-default" %>
<% end %>
<% end %>
</center>
Properly indenting helps a lot:
<% if current_user.id != user.id %>
<div class="panel panel-default">
<div class="pane-body">
<center>
<% if !current_user.following?(user) %>
<%= form_for(current_user.active_relationships.build) do |f| %>
<div><%= hidden_field_tag :followed_id, user.id %></div>
<%= f.submit "Follow", class: "btn btn-primary" %>
<% end %>
<% else %>
<%= form_for(current_user.active_relationships.find_by(followed_id: user.id), html: {method: :delete}) do |f| %>
<%= f.submit "Unfollow", class: "btn btn-default" %>
<% end %>
<% end %>
</center>
</div>
</div>
<% end %>
Your end on line 13 closed off the form_for do |f| block, meaning you needed one more to close off the if \ else do block

How to pass current_item id in partial after link_to in RoR

I am trying to make a front end from which if clicked on a button "Rent", it will render a Modal of a bootstrap in which I need to get current_item in order to rent that item. I am currently getting the latest item instead of current_item.
_itemviewer.html.erb
<% #items.each_with_index do |item, j| %>
<% if item.user != current_user %>
<%= link_to 'Rent', '#rentModal', class: 'btn btn-success', "data-toggle" => "modal", "data-target" => "#rentModal"%>
<%= render partial: "layouts/rent_modal", locals: {current_item: item } %>
<% end %>
<% end %>
inside _rent_modal.html.erb
<%= simple_form_for [current_item, #acquiretime] do |f| %>
<div class = "add_item_form">
<%= f.input :required_time, as: :date, html5: true %>
<%= f.input :return_time, as: :date, html5: true %>
<%= f.hidden_field :user_id, value: current_user.id %>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<%= f.submit 'Add Item', class: 'btn btn-primary' %>
</div>
<% end %>
the problem is that you are creating many links to your modal with the same id.
<% #items.each_with_index do |item, j| %>
<% if item.user != current_user %>
<%= link_to 'Rent', '#rentModal', class: 'btn btn-success', "data-toggle" => "modal", "data-target" => "#rentModal"%>
<%= render partial: "layouts/rent_modal", locals: {current_item: item } %>
<% end %>
<% end %>
here's it, for each item you create a new link to your modal with the id #rentModal the last modal is the one of the latest item; that may be the cause of being it rendered.
so you may need to create different modals and links ids for each item.
like the code below.
<% #items.each_with_index do |item, j| %>
<% if item.user != current_user %>
<%= link_to 'Rent', "#rentModal_#{j}", class: 'btn btn-success', "data-toggle" => "modal", "data-target" => "#rentModal_#{j}"%>
<%= render partial: "layouts/rent_modal", locals: {current_item: item, index: j } %>
<% end %>
<% end %>
here you can see that we used j to define the Modal id, you need to use that id also in your modal html part, so in your _rent_modal.html.erb you need to use the same id for each item and that is why we passed index as a local to that partial.. Also you need to wrap your code inside _rent_modal.html.erb inside a div with the modal id. you can see how you define a modal here.

Rails check_box form to create has_many relationship

I have a model called account which has_many :options. I want to create a form in which i can list all the options with a checkbox at the side, so the current account can select the options he/she wants inside a form so I can create the has_many relation.
This is what i have
def index
#account = current_account
#options = ['Op 1', 'Op 2', 'Op 3', 'Op 4']
end
and for the view:
<%= form_for(#account, url: options_path) do |f| %>
<% #options.each do |op| %>
<div class="checkbox">
<%= f.check_box(?????, {:multiple => true}, op, nil) %>
</div>
<% end %>
<%= f.submit class: 'btn btn-default' %>
<% end %>
This is obviously not working and I'm pretty sure this is not the right way to achieve what I want to do, so any help would be appreciated.
You could use fields_for:
<%= form_for(#account, url: options_path) do |f| %>
<%= fields_for :options do |options_form| %>
<% #options.each do |option| %>
<div class='checkbox'>
<%= options_form.label option do %>
<%= options_form.check_box option %> <%= option %>
<% end %>
</div>
<% end %>
<% end %>
<%= f.submit class: 'btn btn-default' %>
<% end %>
And in your params, you will get values like: params[:account][:options]['Op1'] with value '1' for true and '0' for false.

Using a bootstrap glyphicon in a Rails button using button_tag or form_for

The seemingly trivial task of using a bootstrap glyphicon in a form submit button is anything but. I can't get button_tag or form_for to work.
button_tag successfully shows the glyphicon, but fails because (looking at the console) it doesn't submit a request to the server. It's dead, nothing happens:
<%= button_tag(type: 'submit', name: nil, class: 'btn btn-default btn-xs', id: 'vote_button',
path: postvoterelationships_path, remote: true) do %>
<span class='glyphicon glyphicon-star-empty' aria-hidden="true"></span>
<%= hidden_field_tag(:voter_callsign, #character.callsign) %>
<%= hidden_field_tag(:voted_id, #post.id) %>
<% end %>
form_for is failing because, while it successfully sends a request, I can't get it to display the glyphicon. The following two variations both just create a large grey button with 'Create Postvoterelationshiop' in the middle:
<%= form_for(#character.active_post_vote_relationships.build, remote: true) do |f| %>
<div><%= hidden_field_tag(:voter_callsign, #character.callsign) %></div>
<div><%= hidden_field_tag(:voted_id, #post.id) %></div>
<%= f.submit do %>
<span class="glyphicon glyphicon-star-empty"></span>
<% end %>
<% end %>
<%= form_for(#character.active_post_vote_relationships.build, remote: true) do |f| %>
<div><%= hidden_field_tag(:voter_callsign, #character.callsign) %></div>
<div><%= hidden_field_tag(:voted_id, #post.id) %></div>
<%= f.submit class: "glyphicon glyphicon-star-empty" %>
<% end %>
How do I get either button_tag to submit the request, or form_for to display the glyphicon?
You could try button_to
<%= button_to postvoterelationships_path, class: 'btn btn-default btn-xs', params: {voter_callsign: #character.callsign, voted_id: #post.id}, remote: true do %>
<span class="glyphicon glyphicon-star-empty"></span>
<% end %>

Rails Same page with url paramaters only change

Actually this is my view am getting values from mysql database and i showed in browser based on the params page=1 page =2 page =3 and i want to change the params[:page] in form next button click.
Based on pages=1,2,3 questions and answers will vary thats why i need url to be change while button click in same page.! like below URL http://localhost:3000/responses/new?page=1 Thanks in advance.
<%= form_for (#response) do |f| %>
<% #questions.each do |pgquestion| %>
<% if pgquestion.group_id == 0 %>
<%= label :pgquest,pgquestion.description %><br><br>
<% else %>
<%= label :pgquest,pgquestion.description %>
<% (1..pgquestion.question_value.to_i).each do |i| %>
<%= radio_button_tag "ans_value[#{ pgquestion.quest_id }]", i %>
<% end %>
<br><br>
<% if pgquestion.question_type == "textarea" %>
<%= text_area "", "ans_value[#{ pgquestion.quest_id }]" ,size: '80x5' %><br><br>
<% else if pgquestion.question_type == "text" %>
<%= text_field "", "ans_value[#{ pgquestion.quest_id }]" %> <br><br>
<% end %>
<% end %>
<% end %>
<% end %>
<div style="text-align: center">
<%= f.submit 'Prev' ,:class => 'btn btn-primary',:name => "previous_button" %>
<%= f.submit 'Next', :class => 'btn btn-primary' %> <br><br>
<p><%= f.submit "Continue" %></p>
<p><%= f.submit "Back", :name => "previous_button" %></p>
</div>
<% end %>
As far as i understand your problem use the will_paginate gem https://github.com/mislav/will_paginate

Resources