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.
Related
I want to be able to performa range search with ransack. I was hoping I could do this with a slider that has two thumbs. I'm thinking that the _in predicate would be perfect for a situation like this. However, I cannot find an example on how to appropriately configure this. Here would be some example code:
CONTROLLER `users_controller.rb:
def index
#page_title = 'Users'
#page_icon = 'store'
#q = User.search(params[:search]).ransack(params[:q])
#retailers = #q.result.order("#{sort_column} #{sort_direction}").paginate(page: params[:page], per_page: 20)
end
VIEW users/index.html.erb:
<%= search_form_for #q, url: users_path, class: 'form' do |f| %>
<div class="row">
<div class="col-sm-3">
<div class="form-group">
<%= label_tag :search %>
<%= text_field_tag :search, params[:search], class: "form-control" %>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<%= f.label :first_name_eq, "First Name" %>
<%= f.select :first_name_eq, {include_blank: true}, {class: "chosen-select"} %>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<%= f.label :team_eq, "Team" %>
<%= f.select :team_eq, Team.options_for_select, {include_blank: true}, {class: "chosen-select"} %>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-3">
<div class="form-group">
<%= f.label :score_in, "Score Range" %>
<%= f.search_field :score_in, {include_blank: true} %>
</div>
</div>
</div>
<div>
<%= f.submit 'Apply Filters', class: 'btn btn-outline-dark btn-md' %>
<%= link_to 'Reset Filters', users_path(q: {reset: true}), class: 'btn btn-outline-danger btn-md' %>
</div>
<% end %>
I guess where I'm lost is how to start or implement a slider w/ two thumbs that would appropriately pass the parameters when submitted. Or even if I just create a two thumbed slider from a js library, how to connect it then to ransack, or at least pass the values of the two thumbs to ransacks params. Another thing that might be helpful is if someone has an example, both in controller/view, of what the _in predicate should look like.
I'm getting an error on RoR that I can't seem to fix yet, it is the following:
I have my form setup like this
<%= form_for #sheet, url: '/sheets#new' do |f| %>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
Inicio:<br>
<%= f.datetime_field_tag :Inicio %>
Fim: <br>
<%= f.datetime_field_tag :Fim %><br>
Tarefa:<br>
<%= f.number_field_tag :Tarefa %><br>
PNA:<br>
<%= f.number_field_tag :PNA %><br>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
FPNA:<br>
<%= f.number_field_tag :FPNA %><br>
Deslocação:<br>
<%= f.number_field_tag :Deslocacao %><br>
KM's:<br>
<%= f.number_field_tag :km %><br>
Estadia:<br>
<%= f.number_field_tag :estadia %><br>
</div>
</div>
</div>
<div class="text">
Descrição:<br>
<div class="textfield">
<%= f.text_field_tag :descricao %><br>
</div>
</div>
<%= submit_tag "Submit" %>
<% end %>
and when I try to submit anything I get the following error:
undefined method `datetime_field_tag' for #<ActionView::Helpers::FormBuilder:0xd13f898>
If for example I take out the f. on the fiels it works but it posts every field as "null" on the database, help/ideas on how to fix this?
Use datetime_field with form_for instead of datetime_field_tag
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.
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.
I'd like to have a single form that has 2 or more field-set_tags and I'd like to have a single submit button to update the changes for all field_set_tags at once, but that's not working ; if I create a form for each field_set_tag and one submit button per form, it works fine ; anyone can help? my simplified partial view _form.html.erb has the following structure
<%= form_for(#my_form) do |f| %>
<%= field_set_tag "Field Set A" do %>
<div class="field>
<%= f.label :"Configure A" %>
<%= f.check_box :ConfigA %>
<%= f.label :"Login" %>
<%= f.text_field :A_Login, {:size => 12} %>
<%= f.label :"Password" %>
<%= f.password_field :A_Password, {:size => 12} %>
<!-- more Ruby code here -->
</div>
<% end %>
<%= field_set_tag "Field Set B" do %>
<div>
<%= f.label :"Configure B" %>
<%= f.check_box :ConfigB %>
<%= f.label :"Login" %>
<%= f.text_field :B_Login, {:size => 12} %>
<%= f.label :"Password" %>
<%= f.password_field :B_Password, {:size => 12} %>
<!-- more Ruby code here -->
</div>
<% end %>
<div class="actions">
<%= f.submit "Update" %>
</div>
<!-- more field_sets and Ruby code here -->
<% end %>
I also tried this with no luck either
<input type="submit" value="Update" />
My issue was caused by a button I configured as a place holder for future use like this
<div class="field">
<%= button_to "Advanced" %>
</div>
When I removed that button, all worked fine
In a similar way, I configured 2 successive submit buttons like this
<div class="actions">
<%= button_to "Update", :type => "submit" %>
</div>
<div class="actions">
<%= button_to "Update", :type => "submit" %>
</div>
the 1st one does submit but the 2nd one generates this error
Unknown action
The action '4' could not be found for L2CircuitTestsController
2 or more successive "Advanced" buttons cause the same behavior : only the 1st one does the job and the other are eclipsed!