【问题标题】:Can't determine why iteration not showing objects - Ruby / ActiveRecord / Sinatra无法确定为什么迭代不显示对象 - Ruby / ActiveRecord / Sinatra
【发布时间】:2021-06-07 17:28:36
【问题描述】:

我通过 ActiveRecord 创建了两个表:UsersPaintings。我能够创建两个对象的实例。绘画与用户具有belongs_to 关系。用户与绘画有 has_many 关系。当我尝试遍历用户的绘画列表时,没有任何反应。当我使用类似的约定来迭代所有绘画时,我不会遇到任何问题。


require './config/environment'

class ApplicationController < Sinatra::Base

   configure do
       set :public_folder, 'public'
       set :views, 'app/views'
       enable :sessions
       set :session_secret, "extra_secret"
     end

    get '/' do
      erb :index
    end


  helpers do

    def logged_in?
      !!current_user
    end

    def current_user
      @current_user ||= User.find_by(id: session[:user_id]) if session[:user_id]
    end
  end 


end 

class UsersController < ApplicationController

  get '/users/:slug' do
    @user = User.find_by_slug(params[:slug])
      erb :'users/user_homepage'
  end

get '/signup' do
  if !logged_in?
    erb :'users/new_user'
  else
    redirect to '/'
  end
end 

post '/signup' do
  if params[:username] == "" || params[:email] == "" || params[:password] == ""
    redirect to '/new_user'
  else
    @user = User.new(:username => params[:username], :email => params[:email], :password => params[:password])
    @user.save
    session[:user_id] = @user.id
    redirect to '/user_homepage'
  end
end


  get '/login' do
    if !logged_in?
      erb :'users/login'
    else
      redirect '/index'
    end
  end

post '/login' do
    user = User.find_by(:username => params[:username])
    if user && user.authenticate(params[:password])
        session[:user_id] = user.id
        redirect to '/index'
    else 
        redirect to 'users/new_user'
    end 
end 

get '/logout' do
  if logged_in?
    session.destroy
    redirect to '/login'
  else
    redirect to '/'
  end
end

end 

class PaintingsController < ApplicationController

  get '/index' do
    if logged_in?
      @paintings = Painting.all
      erb :'paintings/index'
    else
      redirect to '/login'
    end 
  end

  get "/new" do  #create get
    erb :'paintings/new'
  end 

    post "/new" do  #create post
      Painting.create(params)
      redirect "/user_homepage"
     end



   post '/user_homepage' do #changed from /index to /user_homepage. Did not fix.
     if logged_in?
       if params[:name] == ""
         redirect to "/new"
       else
         @painting = current_user.paintings.build(name: params[:name])
         if @painting.save
           redirect to "/index/#{@painting.id}"
         else
           redirect to "/new"
         end
       end
     else
       redirect to '/login'
     end
   end  



    get "/paintings/:id/edit" do #update get
      if logged_in?
          @painting = Painting.find_by_id(params[:id])
        if @painting && @painting.user == current_user
          erb :'paintings/edit_tweet'
        else
        redirect to '/user_homepage'
        end
      else
        redirect to '/login'
      end
    end

    post "/paintings/:id" do #update post
        @painting = Painting.find(params[:id])
        @painting.update(params.select{|p| p=="name" || p=="year" || p=="artist_id"})
        redirect "/paintings/#{@painting.id}"
      end


      get "/paintings/:id" do #read get
        @painting = Painting.find(params[:id])
        erb :'paintings/show'
      end

      get '/user_homepage' do
        if logged_in?
          erb :'users/user_homepage'
        else
          redirect '/index'
        end
      end 
      
    #Need post /user_homepage to show users paintings?

end 

#不显示用户绘画的视图

<h1> User Homepage </h1>

<h2>Welcome <%= current_user.username %>!</h2>
<p> Below is a list of your favorite paintings: </p>


<% @current_user.paintings.each do |painting| %>

  <div>
    Name: <%= painting.painting_name %>
    Year: <%= painting.year %>
  </div>
 
<% end %>

<p> <a href="/new"> Add New Painting</a>

<p> <a href="/index"> Click here to see today's popular paintings! </a> </p>

ActiveRecord::Schema.define(version: 2021_03_03_151621) do

  create_table "paintings", force: :cascade do |t|
    t.string "painting_name"
    t.string "year"
    t.integer "artist_id"
    t.integer "user_id"
  end

  create_table "users", force: :cascade do |t|
    t.string "username"
    t.string "email"
    t.string "password_digest"
  end

end

class Painting < ActiveRecord::Base

  belongs_to :user
  
end

class User < ActiveRecord::Base
 
 has_secure_password

 has_many :paintings

 def slug
    username.downcase.gsub(" ","-")
  end

  def self.find_by_slug(slug)
    User.all.find{|user| user.slug == slug}
  end

end 

【问题讨论】:

  • 那里有错字吗?是复制粘贴吗? “@current_user”似乎从未被分配到任何地方。无论如何,如果它只是问题中的一个错字,你应该调试它,没有简单的方法来回答它。只需在视图中输入“”或其他内容,看看current_user.paintings.to_a 返回什么。
  • 抱歉,@current_user 是在应用程序控制器中定义的。我会把它添加到我的帖子中。
  • 与 kaplan 的问题一样,您在视图中同时使用 current_user 和 @current_user。这是代码的样子,还是问题中的错字?另外,是用户名无法渲染,还是只有画没有出现?这可能是一个线索。您是否遇到任何错误,或者它是否静默失败?

标签: ruby model-view-controller activerecord sinatra


【解决方案1】:

看来您可能在打算使用@user 的地方使用current_user

这是您的“用户主页”操作:

@user = User.find_by_slug(params[:slug])
erb :'users/user_homepage'

但在你看来

@current_user.paintings

要检查的另一个选项是(我不是 Sinatra 专家)助手会暴露于视图,但不会暴露于底层实例变量,因此您需要使用

current_user.paintings

【讨论】:

    猜你喜欢
    • 2015-12-16
    • 2023-01-20
    • 1970-01-01
    • 2020-10-22
    • 1970-01-01
    • 2021-12-15
    • 2015-12-03
    • 2021-03-01
    • 2021-05-16
    相关资源
    最近更新 更多