除了默认的Rails页面外,无法渲染任何东西

时间:2019-06-10 20:40:51

标签: ruby-on-rails ruby

我正在遵循教程(https://blog.teamtreehouse.com/static-pages-ruby-rails)以使用Rails路由来呈现页面。问题是我得到一个错误而不是应该呈现的页面。我不知道我在做什么错,因为实际上只有几个步骤。

我的代码与教程唯一的区别是微不足道的。我使用的是Videos而不是Pages.

config/routes.rb中:

Rails.application.routes.draw do
    # This means that all paths following the pattern:
    # /videos/about, /videos/home, /videos/features
    # will route here!
    get "/videos/:video" => "videos#show"
end

app/controllers/videos_controller.rb"中:

class VideosController < ApplicationController
    def show
        render template: "videos/#{params[:page]}"
    end
end

最后,我有一个静态页面,里面充满了乱七八糟的东西,存储在app/views/videos/videos.html.erb中。

但是,当我运行服务器并转到0.0.0.0:3000/videos/时,出现以下错误:

No route matches [GET] "/videos"
Rails.root: /home/mightu/Desktop/portal_rails

当我尝试0.0.0.0:3000/videos/pizza时,我得到:

Missing template /videos with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in: * "/home/mightu/Desktop/portal_rails/app/views" 

它告诉我问题是此行:

render template: "videos/#{params[:page]}"

请帮助

2 个答案:

答案 0 :(得分:2)

您需要定义到/videos的路由,如果需要所有静态路由,则可以添加resources :videos或仅 get 'videos', to: 'videos#index'

您仅为show动作定义了路线,但是您尝试获取视频的index页,可以查看路由here的详细信息

答案 1 :(得分:1)

先做:

DetailItem

哪个会给你:

public class DetailItem : INotifyPropertyChanged
{
    private void UpdateExplanation()
    {
      this.Explanation = GetExplanation(this.Reference, this.Detail);
    }

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
      this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private string mark;
    public string Mark
    {
      get => this.mark;
      set
      {
        if (value == this.mark) return;
        this.mark = value;
        OnPropertyChanged();
      }
    }

    private string reference;
    public string Reference
    {
      get => this.reference;
      set
      {
        if (value == this.reference) return;
        this.reference = value;
        OnPropertyChanged();
        UpdateExplanation();
      }
    }

    private string detail;
    public string Detail
    {
      get => this.detail;
      set
      {
        if (value == this.detail) return;
        this.detail = value;
        OnPropertyChanged();
        UpdateExplanation();
      }
    }

    private string explanation;
    public string Explanation
    {
      get => this.explanation;
      set
      {
        if (value == this.explanation) return;
        this.explanation = value;
        OnPropertyChanged();
      }
    }
  }

然后,将Rails.application.routes.draw do resources :videos end 重命名为 videos GET /videos(.:format) videos#index POST /videos(.:format) videos#create new_video GET /videos/new(.:format) videos#new edit_video GET /videos/:id/edit(.:format) videos#edit video GET /videos/:id(.:format) videos#show PATCH /videos/:id(.:format) videos#update PUT /videos/:id(.:format) videos#update DELETE /videos/:id(.:format) videos#destroy

然后将对app/views/videos/videos.html.erb的{​​{1}}操作修改为:

app/views/videos/show.html.erb

现在show将呈现VideosController模板,您将得到一个class VideosController < ApplicationController def show end end ,其值为0.0.0.0:3000/videos/pizza