1

我正在尝试在 c9 ubuntu 设置中使用 rails 构建博客。当我优化路线时,我意识到我在浏览器中的 url 不会随着我的操作而改变,尽管一切正常。

在此处输入图像描述

可以在浏览器中看到的 url 实际上设置为显示所有博客的帖子,确实如此,但是当我尝试仅单击一个(在本例中为“我的博客帖子 1”)时,它会执行该操作,但是网址保持不变。

但是,如果我手动输入正确的 url,它也可以工作。

在此处输入图像描述

一切正常,但我的网址不会自动更改,但我必须手动更改。在这个阶段我可以处理,但我担心我以后会遇到问题。这是我第一次使用 cloud9 环境。控制器:

class BlogsController < ApplicationController
  before_action :set_blog, only: [:show, :edit, :update, :destroy]

  # GET /blogs
  # GET /blogs.json
  def index
    @blogs = Blog.all
  end

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

  # GET /blogs/new
  def new
    @blog = Blog.new
  end

  # GET /blogs/1/edit
  def edit
  end

  # POST /blogs
  # POST /blogs.json
  def create
    @blog = Blog.new(blog_params)

    respond_to do |format|
      if @blog.save
        format.html { redirect_to @blog, notice: 'Blog was successfully created.' }
      else
        format.html { render :new }
      end
    end
  end

  # PATCH/PUT /blogs/1
  # PATCH/PUT /blogs/1.json
  def update
    respond_to do |format|
      if @blog.update(blog_params)
        format.html { redirect_to @blog, notice: 'Blog was successfully updated.' }
      else
        format.html { render :edit }
      end
    end
  end

  # DELETE /blogs/1
  # DELETE /blogs/1.json
  def destroy
    @blog.destroy
    respond_to do |format|
      format.html { redirect_to blogs_url, notice: 'Blog was successfully destroyed.' }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_blog
      @blog = Blog.friendly.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def blog_params
      params.require(:blog).permit(:title, :body)
    end
end

路线:

Rails.application.routes.draw do
  resources :portofolioos, except: [:show]
  get 'portofolioo/:id', to: 'portofolioos#show', as: 'portofolioo_show'

  get 'about', to: 'pages#about'
  get 'contact', to: 'pages#contact'

  resources :blogs

  root to: 'pages#home'
end

View 只是一个表单。它几乎是脚手架,我只将显示页面更改为在 url 中显示 id 并添加了friendly_id gem,但它都没有显示在 url 中,但它工作得很好。

  <p><%= link_to portofolio_item.title, portofolioo_show_path(portofolio_item) %></p>
4

0 回答 0