【问题标题】:rails application displaying blank space in users(venue) placerails 应用程序在用户(地点)位置显示空白区域
【发布时间】:2015-04-22 22:45:31
【问题描述】:

我可以发布活动,但是当我选择要发布的用户时,下拉菜单会显示用户(场地名称)应该在的空白区域。我已经尝试了我能想到的一切。任何想法将不胜感激。

我在 Rails 4 上。我也在使用设计。

这是我的文件:

show.html.erb:

<p id="notice"><%= notice %></p>

<p>
  <b>Name:</b>
  <%= @gig.user.venue_name %>
</p>

<p>
  <strong>Event:</strong>
  <%= @gig.event %>
</p>

<p>
  <strong>Date:</strong>
  <%= @gig.date %>
</p>

<p>
  <strong>Doors:</strong>
  <%= @gig.doors %>
</p>

<p>
  <strong>Showtime:</strong>
  <%= @gig.showtime %>
</p>

<p>
  <strong>Age:</strong>
  <%= @gig.age %>
</p>

<p>
  <strong>Price:</strong>
  <%= @gig.price %>
</p>

<p>
  <strong>Description:</strong>
  <%= @gig.description %>
</p>

<%= link_to 'Edit', edit_gig_path(@gig) %> |
<%= link_to 'Back', gigs_path %>

index.html.erb:

<p id="notice"><%= notice %></p>
<div class="page-header">
  <h1>All Gigs</h1>
</div>

<% @gigs.each do |gig| %>
<div class="gig">
    <strong><%= gig.user.venue_name %></strong>
    <p><%= gig.event %></p>
    <p><%= gig.date %></p>
    <p><%= gig.doors %></p>
    <p><%= gig.showtime %></p>
    <p><%= gig.age %></p>
    <p><%= gig.price %></p>
    <p><%= gig.description %></p>
    <div class="meta">
      <%= link_to time_ago_in_words(gig.created_at) + " ago", gig %>
      <span class="admin">
        | <%= link_to 'Edit', edit_gig_path(gig) %> |
        <%= link_to 'Delete', gig, method: :delete, data: { confirm: 'Are you sure?' } %>
      </span>
    </div>
</div>
<% end %>
  </tbody>
</table>

gig.rb:

class Gig < ActiveRecord::Base
  belongs_to :user
end

user.rb:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

has_many :gigs

end

_form.html.erb:

<%= simple_form_for(@gig, html: {class: "form-horizontal"}) do |f| %>
  <% if @gig.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@gig.errors.count, "error") %> prohibited this gig from being saved:</h2>

      <ul>
      <% @gig.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <%= f.input :user_id, collection: User.all, label_method: :venue_name %>
  <%= f.input :event %>
  <%= f.input :date %>
  <%= f.input :doors %>
  <%= f.input :showtime %>
  <%= f.input :age %>
  <%= f.input :price %>
  <%= f.input :description %>
  <div class="form-actions">
    <%= f.button :submit %>
  </div>

db migrate 添加用户 id 到 gig

class AddUserIdToGigs < ActiveRecord::Migration
  def change
    add_column :gigs, :user_id, :integer
    add_index :gigs, :user_id
    remove_column :gigs, :name
  end
end

gigs_controller.rb

class GigsController < ApplicationController
  before_action :set_gig, only: [:show, :edit, :update, :destroy]

  # GET /gigs
  # GET /gigs.json
  def index
    @gigs = Gig.all
  end

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

  # GET /gigs/new
  def new
    @gig = Gig.new
  end

  # GET /gigs/1/edit
  def edit
  end

  # POST /gigs
  # POST /gigs.json
  def create
    @gig = Gig.new(gig_params)

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

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

  # DELETE /gigs/1
  # DELETE /gigs/1.json
  def destroy
    @gig.destroy
    respond_to do |format|
      format.html { redirect_to gigs_url, notice: 'Gig was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def gig_params
      params.require(:gig).permit(:name, :event, :date, :doors, :showtime, :age, :price, :description, :user_id)
    end
end

【问题讨论】:

  • 您的索引和显示控制器是什么样的?我也有点困惑你对帖子和用户的意思......你在使用设计吗?需要更多信息来考虑解决方案。
  • 我正在使用设计,当用户/场地注册时,我设置了它,他们可以发布演出。除 user_id 外,所有内容均显示。
  • 我对这个问题还不是很清楚……但是,show 方法中没有任何内容……例如@gig = Gig.find(:id =&gt; params[:id]) 以及您要查找的用户或场所。 .. 无论哪种方式,您都需要在控制器方法中使用类似于@venues = Venue.all 的内容,以便在 *.html.erb 中调用它
  • 嗯,不明白。我可以分享我的 git repo 给你一个更好的主意吗?
  • 好的,我将所有内容都更改为 但现在它只显示 ID 号而不是用户名。越来越近

标签: ruby ruby-on-rails-4


【解决方案1】:

解决了:

运行 irb 后,我注意到数据库中的场所为空。因此,我意识到我必须通过创造新的形式作为设计场所。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    • 2020-03-29
    • 2017-12-16
    • 2021-02-15
    • 2015-01-06
    • 2021-12-02
    • 2022-06-29
    相关资源
    最近更新 更多