Rails url路径和资源路由

时间:2017-01-06 19:35:08

标签: ruby-on-rails ruby routes helper

我有一个具体的问题。所以如果没有人帮助我,我会理解的。 :)无论如何,提前谢谢!如何使用资源和restful协议重构此代码?我的意思是render_with_hashtags_of_question(text)render_with_hashtags_of_answer(text)方法和相关路线 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:

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:

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中:

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:

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

老实说,我已经努力解决这个问题好几天了,但最终什么都没有。

1 个答案:

答案 0 :(得分:0)

我做了以下更改:

在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中:

resources :tags, except: [:new, :destroy], param: :name
相关问题