Rails Same page with url paramaters only change - ruby-on-rails

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

Related

If condition in html.erb rails

How can I place an if condition in the HTML, in pseudocode, I'd like to achieve the following:
if #time.status is pending
place edit and delete below the message
else
not just display
end
This is my current code:
<% #value.each do |s| %>
<p><strong>Message:</strong>
<%= s.message %>
</p>
<p><strong>Date</strong>
<%= s.date %>
</p>
<p><strong>Status:</strong>
<%= s.status %>
</p>
<p>
<%= link_to 'Edit', value(), %>
<%= link_to 'Delete', value(), %>
</p>
If you have access to #time and status is an attribute of it, with a value that can be "pending" (thing you didn't add in the question), then you can do:
<% if #time.status == 'pending' %>
<!-- place edit and delete -->
<% else %>
<% #value.each do |s| %>
<p>
<strong>Message:</strong>
<%= s.message %>
</p>
<p>
<strong>Date</strong>
<%= s.date %>
</p>
<p>
<strong>Status:</strong>
<%= s.status %>
</p>
<p>
<%= link_to 'Edit', value(), %>
<%= link_to 'Delete', value(), %>
</p>
<% end %>
<% end %>
It is same as your iteration
<% if #time.status=="PENDING" %>
#your html code for edit in delete
<%end%>

Save returning true, object not being saved

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' %>.

Block in rails 3

My view:
<%= form_tag(rate_url) do %>
<%= hidden_field_tag :prod_id, params[:product_id] %>
<%= hidden_field_tag :rating_set_id, params[:rating_set_id] %>
<span class = "heading"> Recommendations </span>
<div><%= submit_tag 'Submit Ratings', :class => " btn right" %></div>
<span id = "rate_more_link">
<%= link_to "Rate More Products", products_path(:rating_set_id => params[:rating_set_id])%>
</span>
<br/>
<div id ="prods_container">
<% #recommendations.each do |rec| %>
<% url_raw = URI.parse("url_to_parse") %>
<% url = Net::HTTP.get_response(url_raw).body %>
<% if url.empty? %>
<% #title = "Product Unavailable via API" %>
<% #url = "#{rec.wmt_id}" %>
<% #cover_img = "_180X180.jpg" %>
<% #price = "Product Unavailable via API" %>
<% else %>
<% begin %>
<% #response1 = JSON.parse(url) %>
<% #title = #response1["ProductName"]%>
<% #url = "{#response1["ProductUrl"]}"%>
<% #cover_img = #response1["ImagePath"]%>
<% #price = #response1["currentItemPrice"]%>
<% rescue %>
<% end %>
<% end %>
<div id ="prod">
<span class = "radio_button">
<%= hidden_field_tag "recommendation_ratings[#{rec.id}][recommendation_id]", rec.id %>
<%= radio_button_tag "recommendation_ratings[#{rec.id}][rating]", '3'%> Good
<%= radio_button_tag "recommendation_ratings[#{rec.id}][rating]", '2'%> Fair
<%= radio_button_tag "recommendation_ratings[#{rec.id}][rating]", '1'%> Bad
<%= radio_button_tag "recommendation_ratings[#{rec.id}][rating]", '0'%> N/A
</span>
<div>
<a href='<%= #url %>' target="_blank">
<img src='<%= #cover_img %>' class='product_image_rec_table'></img>
</a>
</div>
<div class = "rec-desc">
<div class = "small"><b>Wmt ID: </b><%= rec.wmt_id %></div>
<div class = "small"><b>Title: </b><%= #title %></div>
<div class = "small"><b>Price: </b>$<%= #price %></div>
<div class = "em">
<b>Current Rating: </b>
<% rec.ratings.each do |rating_phin| %>
<%= rating_phin.label %>
<% end %></div>
<br/>
</div>
<div id="rec_note_text">
<%= text_field_tag "recommendation_ratings[#{rec.id}][notes]", "#{rec.notes}" ,:id => "rec_note_text", :placeholder => 'Enter Notes..'%>
</div>
</div>
<% end %>
<% end %>
</div>
I'm trying to move the <% end %> from the Current Rating block to the end so that I can call the rating_phin variable in my text_field_tag, however when I move the <% end %> to the end of my view, everything after the Current Rating block is not being rendered. What am I doing wrong? How can I update the rating_phin.notes instead of rec.notes in my text_field_tag at the end of my view?
That rating_phin variable only exists within the scope of that each loop.
If you require it later, you will have to loop again:
<div id="rec_note_text">
<% rec.ratings.each do |rating_phin| %>
<%= text_field_tag "recommendation_ratings[#{rec.id}][notes]", "#{rec.notes}" ,:id => "rec_note_text", :placeholder => 'Enter Notes..'%>
<% end %>
</div>
I'm not sure why you would expect that moving the <% end %> tag would be the right thing to do.
If you're getting lost inside of ERB, which is not especially difficult considering how fussy and convoluted HTML can get, especially when there's live Ruby in it, you might want to try HAML. It would reduce this down to about half the number of lines and remove a lot of the <% %> and closing tag debris that tends to make ERB hard to read.

Passing a parent_id via a form using the Ancestry gem

I am new to rails, I am using the ancestry gem and I am having trouble passing the parent_id via a form.
<% title "Messages" %>
<% for message in #messages %>
<div class="message">
<div class="created_at"><%= message.created_at.strftime("%B %d, %Y") %></div>
<div class="content">
<%= message.content %>
</div>
<div class="actions">
<%= link_to "Reply", new_message_path(:parent_id => message) %> |
<%= link_to "Destroy", message, :confirm => "Are you sure?", :method => :delete %>
</div>
</div>
<% end %>
<%= semantic_form_for #message do |f| %>
<%= f.error_messages %>
<p><%= f.select :content, [['United States','2'],['England','3'],['London','4']] %></p>
<p><%= link_to "Reply", new_message_path(:parent_id => :content) %></p>
<% end %>
I am trying to assign a number (if the user selects 'United States' the number will be '2') to some variable and pass that variable when the user click reply as <%= link_to "Reply", new_message_path(:parent_id => some_variable) %>
You are doing it wrong. Rethink the problem. You probably want to do something like
<%= semantic_form_for #message do |f| %>
<%= f.error_messages %>
<p><%= f.select :parent_id, [['United States','2'],['England','3'],['London','4']] %></p>
<p><%= f.submit "Reply" %></p>
<% end %>
And define needed routes.

Ajax and voting

I have a parent class Car and the /cars/show page lists all of the reviews associated with it. The reviews can be voted on. I am having some difficulty getting the :votes_count to update by JavaScript.
/votes/create.js.erb
$("#votes").html("<%= review.votes_count %>") // this does not work
/cars/show
<% #car.reviews.each do |review| %>
<p id='votes'><%= pluralize(review.votes_count, 'vote') %></p>
<% if current_user %>
<%= form_for(#vote, :remote => true) do |f| %>
<%= f.hidden_field "review_id", :value => review.id %>
<%= f.hidden_field "user_id", :value => current_user.id %>
<%= f.submit "vote" %>
<% end %>
<% end %>
<br />
<%= review.content %>
<% end %>
create.js.erb
<% #car.reviews.each do |review| %>
$("#review_<%= review.id %>").html("<%= pluralize(review.votes_count, 'vote') %>");
<% end %>

Resources