why the parameter come out in my UI page? - ruby-on-rails

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| %>

Related

Why is post title not showing up?

So here it is:
For some reason this is not showing the title for my post.
However when I press show option and it goes to post's page you can see al the detail.
_form.html.erb
<%= form_with(model: post, local: true) do |form| %>
<% if post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :date %>
<%= form.datetime_select :date, id: :post_date %>
</div>
<div class="field">
<%= form.label :name %>
<%= form.text_area :name, id: :post_name %>
</div>
<div class="field">
<%= form.label :user_id %>
<%= form.number_field :user_id, id: :post_user_id, value: current_user.id %>
</div>
<div class="field">
<%= form.label :description %>
<%= form.text_area :description, id: :post_description %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
#show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Date:</strong>
<%= #post.date %>
</p>
<p>
<strong>Name:</strong>
<%= #post.name %>
</p>
<p>
<strong>User:</strong>
<%= #post.user_id %>
</p>
<p>
<strong>Description:</strong>
<%= #post.description %>
</p>
<% if current_user == #post.user %>
<%= link_to 'Edit', edit_post_path(#post) %> |
<%end%>
<%= link_to 'Back', posts_path %>
<h1>Editing Post</h1>
<%= render 'form', post: #post %>
<%= link_to 'Show', #post %> |
<%= link_to 'Back', posts_path %>
edit.html.erb
<%= render 'form', post: #post %>
<%= link_to 'Show', #post %> |
<%= link_to 'Back', posts_path %>
I've tried changing it <%= form.text_area :name, id: :post_name %>
to string_area :name and also string_field but none of it is working.
In fact when I change it to the last 2, I get a method error if I go on the edit page. I'm still fairly new to rails and to ruby so I would greatly appreciate some help. I did try to google however and was unsuccessful, I guess I didn't know what to search for or maybe I worded it wrong.

Form Refactoring Rails

I am currently creating a form in my rails application but would like some advice on refactoring it. The form is being used to save bagel toppings for an order. The user can select three different toppings (topping1, topping2, and topping3), and the form fields for each one are virtually identical.
ie.
<%= form_for order_item, remote: true do |f| %>
...
<div class="field">
<%= f.label :topping1 do %>
<%= image_tag("http://www.189harwood.com/statics/images/products/ingredients/butter.png", class:"topping") %>
Butter
<% end %>
<%= f.radio_button :topping1, "butter", checked: "checked" %>
</div>
<div class="field">
<%= f.label :topping1 do %>
<%= image_tag("http://www.wpclipart.com/food/dairy/cheese/soft_cream_cheese.png", class:"topping") %>
Cream Cheese
<% end %>
<%= f.radio_button :topping1, "cream cheese" %>
</div>
...
<div class="field">
<%= f.label :topping2 do %>
<%= image_tag("http://www.189harwood.com/statics/images/products/ingredients/butter.png", class:"topping") %>
Butter
<% end %>
<%= f.radio_button :topping2, "butter", checked: "checked" %>
</div>
<div class="field">
<%= f.label :topping2 do %>
<%= image_tag("http://www.wpclipart.com/food/dairy/cheese/soft_cream_cheese.png", class:"topping") %>
Cream Cheese
<% end %>
<%= f.radio_button :topping2, "cream cheese" %>
</div>
...
<%= f.submit "Wrap It Up" %>
<% end %>
Any suggestions on how I can refactor this? Thanks
Try storing the toppings in an array of hashes in your model:
def self.toppings
[{name:'butter', url:'http://www.189harwood.com/statics/images/products/ingredients/butter.png'}, {name:'cream cheese', url:'http://www.wpclipart.com/food/dairy/cheese/soft_cream_cheese.png'}, {...}]
end
In your controller:
my_action
#topping_options = Model.toppings
end
And in your view:
<div class="field">
<% #topping_options.each do |topping| %>
<%= f.label topping[:name].to_sym, topping[:name].humanize %>
<%= image_tag(topping[:url], class:"topping") %>
<%= f.radio_button topping[:name].to_sym %>
<% end %>
</div>

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"

Easiest way of displaying certain fields and submit buttons on a sidebar (a la WordPress)?

So, I want to archive something like this:
I want to have tag, image fields, submit, and save as draft submit buttons in a sidebar.
Right now, the structure is like this:
new.html.erb:
<% provide(:title, 'Create post') %>
<h1 class="navless-title">Create post</h1>
<%= form_for(#post, remote: true, :html => { :multipart => true }) do |f| %>
<%= render 'fields', f: f %>
<div class="form-actions">
<%= f.submit "Publish", class: "publish btn btn-primary pull-left" %>
<%= f.submit "Save Draft", class: "save-draft btn btn-default pull-left" %>
<div class="busy pull-left"></div>
</div>
<% end %>
fields.html.erb:
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :content %>
<%= f.text_area :content, id: "wysihtml5-textarea" %>
<%= f.label :tag_list, "Tags" %>
<%= f.text_field :tag_list, "data-pre" => #post.tags.map(&:attributes).to_json %>
<%= f.label :image %>
<%= f.file_field :image %>
<p class="status">
<% if #post.id? && #post.status == "Draft" %>
Status: <span class="label"><%= #post.status %></span>
<% elsif #post.id? && #post.status == "Published" %>
Status: <span class="label label-primary"><%= #post.status %></span>
<% end %>
</p>
<%= render 'shared/editor_toolbar' %>
So, I'm not sure what to do; either using absolute positioning, jQuery, or cutting the form into half (not sure if this is possible).
What's the easiest way of doing this?

'shared/error_messages' partial not rendering when it should

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

Resources