【问题标题】:How to link the backend admin code to front end in rails如何将后端管理代码链接到 Rails 中的前端
【发布时间】:2018-04-20 05:48:12
【问题描述】:

我已经为网站创建了一个具有后端功能的管理面板,现在,我必须将其链接到前端设计。我怎么能那样做?我知道我必须创建一个前端控制器,但之后如何将它与网站设计联系起来?谁能帮帮我这是我想做的第一个项目?

我需要用前端填充课程代码。如何调用查询?

后端管理面板的课程代码:

class CoursesController < AdminController
 before_action :set_course, only: [:show, :edit, :update, :destroy]

 def index
  @courses = Course.all
 end

 def show
 end

def new
 @course = Course.new
end

def edit
end

def create
 @course = Course.new(course_params)
 respond_to do |format|
  if @course.save
   format.html { redirect_to @course, notice: 'Course was successfully 
   created.' }
   format.json { render :show, status: :created, location: @course }
  else
   format.html { render :new }
   format.json { render json: @course.errors, status: :unprocessable_entity 
  }
  end
 end
end

def update
 respond_to do |format|
  if @course.update(course_params)
    format.html { redirect_to @course, notice: 'Course was successfully 
 updated.' }
    format.json { render :show, status: :ok, location: @course }
  else
    format.html { render :edit }
    format.json { render json: @course.errors, status: :unprocessable_entity 
  }
  end
 end
end

def destroy
 @course.destroy
 respond_to do |format|
  format.html { redirect_to courses_url, notice: 'Course was successfully 
  destroyed.' }
  format.json { head :no_content }
 end
end

private
 def set_course
   @course = Course.find(params[:id])
 end

 def course_params
   params.require(:course).permit(
    :title,
    :alias,
    :start_date,
    :end_date,
    :max_participants,
    :min_participants,
    :course_fee,
    :tax,
    :deposit,
    :description,
    :category_id,
    :location_id,
    :short_description,
    :currency,
    :if_fully_booked,
    course_ids: []
    )
  end
 end

我也有写在 course.html.erb 文件中的管理面板前端代码。但我对前端网站设计有一个单独的看法。如何将该代码链接到后端?

【问题讨论】:

    标签: ruby-on-rails ruby frontend backend


    【解决方案1】:

    正如您在代码中看到的,您以两种格式响应:

    • HTML (format.html)
    • JSON (format.json)

    所以,如果你在浏览器中写这样的内容:localhost:3000/courses.json(扩展名 .json 很重要),你应该会收到一个 json 响应。

    如果你不知道如何使用 javascript 来构建 json,你需要学习一下。

    更多信息在

    【讨论】:

    • 感谢您的回复.. 看看信息
    猜你喜欢
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    • 2021-09-14
    • 2020-11-04
    相关资源
    最近更新 更多