如何将后端管理代码链接到rails

时间:2018-04-20 05:48:13

标签: ruby-on-rails ruby frontend backend

我创建了一个管理面板,其中包含网站的后端功能,现在,我必须将其链接到前端设计。我怎么能这样做?我知道我必须创建一个前端控制器,但之后如何将其与网站设计联系起来?任何人都可以帮助我这是我想要做的第一个项目吗?

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

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

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文件编写的。但是我对前端网站设计有一个单独的看法。如何将该代码链接到后端?

1 个答案:

答案 0 :(得分:0)

正如您在代码中看到的那样,您将以两种格式进行响应:

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

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

如果您不知道如何使用javascript对json进行测试,则需要对其进行研究。

中的更多信息