Accessing Object within Form - Rails - ruby-on-rails

i try to access the first element of a array within my #user object and get the first object
out of that array. I just bring it not to work. Here is the code:
<%= form_for(#user) do |f| %>
<% if #user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<% if params[:redirect_to] -%>
<%= hidden_field_tag 'redirect_to', params[:redirect_to] %>
<% end -%>
#Thats not working
Last booking: <%= f.text_field Booking.where("user_id="+ #user.id+").order('start_date DESC').first%>
<% end %>

First of all You are trying to push a whole Booking object into the field, which seems to be wrong, probably You want some particular value like booking_date or something like this.
Booking.where(user_id: #user.id).order(:start_date).last.booking_date
Secondly You have a syntax error there and text_field method expects name of the user model field. If You defined has_many :bookings then You should use fields_for. Remember if You use form_for form builder You must reference model fields, if You need something custom use text_field_tag instead.
text_field_tag :booking, Booking.where(user_id: #user.id).order(:start_date).last.booking_date

Related

How to display errors on a form_for with multiple resources

I have an order form where I can add order items
<h2>Order: <%= #order.title %></h2>
<h3>Items</h3>
<%= render #order.items %>
<h4>Add an item:</h4>
<%= form_for([#order, #order.items.build]) do |f| %>
<%= f.label :Item %>
<%= f.submit 'Submit' %>
<% end %>
I want to check if there are any errors when creating the item.
This is what I use on the order form:
<% if #order.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(#order.errors.count, "error") %> prohibited
this order from being saved:
</h2>
<ul>
<% #order.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
I don't know how to display errors when there is multiple resources in the form for.
I've run into this before and ended up wanting to really customize the messages. In order to do it, I used a solution like this:
<% if #errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(#errors.count, "error") %> prohibited
this order from being saved:
</h2>
<ul>
<% #errors.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
This allows you to create an #errors object in your controller where you build array of messages. For example something like this:
#errors = []
#errors += #order.errors.map(&:full_messages)
#errors += #items.map(&:errors).map(&:full_messages).flatten
...
I hope this helps!

rails referencing arrays created by the group_by function

This code causes ActionController to return undefined method `name' for # How do I print the category name
<h2>Categories</h2>
<%= #categories.group_by(&:parent_id).each do |category| %>
<%= category.name %>
<% end %>
I mean, that the :parent_id is an attribute of Category model and #categories is a collection of ::ActiveRecord_Relation class. Let you try:
<h2>Categories</h2>
<%= #categories.group(:parent_id).each do |category| %>
<%= category.name %>
<% end %>
Consider using <%= debug #categories.group_by(&:parent_id) %> so that you can inspect the data structure
I believe you want to pass two arguments to the each block, one for the parent and one for the categories associated with it
<h2>Categories</h2>
<% #categories.group_by(:parent).each do |parent, categories| %>
<%= parent.name %>
<ul>
<% categories.sort_by(&:name).each do |category| %>
<li><%= category.name %></li>
<% end %>
</ul>
<% end %>
Above is based on my assumptions about your data model which could be totally incorrect?

Rails - Simple Form - style error messages

I'm trying to make a partial in my views folder, which his shared for error messages.
I want to remove the simple form standard error message and replace it with my own styling - across all models.
My question is, how do I reference the relevant model in my partial. Depending on where its used, it needs to reference the form in which the partial is included.
For example, the standard simple form error block is:
<% if #question.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#project_question.errors.count, "error") %> prohibited this question from being
saved:</h2>
<ul>
<% #project_question.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
How do I replace #question, with #[whatever the relevant model is called]?
Thank you
For this you can make a partial _error_messages,html.erb
<% if model.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(model.errors.count, "error") %> prohibited
this from being saved:
</h2>
<ul>
<% model.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
And you can render this partial in your view as:
<%= render partial: "error_messages", locals: {model: #question} %>
Your answer is a passing a local variable http://guides.rubyonrails.org/layouts_and_rendering.html#passing-local-variables
<%= render partial: "your_partial", locals: {question: #question} %>

Supply information automatically to simple form

I'm working on a simple little Rails social network and right now I am trying to automatically fill in the name of the currently signed in user when creating a new status update. My original code let them select their user name from a drop down which is silly - when you go on Facebook, you fill out the box and it automatically knows that it belongs to you. Here is my original code:
<%= simple_form_for(#status, html: {:class => "form-horizontal"}) do |f| %>
<% if #status.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#status.errors.count, "error") %> prohibited this status from being saved:</h2>
<ul>
<% #status.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.input :user_id, collection: User.all, label_method: :full_name %>
<%= f.input :content %>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
And here is what I am trying to replace it with:
<%= simple_form_for(#status, html: {:class => "form-horizontal"}) do |f| %>
<% if #status.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#status.errors.count, "error") %> prohibited this status from being saved:</h2>
<ul>
<% #status.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.input :user_id, current_user.full_name %>
<%= f.input :content %>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
It doesn't like this and says "can't convert Symbol into Integer"
How can I supply it with the logged in user's full name without ever giving them the option to chose another user?
user_id is the forreign key to User, it holds something like current_user.id not it's name.
It seems, that the user is already signed in, so you just need to show the name and provide the id to the create action:
<%= f.input :user_id, as: :hidden %>
<%= current_user.name %> <%# just for information %>
and set it in the new #status in the controller:
def new
#status = Status.new(user: current_user)
...
end
Your first input of the form is specified as an input for the :user_id which is an integer in your table of users. Putting a full name there is creating a mismatch in data types. Does this help?
Now that you want the username to be displayed without the user being able to change what is displayed, is there really a need to have this as an input? Can you just display the username in a normal <div></div>, so to speak?

Submit a form to a model from my homepage

I have a links model which has all the generic scaffold created for it, however, rather than go to the link#new page, I'd like to submit a form from my homepage that populates a new record.
I only have one text field, but im not sure how to construct the form. I read somewhere you have to specify the controller in the form field but this doesn't appear to be working.
<%= form_for(:link, #link) do |f| %>
<% if #link.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#link.errors.count, "error") %> prohibited this link from being saved:</h2>
<ul>
<% #link.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :url %><br />
<%= f.text_field :url %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
You don't need to specify anything if you are using default routes.
If the #link is an object that doesn't exist in database, Rails will automatically think this is a form for #new. So the form action will be /links, and method is post, which is the default resource to #create
In your case, you don't need to do anything, just revise the form code to:
<%= form_for(#link) do |f| %>
....
Besides, you need to prepare #link object in home controller, something like
#link = Link.new
All you have to do is add a url parameter to the form_for helper
<%= form_for :link, url: your_home_path do |f| %>

Resources