Rails URL路径和资源路由 [英] Rails url paths and resource routing

查看:94
本文介绍了Rails URL路径和资源路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具体问题.所以我会了解是否没有人帮助我. :)无论如何,在此先感谢!如何使用资源和静态协议来重构此代码?特别是我的意思是render_with_hashtags_of_question(text)render_with_hashtags_of_answer(text)方法及相关路线 get '/questions/hashtag/:name', to: 'questions#hashtags'

I have a specific question. So I'll understand if nobody helps me. :) Anyway, thanks in advance! How can I refactor this code using resources and restful agreement? Especially I mean render_with_hashtags_of_question(text) and render_with_hashtags_of_answer(text) methods and relevant route get '/questions/hashtag/:name', to: 'questions#hashtags'

问题模型:

require 'active_support/all'

class Question < ActiveRecord::Base
  TAG_REGEX = (/#[\p{L}_]+/)

  belongs_to :user
  belongs_to :author, class_name: 'User'
  has_and_belongs_to_many :tags

  validates :text, :user, presence: true
  validates :text, length: { maximum: 255 }

  after_save :add_hashtags

  after_commit do
    self.tags.clear
    add_hashtags
  end

  private

  def add_hashtags
    hashtags = self.text.scan(TAG_REGEX)
    hashtags << self.answer.scan(TAG_REGEX) if self.answer.present?
    hashtags.uniq.map do |hashtag|
      tag = Tag.find_or_create_by(name: hashtag.to_s.mb_chars.downcase.delete('#[]"'))
      self.tags << tag
    end
  end
end

question_controller.rb:

question_controller.rb:

class QuestionsController < ApplicationController
  before_action :load_question, only: [:show, :edit, :update, :destroy]
  before_action -> { authorize_owner(@question.user) }, only: [:update, :destroy]

  def index; end

  def show; end

  def edit; end

  def hashtags
    tag = Tag.find_by(name: params[:name])
    @questions = tag.questions
  end

  def create
    @question = Question.new(question_params)
    @question.author = current_user if current_user.present?

    if check_captcha(@question) && @question.save
      redirect_to user_path(@question.user), notice: 'Question was successfully created.'
    else
      render :edit
    end
  end

  def update
    if check_captcha(@question) && @question.update(question_params)
      redirect_to user_path(@question.user), notice: 'Question was successfully edited.'
    else
      render :edit
    end
  end

  def destroy
    user = @question.user
    @question.destroy
    redirect_to user_path(user), notice: 'Question was successfully deleted.'
  end

  private

  def load_question
    @question = Question.find(params[:id])
  end

  def authorize_owner(user)
    reject_user unless user == current_user
  end

  def question_params
    if current_user.present? && params[:question][:user_id].to_i == current_user.id
      params.require(:question).permit(:user_id, :text, :answer, :tag_id, :tag, { tag_ids: [] }, :tag_ids)
    else
      params.require(:question).permit(:user_id, :text, :tag_id, :tag, { tag_ids: [] }, :tag_ids)
    end
  end

  def check_captcha(model)
    verify_recaptcha(model: model) unless current_user.present?
  end
end

question_helper.rb:

question_helper.rb:

require 'active_support/all'

module QuestionsHelper
  TAG_REGEX = (/#[\p{L}_]+/)

  def render_with_hashtags_of_question(text)
    text.gsub(TAG_REGEX){
      |word| link_to word, "/questions/hashtag/#{word.mb_chars.downcase.delete('#')}"
    }.html_safe
  end

  def render_with_hashtags_of_answer(answer)
    answer&.gsub(TAG_REGEX){
     |word| link_to word, "/questions/hashtag/#{word.mb_chars.downcase.delete('#')}"
    }&.html_safe
  end
end

routes.rb:

routes.rb:

Rails.application.routes.draw do
  root 'users#index'

  resources :users
  resource :session, only: [:new, :create, :destroy]
  resources :questions, except: [:new] , :index]

  get '/questions/hashtag/:name', to: 'questions#hashtags'
end

schema.rb:

schema.rb:

ActiveRecord::Schema.define(version: 20161227111328) do

  create_table "questions", force: :cascade do |t|
    t.string   "text"
    t.string   "answer"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "user_id"
    t.integer  "author_id"
  end

  add_index "questions", ["user_id"], name: "index_questions_on_user_id"

  create_table "questions_tags", id: false, force: :cascade do |t|
    t.integer "question_id"
    t.integer "tag_id"
  end

  add_index "questions_tags", ["question_id"], name: "index_questions_tags_on_question_id"
  add_index "questions_tags", ["tag_id"], name: "index_questions_tags_on_tag_id"

  create_table "tags", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "users", force: :cascade do |t|
    t.string   "name"
    t.string   "username"
    t.datetime "created_at",       null: false
    t.datetime "updated_at",       null: false
    t.string   "email"
    t.string   "password_hash"
    t.string   "password_salt"
    t.string   "avatar_url"
    t.text     "background_color"
    t.text     "font_color"
  end

end

几天来我一直在努力解决这个问题,但最终什么都没得到.

I've tried hard to solve the problem for several days honestly, but eventually have got nothing.

推荐答案

我进行了以下更改:

在QuestionsHelper中:

In QuestionsHelper:

def render_with_hashtags_of_question(text)
  text.gsub(TAG_REGEX){
    |word| link_to word, tag_path(word.mb_chars.downcase.delete('#'))
  }.html_safe
end

在routes.rb中:

And in routes.rb:

resources :tags, except: [:new, :destroy], param: :name

这篇关于Rails URL路径和资源路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