Ruby视图中的逻辑

时间:2015-10-17 17:28:53

标签: ruby-on-rails join logic

我正在制作一个用户可以加入和取消加入活动的项目。我得到了连接功能,除了在视图页面中,当我为它编写逻辑时:“if @joins.count> 0 ...然后显示”Joined / Unjoin“,否则,显示”Join“..当我点击Join时,它会影响到每一行,而不仅仅是我想要加入的特定事件。而且它不会像那样进入数据库。因为当我点击一个事件时它只会显示计数增加了我加入的1,即使在主索引页面上看起来好像这个特定用户已加入/取消加入所有事件......这是我的代码。我该怎么做才能纠正这个

events_controller.rb

class EventsController < ApplicationController
before_action :require_login, except: [:new, :create, :show, :edit, :update, :delete]
before_action :require_correct_user, only: [:edit, :update, :delete]

def index
    @events = Event.all
    @user = current_user
    @joins = Join.where(user:@user)

end

def create
    user = User.find(current_user.id)
    user.events.create(name: params[:name], timing: params[:timing], location: params[:location], state: params[:state])        
    redirect_to "/events"
end

def show
    @event = Event.find(params[:id])
    @comments = @event.comments
    @joins = Join.where(event:@event, user: current_user)
end

def delete
    event = Event.find(params[:id])
    event.destroy
    redirect_to "/events"
end

def edit
    @event = Event.find(params[:id])
end

def update
    event = Event.find(params[:id])
    event.update(name: params[:name], location: params[:location], timing: params[:timing], state: params[:state])
    redirect_to "/events"
end

def join
    event = Event.find(params[:id])
    user = User.find(current_user)
    Join.create(user:user, event:event)
    redirect_to "/events"
end

def unjoin
    event = Event.find(params[:id])
    user = current_user
    Join.find_by(user: user, event: event).destroy
    redirect_to "/events"
end
end

index.html.erb

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title></title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>

<body>

<div class="container">

  <% if current_user %>
    <form action='/sessions' method='post'>
      <input type='hidden' name='authenticity_token' value='<%=      form_authenticity_token %>'>
      <input type='hidden' name='_method' value='delete'>
      <input type='submit' value='Log Out'>
    </form>
  <% end %>
  <a href="/users/<%=current_user.id%>">Edit Profile</a>
  <hr>

  <h3>Welcome, <%=current_user.first_name%> <%= current_user.last_name %></h3>
  <hr>
  <h4>Here are some of the events in your state:</h4>
  <table class="table class table-bordered table-striped table-hovered table- hover">
    <thead>
    <tr>
      <th>Name</th>
      <th>Date</th>
      <th>Location</th>
      <th>State</th>
      <th>Host</th>
      <th>Action/Status</th>
    </tr>
    </thead>
    <tbody>
    <% @events.each do |e| %>

      <% if e.state==c urrent_user.state %>
        <td>
          <a href="/events/<%=e.id %>">
            <%=e.name %>
        </td>
        <td>
          <%=e.timing%>
        </td>
        <td>
          <%=e.location%>
        </td>
        <td>
          <%=e .state %>
        </td>
        <td>
          <%=e.user.first_name%>
        </td>
        <% if e.state==c urrent_user.state and e.user.id==c urrent_user.id %>
          <td><a href="/events/<%=e.id%>/edit">Edit</a>
            <form action="/events/<%=e.id%>" method="post">
              <input type="hidden" name="_method" value="delete">
              <input type="hidden" name="authenticity_token" value="  <%=form_authenticity_token%>">
              <input type="submit" value="Delete">
            </form>
          </td>
        <% elsif e.state==c urrent_user.state and @joins.count> 0 %>
          <td>Joined |
            <form action="/joins/<%=e.id%>" method="post">
              <input type="hidden" name="_method" value="delete">
              <input type="hidden" name="authenticity_token" value="   <%=form_authenticity_token%>">
              <input type="submit" value="Cancel">
            </form>
          </td>
        <% elsif @joins.count==0 and e.state==c urrent_user.state %>
          <td>
            <form action="/joins/<%=e.id%>" method="post">
              <input type="hidden" name="authenticity_token" value="<%=form_authenticity_token%>">
              <input type="hidden" name="id" value="<%=e.id%>">
              <input type="submit" value="Join">
            </form>
          </td>
        <% end %>
        </tr>
      <% end %>
    <% end %>
    </tbody>
  </table>
  <hr>
  <h4>Here are some of the events in other states:</h4>
  <table class="table class table-bordered table-striped table-hovered table-hover">
    <thead>

    <tr>
      <th>Name</th>
      <th>Date</th>
      <th>Location</th>
      <th>State</th>
      <th>Host</th>
      <th>Action/Status</th>
    </tr>
    </thead>
    <tbody>
    <% @events.each do |e| %>
      <tr>
        <% if e.state !=c urrent_user.state %>
          <td>
            <a href="/events/<%=e.id %>">
              <%=e.name %>
          </td>
          <td>
            <%=e.timing%>
          </td>
          <td>
            <%=e.location%>
          </td>
          <td>
            <%=e .state %>
          </td>
          <td>
            <%=e.user.first_name%>
          </td>
          <% if e.user.id==c urrent_user.id %>
            <td><a href="/events/<%=e.id%>/edit">Edit</a> |
              <form action="/events/<%=e.id%>" method="post">
                <input type="hidden" name="_method" value="delete">
                <input type="hidden" name="authenticity_token" value="  <%=form_authenticity_token%>">
                <input type="submit" value="Delete">
              </form>
            </td>
          <% elsif @joins.count> 0 %>
            <td>Joined |
              <form action="/joins/<%=e.id%>" method="post">
                <input type="hidden" name="_method" value="delete">
                <input type="hidden" name="authenticity_token" value="<%=form_authenticity_token%>">
                <input type="submit" value="Cancel">
              </form>
            </td>
          <% elsif @joins.count==0 %>
            <td>
              <form action="/joins/<%=e.id%>" method="post">
                <input type="hidden" name="authenticity_token" value="<%=form_authenticity_token%>">
                <input type="submit" value="Join">
              </form>
            </td>
          <% end %>
        <% end %>
      </tr>
    <% end %>
    </tbody>
  </table>
  <hr>
  <h4>Add Event:</h4>
  <form action="/events" method="post">
    <input type="hidden" name="id" value="<%=current_user.id%>">
    <input type="hidden" name="authenticity_token" value=" <%=form_authenticity_token%>"> Name:
    <input type="text" name="name">
    <br> Date:
    <input type="date" name="timing">
    <br> Location:
    <input type="text" name="location">
    <select name="state" id="">
      <option value="<%=current_user.state%>">
        <%=current_user.state%>
      </option>
      <% if current_user.state=="CA" %>
        <option value="WA">WA</option>
        <option value="AZ">AZ</option>
      <% elsif current_user.state=="WA" %>
        <option value="CA">CA</option>
        <option value="AZ">AZ</option>
      <% elsif current_user.state=="AZ" %>
        <option value="WA">WA</option>
        <option value="CA">CA</option>
      <% end %>
    </select>
    <input type="submit" value="Add Event">
  </form>
</div>
</body>

</html>

0 个答案:

没有答案