'shared/error_messages' partial not rendering when it should - ruby-on-rails

I can't figure out why my 'shared/error_messages' partial isn't being rendered when it should be (i.e. when an invalid 'treating' is submitted through this form):
_treating_form.html.erb:
<%= form_for(#treating) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div><%= f.hidden_field :requestee_id %></div>
<div>
<%= f.text_field :proposed_location, placeholder: "Propose a location here..." %>
</div>
<div>
<%= f.text_field :proposed_date, placeholder: "Propose a date here..." %>
</div>
<div class="field">
<%= f.text_area :intro, placeholder: "Write your introduction here..." %>
</div>
<%= f.submit "Send", class: "btn btn-large btn-primary" %>
<% end %>
This _treating_form.html.erb partial is called in a users/show.html.erb view:
<% provide(:title, #user.name) %>
<div class="row">
<aside class="span4">
<section>
<h1>
<%= #user.name %>
</h1>
</section>
<% if signed_in? %>
<section>
<%= render 'shared/treating_form' unless current_user?(#user) %>
</section>
<% end %>
</aside>
</div>
Here is my error_messages partial:
<% if object.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(object.errors.count, "error") %>.
</div>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
The error_messages partial is correctly being rendered upon validation errors triggered in submitting to user 'edit settings' page:
<% provide(:title, "Edit user") %>
<h1>Update your profile</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirm Password" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
Any ideas why the errors aren't being shown when an invalid 'treating' is being submitted? Thanks!

Whenever you add ANY options to a partial you need to explicitly specify :partial =>
= render partial: 'shared/error_messages', object: f.object

Related

why the parameter come out in my UI page?

i have no idea why the parameter suddenly come out at my role create page. After i add the permission checkbox there then it appear.
new.html.erb
<% provide(:title, "Create Roles") %>
<h1 class="dashboard">Create Role</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_with(model: #role, local: true) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= #permissions.each do |permission|%>
<%= check_box_tag 'permission_ids[]', permission.id %>
<%= f.label :permission_name, permission.name %>
<% end %>
<%= f.hidden_field :company_id , value: 2%>
<%= f.submit "Create Role", class: "btn btn-primary bottom" %>
<% end %>
</div>
</div>
Parameter show at role create page
You are currently seeing the string output of the each method because you're using <%= #permissions.each ...
Instead use the "silent" <% as you do for end:
<% #permissions.each do |permission| %>

Accessing virtual attribute from params

ruby 2.1.5
rails 4.2.1
I am trying to figure out how to access a virtual attribute from params.
In my books_controller.rb, I have:
wrap_parameters :books_list
def product_params
params.require(:product).permit(:author_name, :book_name, :books_list)
end
Obviously, the books_list is the virtual attribute. I will be saving it to the books_list table
In my new.html.erb,I have:
<% #title = "Books" %>
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<%= form_for(#books, html: { class: 'form-horizontal' }) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.form_group :author_name do |f| %>
<%= f.label :author_name, class: 'control-label col-md-2' %>
<div class='col-md-8'>
<%= f.text_field :author_name, class: 'form-control' %>
<%= f.error_messages %>
</div>
<% end %>
<div class="form-inputs">
<%= f.form_group :book_name do |f| %>
<%= f.label :book_name, class: 'control-label col-md-2' %>
<div class='col-md-8'>
<%= f.text_field :book_name, class: 'form-control' %>
<%= f.error_messages %>
</div>
<% end %>
<%= f.form_group :books_list do |f| %>
<%= f.label :books_list, class: 'control-label col-md-2' %>
<div class='col-md-8'>
<%= f.text_area :books_list, class: 'form-control' %>
<%= f.error_messages %>
</div>
<% end %>
</div>
<div class="form-actions col-md-offset-2 col-md-10">
<%= f.submit class: 'btn btn-primary' %>
<%= link_to "Cancel", test_sets_path, class: 'btn' %>
</div>
<% end %>
</div>
</div>
</div>
When I submit the form, here's what I get for params:
Parameters: {
"utf8"=>"✓",
"authenticity_token"=>"5mxkibQHkwRnzVU31A6pe9uezsmaWeYbCUgU+gUZPLmiAZPnN6si+smdEePU0lfGITh7gmmyChf/bOY+YQQcQg==",
"books"=>{"author_name"=>"Some Author", "book_name"=>"Dwan Of The Dead"},
"books_list"=>{"{:class=>\"form-control\"}"=>"book1\r\nbook2"},
"commit"=>"Create Book List"
}
I am having trouble accessing the books_list in params. Any ideas?
If books_list column is not exist in #book, it raises error.
There is no method f.form_group in rails, I think you use gem that defines form_group method.
Maybe "books_list"=>{"{:class=>\"form-control\"}"=>"book1\r\nbook2"} parameter is made by some bootstrap gem. you check check name attribute in html source.

Rails - Show updated fields when editing

I am rolling my own authentication, and the issue I am running into is the edit form for my users. I here is the form...
#app/views/users/edit.html.erb
<h1>Editing User</h1>
<%= render 'form' %>
<%= link_to 'Show', #user %> |
<%= link_to 'Back', root_path %>
#app/views/users/_form.html.erb
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% #user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
</div>
<div class="field">
<%= f.select :user_type, options_for_select(['Bar', 'Brewery', 'Restaurant', 'Hotel']) %><br/>
</div>
<div class="actions"><%= f.submit "Sign Up" %></div>
<% end %>
The issue I am having is that when I display the edit form the :user_type field is not correct. I want this field to reflect what is currently saved for the current user, instead I just get the drop down with the first option in the list displayed.
You want to either use:
<%= f.select :user_type, options_for_select(['Bar', 'Brewery', 'Restaurant', 'Hotel'], #user.user_type) %>
or:
<%= f.select :user_type, options_for_select(['Bar', 'Brewery', 'Restaurant', 'Hotel']), :selected => #user.user_type %>
options_for_select has second parameter to preselect it...
options_for_select(['Bar', 'Brewery', 'Restaurant', 'Hotel'], #user.user_type)

Rails: How to make a form post to another controller action

I know you are usually supposed to use the linking between new/create and edit/update in rails, but I have a case where I need something else. Is there anyway I can achieve this same connection?
I have a form for a model and I want it to post the data (similar to how a new view, post to the create action).
Here is my form
<div id="new-job">
<%= form_for(#job) do |f| %>
<% if #job.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#job.errors.count, "error") %> prohibited this job from being saved:</h2>
<ul>
<% #job.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :location %><br />
<%= f.text_field :location %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="actions">
<%= f.submit "create job", id: "submit-job", class: "button small radius" %>
<%= link_to "go back", jobs_path, class: "button small radius secondary" %>
<div id="new-job-errors"> </div>
</div>
<% end %>
</div>
Use the :url option.
= form_for #job, :url => company_path, :html => { :method => :post/:put }

How to ensure all nested attributes show during 'edit'

I currently have a nested model User has_many Sales_Orders has_many Items.
I can create the Sales_Order with nested Items properly but when I try to 'edit' the Sales_Order, the Sales_Order information is visible but the Items are not shown. Any idea why these wouldn't show?
[sales_orders_controller.rb]
...
def edit
#sales_order = SalesOrder.find(params[:id])
end
[edit.html.erb]
<% provide(:title, "Edit SO") %>
<h1>Edit Sales Order</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(#sales_order) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="span3">
<%= f.label :so, "SO#:" %>
<%= f.text_field :so %>
<%= f.label :customer, "Customer:" %>
<%= f.text_field :customer %>
<%= f.label :enter_date, "Date Entered:" %>
<%= f.text_field :enter_date, value: date_formatter(#sales_order.enter_date) %>
<%= f.label :request_date, "Request Date:"%>
<%= f.text_field :request_date, value: date_formatter(#sales_order.request_date) %>
<%= f.label :comments, "CS Comments:" %>
<%= f.text_area :comments %>
</div>
<div class="span3">
<% f.fields_for #sales_order.items do |builder| %>
<%= render 'item_fields', f: builder %>
<% end %>
</div>
<%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
[_item_fields.erb]
<fieldset>
<%= f.label :item_code, "Item Code:" %>
<%= f.text_field :item_code %>
<%= f.label :qty_in_kg, "Qty (kg):" %>
<%= f.text_field :qty_in_kg %>
<%= f.label :qc_comments, "Comments:" %>
<%= f.text_field :qc_comments %>
<%= link_to "remove", '#', class: "remove_fields" %>
</fieldset>
<% f.fields_for #sales_order.items do |builder| %>
should be
<%= f.fields_for #sales_order.items do |builder| %>
Without the = you're building the output in the loop but never rendering/printing the return value (the generated HTML output).
Use <%= f.fields_for #sales_order.items do |builder| %>
Notice the = sign. This implies, "evaluate and embed"

Resources