Ruby - Actualizar categoría de un articulo

 
Vista:
Imágen de perfil de Mosiah

Actualizar categoría de un articulo

Publicado por Mosiah (1 intervención) el 14/10/2016 13:19:20
Estoy realizando un curso de Ruby On Rails, pero tengo un dilema a la hora de actualizar una categoria de un articulo, tengo los siguientes archivos:

article.rb --> Modelo
articles_controller.rb -- > Controlador

y el archivo has_category.rb que es donde tengo el article_id y category_id

article.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class Article < ActiveRecord::Base
	include AASM #Incluimos la maquina de estado
	#para que nuestro modelo interactue con la gema.
		#La tabla
		#Campos
		#Escribir métodos
		belongs_to :user
		has_many :comments
		has_many :has_categories
		has_many :categories, through: :has_categories
 
		validates :title, presence: true, uniqueness: true #Valida que el elemento no esté vacío.
		validates :body, presence: true, length: {minimum: 20}
		before_save :set_visits_count
		#before_update :save_categories
		after_create :save_categories
		after_update :update_categories
 
		has_attached_file :cover, styles: { medium:"8000x8000", thumb:"300x200" }
		validates_attachment_content_type :cover, content_type: /\Aimage\/.*\Z/
 
		#Custom Setter
		#Metodo que permite asignar valor a un atributo de un objeto
		def categories=(value)
			@categories = value
		end
 
		def update_visits_count
			# self.save if self.visits_count.nil?
			self.update(visits_count: self.visits_count + 1)
		end
 
		aasm column: "state" do
			state :in_draft, initial: true
			state :published
 
			#creamos eventos
 
			event :publish do
				transitions from: :in_draft, to: :published
			end
 
			event :unpublish do
				transitions from: :published, to: :in_draft
			end
		end
 
		private
 
		def save_categories
			#raise @categories.to_yaml
			@categories.each do |category_id|
				HasCategory.create(category_id: category_id, article_id: self.id)
			end
		end
 
		def update_categories
			@categories.each do |category_id|
			HasCategory.update(category_id: self.id, article_id: self.id)
		end
	end
 
		def set_visits_count
			self.visits_count ||= 0
		end
	end

articles_controller.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class ArticlesController < ApplicationController
	#
	before_action :authenticate_user!#, except: [:show,:index]
	before_action :set_article, except:[:index,:new,:create]
	before_action :authenticate_editor!, only: [:new,:create,:update]
	before_action :authenticate_admin!, only: [:destroy]
	#GET /articles
	def index
	#Puede acceder desde la vista o controlador, sin inaccesibles.
	@articles =	Article.where(state: "publish") #todos los registros de la tabla article
	end
 
	#GET /articles/:id
	def show
		#@article.update_visits_count
		@comment = Comment.new
	end
 
	#GET /articles/new
	def new
		@article = Article.new #Aun no esta en la base de datos
		#@categories = Category.all
	end
 
	def edit
 
	end
 
	#POST /articles
	def create
		#INSERT INTO
		@article = current_user.articles.new(article_params)
		@article.categories = params[:categories]
		if @article.save
		redirect_to @article
	else
		render :new
	end
end
 
	#DELETE /articles/:id
	def destroy
		#DELETE FROM articles
		@article = Article.find(params[:id])
		@article.destroy # Destroy eliminar el objeto de la BD
		redirect_to articles_path
	end
 
	#PUT /article/:id
	def update
		# UPDATE
		@article.categories = params[:categories]
		@article.update(article_params)
		redirect_to @article
	else
		render :edit
	end
 
	private
 
	def set_article
		@article = Article.find(params[:id])
	end
	private
 
	def validate_user
		redirect_to new_user_session_path, notice: "necesitas iniciar sesión"
end
	private
	def article_params
		params.require(:article).permit(:title,:body,:cover,:categories)
	end
end

has_category.rb

1
2
belongs_to :article
  belongs_to :category

Alguna ayuda?
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder