Search form with edit_resource_path as action in Rails 5 - ruby-on-rails

I have a resource Invoice and every invoice has an edit path:
http://localhost:3000/invoices/1/edit
On my index page where I will all my invoices I want to have an input field and a submit button to jump right to the edit page for a certain invoice:
<form class="form-inline" action="???" method="get">
<div class="form-group">
<input type="text" class="form-control date" placeholder="Invoice ID" name="id" >
</div>
<button type="submit" class="btn btn-info">Show</button>
</form>
How do I have to define the action so that if I enter for example 1 and click on Show it goes right to my edit method?

I think you're looking for something like this in your html
<%= form_tag find_invoice_url do |f| %>
<%= f.text_field :invoice_number =>
<%= f.submit "Search" =>
<= end =>
then you would need to add a route
post '/invoices/find_invoice', to: "invoices#find_invoice",
as: "find_invoice"
then in your controller something like
def find_invoice
invoice = Invoice.find_by_id(params[:invoice_number])
if invoice
redirect_to invoice_url(invoice.id) #whatever your edit route is
else
redirect_to root_url #anywhere really
end
end

if you are iterating through all the invoices, you wouldn't need an input field.
assuming that you have an invoice obhect, you would just do something like
<%= form_tag edit_invoice_path(invoice), method: "get" do -%>
<%= submit_tag "Show", class="btn btn-info" %>
<% end %>
You may want the button to say edit instead - that's more of a ux "principle of least surprise" issue

Related

Rails: Get checked radio-button value with html input tags

