dsfdsf2fafad
dsfdsf2fafad

Reputation: 321

How to show only articles written by users themselves through Devise in Rails

I'm using full_calendar to show events based on user id.

I have an index of home that renders _form.html.erb of events, and creates events here.

And when I moved to the events/index with the calendar, I tried to show only events based on the user_id.

However, there are currently user id (1 ~ 3), but only events for user id (1) are shown. The events created by the rest of the user id are stored in the DB but are not visible in the calendar.

I would like to display only events according to user id in the calendar.

Home controller

def index
  @event = Event.new
  @events = Event.order("RANDOM()").first(1)
end

index.html.erb (Home controller)

<%= render partial: 'events/form', locals: { event: @event } %>

_form.html.erb (Event controller)

<%= simple_form_for(@event) do |f| %>
  <%= f.error_notification %>
  <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>

  <div class="form-inputs">
    <%= f.input :title %>
    <%= f.input :description %>
    <%= f.input :color %>
    <%= f.input :start_time %>
    <%= f.input :end_time %>
    <%= f.hidden_field :user_id, :value => @event.user_id %>      
    <%= f.hidden_field :user_email, :value => @event.user_email %>      


  </div>
  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

index.html.erb (Event controller)

<div class="row">
    <div class="col-lg-12">
        <section class="panel">
            <div class="panel-body">
                <div id="calendar"></div>
            </div>
        </section>
    </div>
</div>

<% @events.each do |x| %>
    <% if(x.user_id == current_user.id) %>
        <script type='text/javascript' src='jquery-1.8.1.min.js'></script>
        <script>
        $('#calendar').fullCalendar({
           header: {
                         left: 'prev,next today',
                         center: 'title',
                         right: 'month,agendaWeek,agendaDay'
                     },
                    events: '/events.json'
                });
        </script>
    <% else %>
        <script type='text/javascript' src='jquery-1.8.1.min.js'></script>
        <script>
        $('#calendar').fullCalendar({
           header: {
                         left: 'prev,next today',
                         center: 'title',
                         right: 'month,agendaWeek,agendaDay'
                     },
                });
        </script>
    <% end %>
<% end %>

Upvotes: 1

Views: 256

Answers (2)

MrYoshiji
MrYoshiji

Reputation: 54882

You probably have the following line in your Event model:

class Event < ApplicationRecord
  belongs_to :user

And the reflection in the User model:

class User < ApplicationRecord
  has_many :events

With these relations, you can simply do the following in your controller:

@events = current_user.events

Because current_user returns a User instance, and when you defined the has_many :events in the User model, Rails created this events instance method on User.

Upvotes: 2

bo-oz
bo-oz

Reputation: 2872

I don't fully understand your question, but if you want to only show posts by a certain user, the easiest way is to use a scope in the model, and call that from a controller:

model scope

  class Calendar
    scope :by_user, -> (user) { where(user:user) }
  end

And use it in your controller:

 def index
   @calendars = Calendar.by_user(current_user)
 end

This will only fetch result for which user = current_user

Upvotes: 0

Related Questions