How to get the name of the current user in Refinery? - refinerycms

In RefineryCMS, for some functionality I have to store the present logged in user's first name and last name.
How to get logged in user's first name and last name?

While going through this implementation, I found that refinery user table does not have column to store first name, last name or any basic details about user.
So, first step is to create a migration file having first_name, last_name etc as per requirement.
$ rails generate migration AddFirstNameLastNameToRefineryUsers
first_name:string last_name:string
Then run
$ rake db:migrate
Now, create a decorators for user model.
Path :: /app/decorators/models/refinery/user_decorator.rb
Create a decorator : Refinery Documentation Link for Creating Decorator
Add this line inside that model
attr_accessible :first_name, :last_name
validates :first_name,:last_name, :presence => true
Now add this line at view area
/app/views/users/new.html.erb
<div class='field'>
<%= f.label :first_name %>
<%= f.text_field :first_name, :class => 'larger widest' %>
</div>
<div class='field'>
<%= f.label :last_name %>
<%= f.text_field :last_name, :class => 'larger widest' %>
</div>
Now, Rails will save the values, when user makes first time login.

Related

Displaying Rails Required Fields Form Errors All At Once

I'm using the Rails form and I'm also using Bootstrap tabs. I have 4 tabs that a user fills out and on the 4th tab is the submission button. I have a number of required: true fields in the form, and when I go to click on the submit button leaving any of these fields blank, the pop-up saying "Please fill out this field" appears.
Question: How can I just have a box appear with ALL the error messages, instead of each individual error message appearing one at a time?
I've read a number of posts, and have tried most suggestions (with exception to ones that include JS, as I'm hoping there is a strong solution not including JS). I put below some code below my submit button that I tried but it doesn't display anything as the individual box error message I think overrides it.
Any help is appreciated.
_Form:
<%= form_for(#property, html: { multipart: true }) do |p| %>
...
<%= p.file_field :picture, :multiple => true, name: "property_attachments[picture][]", size: 2 %>
<%= p.submit "Submit", class: 'btn btn-primary' %>
<% if #property.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= pluralize(#property.errors.count, "error") %>.
</div>
<ul>
<% #property.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<% end %>
Figured it out and learning a lot about Rails Forms. I was using required: true for a number of fields and this created individual pop-ups on each error one by one. I removed all of the required: true fields and then added validations in the model (eg. validates: :price, presence: true) and then the error message I used above in my question displayed any missing fields.

joining two tables with multiple conditions

I have following code in my template
<% #dishes.each do |d| %>
<div class="card">
<img src="<%= d.image_url %>" alt="">
<p><%= d.name %></p>
<p><%= d.dish_type.name if d.dish_type %></p>
<h4># <%= d.venue %></h4>
<form action="/likes/<%= d.id %>" method="post">
<button <%= 'disabled' if d.likes.where(user_id: current_user.id) %>>Like</button>
<p>Count: <%= d.likes.count %></p>
</form>
What I am trying to achieve is when a user clicks on 'like' button I should disable the button.
I have three tables dishes, users, likes. likes table contains dish_id and user_id as foreign keys. You can say many to many relationship where likes is a junction table.
I am activerecord I have setup all the connections and belongs_to and has_many relations, they all are good.
The problem I am facing is at this particular line
<button <%= 'disabled' if d.likes.where(user_id: current_user.id) %>>Like</button>
somehow the join condition is not working, it always evaluates to true. I tried different variations like using 'joins' and all but nothing is working.
Any help is appreciated. Please let me know if further information is required?
I think it always evaluates to true because the where clause is returning an active record relationship object.
If you want to get true or false from that particular query you can use "exist?":
d.likes.exists?(user_id: current_user.id)
If the record exists in the database you will get True otherwise False.

Error when calling posts.content

I keep getting this error
undefined method`content'for"Post::ActiveRecord_Associations_CollectionProxy:0x00000104e87060"
When I do <%= #user.posts.content %>
but when I do <%= #user.posts.count %> everything shows up normal and it works... I don't understand why I can call count and it show's the number of posts in the users show.html.erb page but when I try and show the content it throws and error.
Any ideas?
Instead of <%= #user.posts.content %>
Use this
<% #user.posts.each do |post| %>
<%= post.content %>
<% end %>
#user.posts return a collection of posts. So, when you call count you get the count of all records returned by #user.posts. BUT in order to access content attribute of each post, you will need to iterate over the collection and display the relevant data.

Rails paperclip, Edit form file_field not assigned

I used paperclip to attach an avatar onto my user, in my Model:
has_attached_file :avatar,
:styles => {square_tiny: '50x50#', square_small: '100x100#', square: '200x200#'}
I have a form
<%= form_for(#user_profile,
:url => { :controller => :user_profiles, :action => :update_general_info, :id => #user_profile.id },
:html => { :multipart => true,
:class=> "form-horizontal" }) do |f| %>
<div class="control-group">
<%= f.label :avatar, :class => "control-label" %>
<div class="controls">
<%= f.file_field :avatar %>
</div>
</div>
....
<% end %>
The upload works perfect, but I come back and EDIT my user, the file field says 'no file chosen'. And since I am validating presence of that avatar, every time a user edit his details, he has to upload his avatar again...
How do I work around that?
I thought the :multipart => true would help but it didn't.
There is absolutely no good way for a page to set a value to a file field, and that's for security reasons.
If the browser allowed a page or a JS script to set a value to a file field that would allow a malicious page to preset the file field value with some system or passwords file. And that would be a massive security hole.
What I do in that case is I display the already saved file as a link that the user can click to download. You can then provide little AJAX links to delete (the file is deleted with an AJAX call and the link replaced with a new file input) and replace (the link is replaced with a file input).
Your last option would be to use AJAX to upload the file. If you use AJAX for a file upload you'll POST to a hidden frame so the file input will keep its selected value.
Either way keep in mind that any change to file field value has to be user initiated.

How do I change the text_area default size for a form in rails 3?

I'm going through Agile Web Development with Rails and I'm having some trouble with the form helper text_area. Specifically, I want to make the text area smaller (the form submits correctly and everything goes into the database correctly). According to the book this code should work:
<%= form_for(#request) do |f| %>
<div class="actions">
...
<div class="field">
<%= f.label :quote_details, "*Items required:" %>
<%= f.text_area :quote_details, :rows=>5, :cols=>40 %>
</div>
It seems that no matter what numbers I put for :rows or :cols, the box stays the same default size. Instead of :rows and :cols, I used :size=>"3x40" and size=>"5x8" etc.. but the box still always stays the same size.
As an experiment I tried
<%= f.text_field :quote_details, :size=>"300*39" %>
That changed the number of columns, but removing the :size and putting :rows or :cols has no effect (it goes back to a default size for a text_field).
I did see this:
Change default Rails text_area helper rows/cols
I tried answer 1, but the answer given didn't work for me. I don't really understand what the second and third answers mean. I might be doing something else wrong or maybe it's a different problem.
I'm just stumped. Any help or ideas on what's going on would be greatly appreciated. Thanks for any responses.
Oh, I'm using rails version 3.0.0 and ruby 1.9.2p0 on vista.
your first code segment has :cols => 40% instead of 40?
I would also consider using CSS to do it, as that can make changing the look of the webpage isolated to the CSS presentation layer.
try do it with form_with. For me it works.
<%=form_with, local: true do |form| %>
<%= form.label :comment %>
<%= form.text_area :body, :rows=>10, :cols=>60 %>
<% end %>