I have a simple structure of contest:
The contest has multiple questions
Each question has multiple answers
possible
For this I feel like what I've created is a good structure:
I am trying to get the answers of a user for each question from the form in my view to my controller. Long story short, I was unable to use the <%= collection_radio_buttons ... %> because the method wouldn't be right. There is not one column in my model for each answer to each question. answer_option is not a column in my questions table it's an association because it's another table... (or would you know how to help on that?)
So I've bypassed this issue by creating loops on answers_options of each question and using html inputs and labels, like this:
<%= simple_form_for #contest, url: contest_send_quizz_path(#contest), method: :post do |f| %>
<%= f.fields_for :questions do |q| %>
<% #questions.each do |question| %>
<h4 class="mt-5"><%= question.title %></h4>
<% question.answer_options.each do |answer_option| %>
<div class="inputGroup">
<input type="radio" name=<%= answer_option.question_id %> value="<%= answer_option.answer %>" id=<%= answer_option.id %> />
<label for=<%= answer_option.id %>><%= answer_option.answer %></label>
</div>
<% end %>
<% end %>
<% end %>
<div class="mt-3">
<span class="button-effect-2">
<%= f.button :button, "Envoyer", type: :submit, class:"text-white" %>
</span>
</div>
<% end %>
However now the problem is fetching those values in the controller. It seems with this question that I'd have to get it with params[:something] and that :something being the name of the input. Is that right? And now that I know that, does putting params[:name] (which is the same for all concerned radio buttons of one question) directly get the checked radio, or is there another thing to do?
Here is what I have for now, there is stuff to ignore since the structure of the rest of my code is bigger than just the contest. This is in the ContestsController:
def show
#contest = Contest.find(params[:id])
authorize #contest
#time_left = seconds_to_units(#contest.deadline - Time.now)
#is_done = #contest.deadline < Time.now
if #is_done
get_winner
end
#questions = #contest.questions.includes([:answer_options])
end
def send_quizz
#contest = Contest.find(params[:contest_id])
#questions = #contest.questions.includes([:answer_options])
authorize #contest
current_user.contests << #contest
user_choice = # TODO : Get checked radio value from view
user_contest = current_user.contests.select { |contest| contest.title == #contest.title }.first
user_contest.questions.each do |question|
question.user_answer = user_choice
end
# TODO : make sure every questions were answered before submitting request
redirect_to contests_path
flash[:notice] = "Ta réponse a été prise en compte"
end
So is there a way to get this value, or should I change my db structure so that each question has one column for each answer? Or maybe another solution? Thanks !
EDIT:
I also tried replacing this:
<input type="radio" name="<%= answer_option.question_id %>" value="<%= answer_option.answer %>" id="<%= answer_option.id %>" />
<label for="<%= answer_option.id %>"><%= answer_option.answer %></label>
With this:
<%= q.check_box :answer_option, name:answer_option.question_id, id:answer_option.id %>
<%= q.label :answer_option, answer_option.answer, for:answer_option.id %>
And getting the value in the controller with user_choice = params[:answer_option] but when I replaced check_box with radio_button, it messed up the name, id etc. values AND I couldn't select anymore.
EDIT 2:
By adding this structure:
<%= q.radio_button :answer_option, answer_option.answer %>
<%= q.label :answer_option, answer_option.answer %>
It works (no errors), however the name is set automatically and to something not specific to each question i.e. contest[questions][answer_option] and the label's for is set to contest_questions_answer_option, therefore clicking on the label does not link to the checkbox.
Managed to retrieve the value of the checkbox with this structure for the radio-buttons:
<%= q.radio_button answer_option.question_id, answer_option.answer %>
<%= q.label "#{answer_option.question_id}_#{answer_option.answer.parameterize.underscore}", answer_option.answer %>
And the controller:
user_choices = params[:contest][:questions]
user_contest.questions.each do |question|
question[:user_answer] = "#{user_choices[:'#{question.id}']}"
question.save
end

Show only names that belong to a code

So in my app a guest searches for their RSVP code in the code/index.html page then clicks on a continue link that brings them to the rsvp page. I am trying to show only names that belong to the specific code on the RSVP but currently all names are showing up. I have passed the code as a param in the link on the index page but when I get to the rsvp page it is showing up in the url with a period which doesn't seem right ex: /rsvp.1
so I am guessing that this is where I am going wrong but I cant figure out why:
associations
Guest belongs_to :code
Code has_many :guest
code/index.html.erb
(page with the link that takes user to rsvp page and passes the params to rsvp page)
<h2><%= link_to 'Continue', rsvp_path(code) %></h2>
code_controller
def index
#codes = Code.search(params[:search])
end
def rsvp
code_id = params[:code]
#guests = Guest.where("code_id", "#{code_id}")
end
code/rsvp.html.erb
<div class="row">
<div class="box">
<div class="col-lg-12">
<form class="row form-inline">
<% #guests.each do |guest| %>
<%= guest.name %>
<% end %>
<div class="form-group">
<p> Will you be attending the wedding of Kristen Albrecht and Chris Alford September 1, 2017? </p>
<button aria-haspopup="true" class="btn btn-default dropdown-toggle" ngbdropdowntoggle="" type="button" aria-expanded="false">
Yes</button>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">No</button>
</div>
</form>
</div>
</div>
</div>
routes
resources :codes
resources :guests
get '/code' =>'codes#code'
get '/rsvp' =>'codes#rsvp'
To make sure that code is a parameter in the URL, you have to make the following changes:
routes.rb
get '/rsvp/:code' =>'codes#rsvp'
code/index.html.erb
<h2><%= link_to 'Continue', rsvp_path(code: code) %></h2>
This way params[:code] will have a value in the code_controller
Okay I figured it out:
I changed the link on the index page to
<%= link_to "Continue", {:controller => "codes", :action => "rsvp", :code_id => code.id }%>
then I changed the controller code to
def rsvp
#guests = Guest.where(code_id: params[:code_id])
end

ROR: HTML input button not posting to controller?

I have very simple HTML (password.html.erb):
<h1>Enter business password to enter:</h1>
<input type="text" id="password" name="enter password"/>
<input type="submit" method="post" name="Submit"/>
This should, on clicking of submit, trigger an action in my controller called 'check':
def check
#entered = params['password']
if #entered == current_customer.business.manager_password_digest
puts("success!")
redirect to '/manage'
else
flash[:danger] = 'Invalid password'
render 'password'
end
end
Here is my route:
get '/password' => 'pages#password'
post '/password' => 'pages#check'
But when I click submit, nothing happens. Is it not possible to use an input in this way?
You haven't told your button to execute which action on click. for this you should use action for the submit button.
<h1>Enter business password to enter:</h1>
<input type="text" id="password" name="enter password"/>
<input type="submit" method="post" name="Submit"/ action = '/password'>
You should be using rails helpers and you need to specify the path where you want to post the data:
<h1>Enter business password to enter:</h1>
<%= form_tag("/password") do %>
<%= text_field_tag :password %>
<%= submit_tag 'Submit' %>
<% end %>

Rails .each function to show ID of whats selected

I have this segment of code,
<form>
ID of event: <input type="text" name="ID" placeholder="E.g. 123">
<input type="submit" value="Submit">
</form>
<% #events.each do |ev| %>
How do i make this work? I'm wanting to have it so the id that is typed in the text box shows the related stuff from the events table in the rest of the page.
I was under the impression that it would be something like this #events.id do |ev| ?but i'm still a noob at rails :)
Thanks for any help Please be gentle!
So i had done this all wrong!
i had to add this in my controller
def index
#events = Event.where(id: params[:id])
#id = params[:id]
end
and put this in to search for the ID
<%= form_tag({controller: "event", action: "index"}, method: "get") do %>
<%= label_tag(:id, "Input the ID to display:") %>
<%= text_field_tag(:id) %></p>
<%= submit_tag("Submit ID") %>
<% end %>
Sorry for the confusion but i thought it best to add the answer on how i fixed it.

Rails not showing error on form submit

I am working in rails 2, I have a form containing data of two models, In one model I am validating fields and entering error using self.errors.add_to_base , but this is not displaying error on form and submitting that request successfully.
This is my Model validation\
validate :checkPunchingEntries
def checkPunchingEntries
if self.punch_in_time.blank? && self.punch_out_time.blank?
self.errors.add_to_base("Both 'Punch in' and 'Punch out' time can not be blank")
end
end
This is my Form
<% form_tag '/punching_requests/create', :method => :post, :class=>"punching_request_form" do %>
<% #punching_request.errors.full_messages.each do |msg| %>
<p class="error"><%=msg%></p>
<% end %>
<p>
<label>Date </label>
<%= text_field_tag 'date'%> <%= calendar_for("date") %>
</p>
<p>
<label> </label>
<input type="checkbox" onclick="punch_in_toggle()">Punch In</input>
<input type="checkbox" onclick="punch_out_toggle()">Punch Out</input>
</p>
<div id="punch_in">
<p>
<label>Punch In Time </label>
<%= text_field_tag 'punch_in_time'%>
</p>
</div>
<div id="punch_out">
<p>
<label>Punch Out Time </label>
<%= text_field_tag 'punch_out_time'%>
</p>
</div>
<p>
<label>Assign To</label>
<%= select_tag(:approved_by, options_from_collection_for_select(#human_resource_persons, "id", "login")) %>
</p>
<p>
<label>Reason </label>
<%=text_area_tag 'reason'%>
</p>
<p class="actions"><label></label><%= submit_tag 'Save' %></p>
<% end %>
It comes into validation, but error is not shown on validation fails.
Have you tried the IRB console? If not, take a look what is going on there, just to be sure, that #punching_request.errors contains something.
try this
def checkPunchingEntries
if self.punch_in_time.blank? && self.punch_out_time.blank?
errors[:base]<< "Both 'Punch in' and 'Punch out' time can not be blank"
end
end
and use form_for and #punching_request
for example
# in your controller
def new
#punching_request = PunchingRequest.new
end
def create
...
...
end
your form look like in your new.html.erb
form_for(#punching_request)
...
...
end
In this you're generating the form using form_tag and the errors you're expecting would be stored in the form builder,form_for.
Instead of form_tag use form_for(#punching_request). This should do the trick.

Resources