My rendered partial '_form' appears, but is not submitting - ruby-on-rails

Ruby 2.1.5 on Rails 4.2.0:
I have two directories. One directory is a rails-generate scaffold named 'inqueries'. The other directory is named 'welcome', which only houses a landing paged named index.html.erb. The inquery form works & submits fine as long as I'm using the view from the actual 'inquery' scaffold/directory.
I am able to render the inquery _form on my index.html.erb landing page using :
<%= render partial: "inqueries/form", locals: {inquery: #Inquery} %>
However, this JUST renders the form. When I hit the submit button, no errors, flash messages, or inquery is submitted. It is complete non-action, including on my rails terminal.
How can I properly make the form work on my landing page?
Here is my welcome_controller.rb file. This controller handles the landing page where I am trying to render the inquery scaffold's form:
class WelcomeController < ApplicationController
def index
#inquery = Inquery.new
render layout: false
end
end
This is my new rails scaffold-generated method in the inqueries_controller.rb:
def new
#inquery = Inquery.new
respond_with(#inquery)
end
Sorry, Here is the Inquery _form itself:
<%= form_for(#inquery) do |f| %>
<div class="form-group col-lg-4">
<%= f.label t('.name') %><br>
<%= f.text_field :name, class: "form-control" %>
</div>
<div class="form-group col-lg-4">
<%= f.label t('.email') %><br>
<%= f.text_field :email, class: "form-control" %>
</div>
<div class="form-group col-lg-4">
<%= f.label t('.phone') %><br>
<%= f.text_field :phone, class: "form-control" %>
</div>
<div class="clearfix"></div>
<div class="form-group col-lg-12">
<%= f.label t('.message') %><br>
<%= f.text_area :message, rows: 8, class: "form-control"%>
</div>
<%= f.submit t('.submit'), class: "btn btn-primary" %>
<% end %>
EDIT 2: I saw there were a couple of suggestions with the logic. I have simplified the partial just to see if I could get it to work and it still does not submit properly outside of its directory. I am new to rails, do I need to render a new action in the second directory that I am calling it into?

Your partial has 2 forms. The one which submits to inqueries#create is an empty form with no submit button, the other which has no action contains the text fields and the submit action.
The form_for tag will create the html tags, you dont need to specify them again.
Tip - Switch to haml. You won't ever look back at erb :)
This should work (not tested) -
<%= form_for(#inquery) do |f| %>
<div id="error_explanation">
<% if #inquery.errors.any? %>
<h2>
<%= pluralize(#inquery.errors.count, "error") %> prohibited this inquery from being saved:
</h2>
<ul>
<% #inquery.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<% end %>
</div>
<br />
<div class="col-lg-12">
<div class="form-group col-lg-4">
<%= f.label t('.name') %><br>
<%= f.text_field :name, class: "form-control" %>
</div>
<div class="form-group col-lg-4">
<%= f.label t('.email') %><br>
<%= f.text_field :email, class: "form-control" %>
</div>
<div class="form-group col-lg-4">
<%= f.label t('.phone') %><br>
<%= f.text_field :phone, class: "form-control" %>
</div>
<div class="clearfix"></div>
<div class="form-group col-lg-12">
<%= f.label t('.message') %><br>
<%= f.text_area :message, rows: 8, class: "form-control"%>
</div>
<div class="form-group col-lg-12">
<input type="hidden" name="save" value="contact">
<%= f.submit t('.submit'), class: "btn btn-primary" %>
</div>
</div>
<% end %>

Figured it out. Foolish error,
My welcome page is a bootstrap theme that I hadn't fully gone over. I had imported some unnecessary javascript files that I think were blocking the form.

Related

Rails: Form is not being loaded when trying to update

I am just trying to set up a basic application with Ruby on Rails. I have the basic CRUD functions for different entities and with one of them, the form that is supposed to load when the user wants to edit a row does not appear. It knows which ID it needs to edit, it just doesn't load the form.
Link to take you to edit page;
<%= link_to 'Edit Review'. edit_review_path(review) %>
Controller function;
def edit
#review=Review.find(params[:id])
end
HTML Form;
<div class="container">
<h2>Edit Review Details</h2>
<% form_for #review do |f| %>
<div class="form-group">
<%= f.label :"Profile" %><br />
<%= f.select :ProfileId, Profile.all.collect {|x| [x.Name,x.id]}, {:include_blank => 'Select Profile'}, class:'form-control'%> <br /><br />
</div>
<div class="form-group">
<%= f.label :"Product" %><br />
<%= f.select :ProductId, Product.all.collect {|x| [x.pName,x.id]}, {:include_blank => 'Select Product'}, class:'form-control'%>
</div>
<div class="form-group">
<%= f.label :"Review Author" %>
<%= f.text_field :Author %>
</div>
<div class="form-group">
<%= f.label :"Product Rating" %>
<%= f.number_field :ProductRating %>
</div>
<div class="form-group">
<%= f.label :"Review Text"%>
<%= f.text_field :ReviewText %>
</div>
<div class="form-group">
<%= f.label :"Date of Review" %>
<%= f.date_field :DateofReview %>
</div>
<div class="form-group">
<%= f.submit %>
</div>
<% end %>
</div>
I have 2 models that have a working update feature and they are the exact same code as this however for this, when I click the button. The page loads but nothing appears other than the text.
Can someone explain why this is happening? I thought it may be the dropdown boxes but I removed them and still no form loaded up.

keep content of my comment when i want edit him

I have comments for my gossips and i display them in the show of my gossips.
When I clic on edit for my gossip that open new windows and that display me the actual title and content.
But when I clic on edit for my comment that open the view for edit bur the content is blank. How display the actual content when i want edit him.
View - edit comment :
<%= form_for edit_gossip_comment_path do |f| %>
<div class="form-group new-gossip-field">
<%= f.text_area :content, placeholder: "Content", class: 'form-control' %>
</div>
<div class="form-group margin-left-50">
<button class="btn btn-primary" type="submit">Edit Comment</button>
</div>
<% end %>
View - edit gossip :
<%= form_for #gossip do |f| %>
<div class="form-group new-gossip-field">
<%= f.text_field :title, placeholder: "Title", class: 'form-control' %>
</div>
<div class="form-group new-gossip-field">
<%= f.text_area :content, placeholder: "Content", class: 'form-control' %>
</div>
<div class="form-group margin-left-50">
<button class="btn btn-primary" type="submit">Edit Gossip</button>
</div>
<% end %>
If i try to let #comment instead edit_gossip_comment_path that would not work, I dont really understand why.
If you need see more code tell me.
I think that the problem is in your form of edit comment, you are not passing the comment, so you should pass the object of work for the form, like:
View - edit comment :
<%= form_for #comment, url: edit_gossip_comment_path(#comment) do |f| %>
<div class="form-group new-gossip-field">
<%= f.text_area :content, placeholder: "Content", class: 'form-control' %>
</div>
<div class="form-group margin-left-50">
<button class="btn btn-primary" type="submit">Edit Comment</button>
</div>
<% end %>
with that, i belieave that the content of your comment will be show in the form.

Rspec/Capybara test post missing parameter from form but parameter is submmitted fine in dev when testing manually

One of my tests is creating a form post via javascript that is dropping the first input field from the form. The tests are obviously failing because of this. When deugging via binding.pry, it looks like a js post is dropping the first parameter [address_by_user] when posting it to my route. This parameter is posted just fine when testing via the ui in localhost but no longer works with rspec. Any idea what could cause this? Tests were passing as of two days ago and the forms haven't been changed. I tried restarting my computer in case it was a zombie process or something being cached, but the behavior continues. Any thoughts?
The form that is submitted via javascript using the following code:
var form_id = "#form-step" + currentIndex;
var my_form = $(form_id);
var url = $(form_id).attr('action');
var form_data = my_form.serialize();
console.log(form_data);
var submission = $.post(url, form_data);
The form looks like this and is submitted after clicking the "Next" button in a wizard, that works fine.
<%= form_with(url:"add_property", scope: :property, class: "form", id:"form-step0") do |f| %>
<div class="form-body" id="step0form">
<h4 class="form-section"><i class="ft-location"></i> <%= t(:property_details) %></h4>
<%= render 'shared/error_messages', object: f.object %>
<div class="row">
<div class="form-group pac-card col-md-8 offset-md-1 required" id='pac-card'>
<div class="field" id='pac-container'>
<%= f.label :address_by_user, t(:address), class:"label-control" %>
<%= f.text_field :address_by_user, :required => true, class: "form-control pac-input google-autocomplete", id:"pac-input" %>
</div>
</div>
<div class="form-group form-row col-md-2">
<div class="field">
<%= f.label :unit_number %>
<%= f.text_field :unit_number, class: "form-control" %>
</div>
</div>
</div>
<div class="form-group form-row">
<div class="field col-md-2 offset-md-1">
<%= f.label :bedrooms %>
<%= f.number_field :bedrooms, :required => true, class: "form-control", in: 1...20 %>
</div>
<div class="field col-md-2 offset-md-1">
<%= f.label :bathrooms %>
<%= f.number_field :bathrooms, class: "form-control", in:1...10 %>
</div>
<div class="field col-md-2 offset-md-1">
<%= f.label :accommodates %>
<%= f.number_field :accommodates, class: "form-control", min: "0" %>
</div>
</div>
<div class="form-group form-row">
<div class="field col-md-2 offset-md-1"><%=label_tag(t(:property_type))%></div>
</div>
<div class="form-group form-row" data-toggle="buttons">
<div class="btn-group col-md-12">
<div class="field col-md-2 offset-md-1">
<%= f.label :room_type, class: 'text-center btn btn-default active' do %>
<%= image_tag('house-icon.png') %>
<br>
<%= f.radio_button :room_type, "entire_place", checked: true %>
<span>Entire Place</span>
<% end %>
</div>
<div class="field col-md-2 offset-md-1">
<%= f.label :room_type, class: 'text-center btn btn-default' do %>
<%= image_tag('room.png') %>
</br>
<%= f.radio_button :room_type, "private_room" %>
<span>Private Room</span>
<% end %>
</div>
<div class="field col-md-2 offset-md-1">
<%= f.label :room_type, class: 'text-center btn btn-default' do %>
<%= image_tag('people.png') %>
</br>
<%= f.radio_button :room_type, "shared_room" %>
<span>Shared Room</span>
<% end %>
</div>
</div>
</div>
<div id="hidden-fields">
<%= f.hidden_field(:latitude, id: 'latitude') %>
<%= f.hidden_field(:longitude, id: 'longitude') %>
<%= f.hidden_field(:street_number, id: 'street_number') %>
<%= f.hidden_field(:street_name, id: 'route') %>
<%= f.hidden_field(:city, id: 'locality')%>
<%= f.hidden_field(:state, id: 'administrative_area_level_1') %>
<%= f.hidden_field(:country, id: 'country')%>
</div>
</div>
<% end %>
When tested directly from the site via localhost, all parameters post fine and I get this: (as expected)
<ActionController::Parameters {"address_by_user"=>"Pura Uvita Vacation Home, Puntarenas Province, Uvita, Costa Rica", "unit_number"=>"", "bedrooms"=>"2", "bathrooms"=>"2.0", "accommodates"=>"6", "room_type"=>"entire_place", "latitude"=>"9.1839769", "longitude"=>"-83.72444630000001", "street_number"=>"", "street_name"=>"", "city"=>"Uvita", "state"=>"Provincia de Puntarenas", "country"=>"Costa Rica"} permitted: false>
When the parameters are obtained via binding.pry from the rspec flow, the following is the result:
<ActionController::Parameters {"unit_number"=>"", "bedrooms"=>"2", "bathrooms"=>"2", "accommodates"=>"6", "room_type"=>"entire_place", "latitude"=>"", "longitude"=>"", "street_number"=>"", "street_name"=>"", "city"=>"", "state"=>"", "country"=>""} permitted: false>
The two most likely causes of this are
The fields are no longer actually being filled in - look at the browser while the tests are running and see if the form is actually being correctly filled in.
You have an error in another JS file causing the JS you think is processing the form not to run. This can happen because JS assets are concatenated in the test environment, allowing an error in on JS file to prevent any files concatenated after it not to be processed.

How to dynamically Update/Edit/Delete existing record within a list

new to rails and could use some help figuring out how to allow users to update records in a list without having to leave the page.
Specifically, I have two forms on a page where users enter their children's info.
One form is for the user to add a NEW child's info to create a list of children below.
The list of children displays the user's previously entered children info.
However, within the child list I would like to allow users to both delete and edit an individual child's record.
My DELETE function is working fine, it's the UPDATE functionality I am having trouble with...
Here's the children#update controller:
def update
raise
#user = current_user
#child = Child.find(params[:id])
if #child.update_attributes(child_params)
flash[:notice] = "Child info was updated."
else
flash[:error] = "Sorry. Something went wrong, please try again."
end
respond_with(#child) do |f|
f.html { redirect_to new_child_path }
end
end
Here's the childlist form partial view:
<form role="form">
<% i = 1 %>
<% #user.children.each do |child| %>
<div class="col-md-12 form-align list-line">
<div class="col-md-10 form-align">
<%= label_tag child, "Child #{i}:" %>
<% i += 1 %>
</div>
</div>
<%= form_for(child, method: :put) do |f| %>
<div class="col-md-12 form-align">
<div class='col-md-4 form-align'>
<%= f.label :first_name, class: 'sr-only' %>
<%= f.text_field :first_name, value: child.first_name, class: 'form-control form-control-align' %>
</div>
<div class='col-md-4 form-align'>
<%= f.label :middle_name, class: "sr-only" %>
<%= f.text_field :middle_name, value: child.middle_name, class: 'form-control form-control-align' %>
</div>
<div class='col-md-4 form-align'>
<%= f.label :last_name, class: "sr-only" %>
<%= f.text_field :last_name, value: child.last_name, class: 'form-control form-control-align' %>
</div>
</div>
<div class="col-md-12 form-align">
<div class="col-md-4 form-group form-inline form-align">
<%= f.label :birth_date, "D.O.B." %>
<%= f.date_field :birth_date, value: child.birth_date, class: 'form-control' %>
</div>
<div class="col-md-4 form-group form-inline form-align">
<%= f.label :deceased, "Deceased?" %>
<%= f.select :deceased, value: child.deceased?, class: 'form-control form-control-align' %>
</div>
<%= f.submit "Update" %>
<%= link_to '<i class="glyphicon glyphicon-remove red"></i>'.html_safe, child, method: :delete %>
</div>
<% end %>
<% end %>
</form>
...and the child model: simply belongs_to :user / user model has_many :children
...and routes: resources :children
I think I need some options passed through my form_for, but unable to find what those need to be...
I would suggest using a gem like Best in Place to allow for in-place editing on certain fields in the list.
If you don't want in-place editing then utilize a modal view that contains the edit form.

Rails - Use the same form with new/edit using fields_for

I have 5 models,
Person
person_car
car (has many tires)
person_car_tire
tire (belongs to car)
so I have this view
_form.html.erb
<%= form_for(#person) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div>
<% Car.all.each do |c|%>
<div class="field">
<%= f.fields_for(:person_cars) do |ff|%>
Car Name: <%= ff.check_box :car_id %>|<%= c.car_name%>
<% c.tires.each do |b|%>
<div class="field">
<%= ff.fields_for(:person_car_tires) do |fff|%>
Tire: <%#= fff.check_box :tire_id%>|<%= b.tire_name%>
<%end%>
</div>
<%end%>
<%end%>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
And when I save it works perfectly, the problem comes when I want to edit using this form because it duplicates data of each car 4 times in the view. I've been searching and fields for allows extra options so I made:
<%= form_for(#person) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div>
<% #person.cars.each do |c|%>
<div class="field">
<%= f.fields_for(:person_cars, c) do |ff|%>
Actividad: <%= ff.check_box :car_id %> | <%= c.car.name%>
<% c.person_car_tires.each do |t|%>
<div class="field">
<%= ff.fields_for(:person_car_tires, t) do |fff|%>
Tarea: <%#= fff.check_box :tire_id%>|<%#= t.tire.name%>
<%end%>
</div>
<%end%>
<%end%>
</div>
<%end%>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
and it now works, but only show the cars and tires that I've selected on the new action. not all as I wanted (because if I use the first form it duplicates checkboxes in the view).
How can I use the same form for both actions?
Hope someone can help me.
You can use the same _form partial for both new and edit. You just need to pass local variables set to different values to this form. Basically, whatever differs, abstract it away as a parameter (local variable) to this function-like partial.

Resources