每个都在Ruby on Rails中使用if条件

时间:2018-02-19 18:13:13

标签: ruby-on-rails list

我想创建一个名为preference的对象列表,但只包含当前用户的首选项。我用以下代码尝试了它:

<% if logged_in? %>
  <div class="center jumbotron">
    <p id="notice"><%= notice %></p>

    <h1>Präferenzen anzeigen</h1>

    <table>
      <thead>
        <tr>
          <th>Institute</th>
          <th>Preference value</th>
          <th colspan="3"></th>
        </tr>
      </thead>

      <tbody>
        <% @preferences if (user_id  == current_user.id).each do |preference| %>
          <tr>
            <td><%= preference.institute_id %></td>
            <td><%= preference.preference_value %></td>
            <td><%= link_to 'Bearbeiten', edit_preference_path(preference) %></td>
            <td><%= link_to 'Löschen', preference, method: :delete, data: { confirm: 'Are you sure?' } %></td>
          </tr>
        <% end %>
      </tbody>
    </table>

    <br>

    <%= link_to 'Neue Präferenz anlegen', new_preference_path %>
  </div>
<% end %>

不幸的是我收到错误:

  

未定义的局部变量或方法`user_id'用于#&lt;#:0x007f8ea83ed030&gt;   你的意思是? user_url

第17行

<% @preferences if (user_id  == current_user.id).each do |preference| %>

如何修复我的代码?有一个简单的方法,还是我需要与助手一起尝试?

非常感谢您的帮助!

这是我的会话助手:

module SessionsHelper

  # Logs in the current user
  def log_in(user)
    session[:user_id] = user.id
  end

  # Remembers a user in a persistent session.
  def remember(user)
    user.remember
    cookies.permanent.signed[:user_id] = user.id
    cookies.permanent[:remember_token] = user.remember_token
  end

  def current_user?(user)
    user == current_user
  end

  # Returns the user corresponding to the remember token cookie.
  def current_user
    if (user_id = session[:user_id])
      @current_user ||= User.find_by(id: user_id)
    elsif (user_id = cookies.signed[:user_id])
      user = User.find_by(id: user_id)
      if user && user.authenticated?(:remember, cookies[:remember_token])
        log_in user
        @current_user = user
      end
    end
  end

  # Returns true if the user is logged in, false otherwise.
  def logged_in?
    !current_user.nil?
  end

  # Forgets a persistent session.
  def forget(user)
    user.forget
    cookies.delete(:user_id)
    cookies.delete(:remember_token)
  end

  # Logs out the current user.
  def log_out
    forget(current_user)
    session.delete(:user_id)
    @current_user = nil
  end

  # Redirects to stored location (or to the default).
  def redirect_back_or(default)
    redirect_to(session[:forwarding_url] || default)
    session.delete(:forwarding_url)
  end

  # Stores the URL trying to be accessed.
  def store_location
    session[:forwarding_url] = request.original_url if request.get?
  end

end

最后是偏好控制器

class PreferencesController < ApplicationController
  before_action :set_preference, only: [:show, :edit, :update, :destroy]

  # GET /preferences
  # GET /preferences.json
  def index
    @preferences = Preference.all
  end

  # GET /preferences/1
  # GET /preferences/1.json
  def show
  end

  # GET /preferences/new
  def new
    @preference = Preference.new
  end

  # GET /preferences/1/edit
  def edit
  end

  # POST /preferences
  # POST /preferences.json
  def create
    @preference = Preference.new(preference_params)

    respond_to do |format|
      if @preference.save
        format.html { redirect_to @preference, notice: 'Preference was successfully created.' }
        format.json { render :show, status: :created, location: @preference }
      else
        format.html { render :new }
        format.json { render json: @preference.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /preferences/1
  # PATCH/PUT /preferences/1.json
  def update
    respond_to do |format|
      if @preference.update(preference_params)
        format.html { redirect_to @preference, notice: 'Preference was successfully updated.' }
        format.json { render :show, status: :ok, location: @preference }
      else
        format.html { render :edit }
        format.json { render json: @preference.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /preferences/1
  # DELETE /preferences/1.json
  def destroy
    @preference.destroy
    respond_to do |format|
      format.html { redirect_to preferences_url, notice: 'Preference was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_preference
      @preference = Preference.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def preference_params
      params.require(:preference).permit(:user_id, :institute_id, :preference_value, :wei_preference_value,
      :accepted)
    end
end

1 个答案:

答案 0 :(得分:1)

User模型中,您应该创建与Preference模型的关系

class User < ApplicationRecord
  . . .
  has_many :preferences
  . . . 
end

只要Preferenceuser_id列标识其所属的User,那么这应该只添加一行。

然后,在您的视图中,您可以像这样使用它:

<% @current_user.preferences.each do |preference| %>
  <tr>
    <td><%= preference.institute_id %></td>
      <td><%= preference.preference_value %></td>
      <td><%= link_to 'Bearbeiten', edit_preference_path(preference) %></td>
      <td><%= link_to 'Löschen', preference, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
<% end %>

您应该在Active Record Associations上阅读更多内容,它们会对您有所帮助,并且是Rails的重要组成部分。