Incrementing model integer via custom method and form_for - ruby-on-rails

I'm extremely new to Rails and so I apologize if this is a simple question. I am attempting to build a simple site that allows users to vote as whether or not they liked the lunch that day. Lunches are associated with providers (restaurants) and thus providers will have many different "lunches."
I know it is possible to auto increment by adding this to a method in the controller (discussed more here) and my current setup looks like this:
The view:
<%-# Show current like and dislike counts. -%>
<div> Likes: <%= #lunch.liked %> </div>
<div> Dislikes: <%= #lunch.disliked %> </div>
<div>
<%= form_for :lunch url: lunch_path, method: :put do |f| %>
<div><%= f.hidden_field :disliked %></div>
<div><%= f.hidden_field :id %></div>
<%= f.submit "Disike Lunch", class: "btn btn-large btn-primary" %>
<% end %>
</div>
The controller:
def downvote_lunch
#lunch = Lunch.find(params[:id])
#lunch.increment!(:disliked)
redirect_to lunch_path
end
Routes:
resources :lunches
...
match '/dislike', to: 'lunches#downvote_lunch', via: 'get'
However, when I click on the "Dislike Button" it tells me that there is no view for "update" - I've defined update as an empty method in the controller, but I'm not sure why it's accessing update - all I'm looking for is to reload lunches/:id with the updated count for "disliked."
My question: How do I get the form to point to the custom "downvote_lunch" method, and have it redirect back to the lunches/:id after successfully incrementing the "dislikes" integer in the model?

For it you have to do
In view
<div>
<%= form_for :lunch url: downvote_lunch_lunch_path(params[:id]), method: :put do |f| %>
<div><%= f.hidden_field :disliked %></div>
<div><%= f.hidden_field :id %></div>
<%= f.submit "Disike Lunch", class: "btn btn-large btn-primary" %>
<% end %>
</div>
In routes
resources :lunches do
member do
put :downvote_lunch
end
end
In controller
def downvote_lunch
#lunch = Lunch.find(params[:id])
#lunch.increment!(:disliked)
redirect_to #lunch
end
it will work as you want

Related

No method error occurs in rails while creating a new text_field

I am trying to add a text field via form and setting the name as code.
My form calls a action verify in users controller. But when I run it
shows no method error code. I am new to rails can anyone help. I need to pass code as a params. I already created a user sign up and signin page.
<%= form_for(#user, :action => 'verify') do |f| %>
<%= f.label :code %>
<%= f.text_field :code %>
<%= f.submit "Verify", class: "btn btn-large btn-primary" %>
// a action in usercontroller
def verify
end
The user model probably does not have a code field. So the application is giving an error. If you just want to send code, use form_tag instead of form_for.
<%= form_tag verify_action_path do %>
<%= label_tag :code %>
<%= text_field_tag :code %>
<%= submit_tag "Verify", class: "btn btn-large btn-primary" %>
<% end %>

Posting a variable to another controller

I have a welcome controller/view.
On the index.html.erb I have a simple form that takes in one value:
<%= form_tag do %>
<div>
<%= label_tag(:zip, "Enter Zipcode to search in:") %>
<%= text_field_tag(:zip) %>
</div>
<%= submit_tag("Search") %>
<% end %>
Upon hitting the submit button I'd like to pass the zip variable to another controller called "theaters". The variable doesnt need to be saved in any kind of model, its just being used to execute an API call in the Theaters controller.
Whats the simplest way to do this?
Thanks
Try this:
<%= form_tag({controller: "theaters", action: "index"}) do %>
<div>
<%= label_tag(:zip, "Enter Zipcode to search in:") %>
<%= text_field_tag(:zip) %>
</div>
<%= submit_tag("Search") %>
<% end %>
Hope it helps.
Make a route for your method in TheatresController, lets assume your method name is get_zip
post '/get_zip' => 'theaters#get_zip', as: "zip"
Create your form
<%= form_tag(url: zip_path, method: :post) do %>
<div>
<%= label_tag(:zip, "Enter Zipcode to search in:") %>
<%= text_field_tag(:zip) %>
</div>
<%= submit_tag("Search") %>
<% end %>
Access it inside your method:
def get_zip
#zip = params[:zip]
#your logic
end

Rails - two forms on one page

I have two forms on one view page. One of these is an update form. The other form is a single button that goes to the same controller action but does not actually update the form. Here is the first form:
<%= form_for [#project, #schedule] do |f| %>
<%= f.fields_for :tasks do |builder| %>
<%= render 'task_fields', :f => builder %>
<% end %>
<p><%= link_to_add_fields "Add task", f, :tasks %>
<p><%= f.submit "Submit" %></p>
<% end %>
And here is the second form:
<%= form_tag project_schedule_path(#project, #schedule), method: "patch" do %>
<div><%= hidden_field_tag :emp_accepts, true %></div>
<%= submit_tag "Accept schedule", class: "btn btn-large btn-primary" %>
<% end %>
The problem is the strong parameters. The strong parameters require :schedule, which only the first form provides. So when I try to use the second form, an error is returned. Here are the strong params:
def schedule_params
params.require(:schedule).permit(:emp_accepts,
tasks_attributes: [:title, :content, :_destroy])
end
you should change the second form to
<%= form_for [#project, #schedule], http: { method: 'patch' } do |f| %>
<div><%= f.hidden_field :emp_accepts, value: true %></div>
<%= f.submit 'Accept schedule', class: 'btn btn-large btn-primary' %>
<% end %>
It looks like you should really use different controller methods for each action. It would make it easy for you to make changes in the future.

Rails with searching dependent value in create a new record

Recently started to study rails and faced with the next problem:
I have two tables Inboxes(request) and Equipments. Inboxes has many Equipments through InboxEquipment.
Since the equipment can be many, the searching for the right is difficult. I need to add field quick search or filter values of table Equipments. I think this can be done using Ajax, but I don't know him. Something similar is described in "Episode #240 Search, Sort, Paginate with AJAX", but I don't know how to make to search has been in the create a new record
My current working code:
../controllers/inbox_controller.rb
def new
if signed_in?
#inbox = Inbox.new(params[:inbox])
#partners = #inbox.partners.build(params[:partners])
#equipments = #inbox.equipments.search(params[:search])
end
end
../inboxes/new.html.erb
<% if signed_in? %>
<%= render 'shared/inbox_form' %>
<% end %>
../shared/_inbox_form.html.erb
<%= form_for #inbox, :html => { :multipart => true } do |f| %>
<div class="new_application">
<h2>Входящая заявка</h2>
<%= render 'shared/error_messages', object: f.object %>
<div class="fields_inbox">
<h4><center>Контрагент:</center></h4>
<%= f.select(:partner_id, options_for_select(Partner.all.collect {|x| [x.name, x.id]}, :include_blank => "Выберите контрагента")) %>
<%= f.select(:equipment_ids, options_for_select(Equipment.all.collect { |z| [z.name, z.id] }), {}, {:multiple=>true, :size => 10}) %>
<h4><center>Тип заявки:</center></h4>
<%= f.select(:application_type, ["Новая","Принята","На рассмотрении","Завершена"], :include_blank => "Выберите тип зявки") %>
<center>
<%= f.submit "Добавить", class: "btn btn-large btn-primary" %>
<%= link_to "Назад", :back, :class => "btn btn-large btn-primary" %>
</center>
</div>
</div>
<% end %>
EDIT
Add two images as works now and how to
So now working http://s12.postimg.org/vt91ceqkd/inbox.jpg
So it should work http://s22.postimg.org/z20zb8c9d/inbox_correct.jpg
What do I need to add to my code to make it work as I need to (Example in second image)?

Rails - 2 forms one model

I'm struggling with this one, basically I want two forms to submit to the one model. When the first form is complete, the second one is rendered which will contain the remaining information which the model requires. Ideally I would like to combine the two returned hashes and save them together at once. If there is a more sensible way of doing this please let me know. Thanks in advance.
app/controllers/books_controller.rb
class BooksController < ApplicationController
def new
#book = #library.books.build
#user = current_user
end
def create
#book = #library.books.build(params[:book])
if #book.total_pages.nil?
#book_first_page = #book
render 'new'
else
#book.update_attributes(#book_first_page)
if #book.save
redirect_to root_path
else
render 'new'
end
end
end
end
app/views/books/new.html.erb
<% if #book.title.nil? %>
<%= form_for [:library, #book] do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="form-group input-group-sm">
<%= f.label :title, 'What is the title of the book?' %>
<%= f.text_area :title, class: "form-control" %>
</div>
<%= f.submit "Submit Title", class: "btn btn-large btn-primary" %>
<% end %>
<% else %>
<%= form_for [:library, #book] do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="form-group input-group-sm">
<%= f.label :total_pages, "How many pages?:" %>
<%= f.text_field :total_pages, class: "form-control" %>
</div>
<%= f.submit "Submit Number of Pages", class: "btn btn-large btn-primary" %>
<% end %>
You might also use a single form for all your data, but initially showing only the first portion of the form, and then employ javascript to hide the first part and show the second part when the first part is complete.
You should consider using accepts_nested_attributes_for
For example, see http://railscasts.com/episodes/196-nested-model-form-revised
why you need to update in two form. use just one form

Resources