I started with the guide on http://guides.rubyonrails.org/getting_started.html. I successfully get it up and running. So far I made a few adjustments (like naming of the controller, etc.). This tutorial didn't cover how to update the relations of a model and this is the point I'm currently struggling.
models/game.rb
class Game < ApplicationRecord
has_many :achievements, dependent: :destroy
validates :title, presence: true,
length: { minimum: 5 }
end
models/achievement.rb
class Achievement < ApplicationRecord
belongs_to :game
end
The update of my games, work like a charm.
controller/game_controller.rb
def edit
@game = Game.find(params[:id])
end
def update
@game = Game.find(params[:id])
if @game.update(game_params)
redirect_to @game
else
render 'edit'
end
end
private
def game_params
params.require(:game).permit(:title, :total_gs_points)
end
So I tried to transfer it to my achievements
controller/achievements_controller.rb
def edit
@game = Game.find(params[:game_id])
@achievement = @game.achievements.find(params[:id])
end
def update
@game = Game.find(params[:game_id])
@achievement = @game.achievements.find(params[:id])
# @achievement = Achievement.find(params[:id])
if @achievement.update(achievement_params)
redirect_to game_achievements_path(@game)
else
render 'edit'
end
end
private
def achievement_params
params.require(:achievement).permit(:id, :title, :description, :status, :hint, :pinned, :gs_points)
end
But there, every time I call my edit route, it fires a create event.
Started GET "/games/1/achievements/3/edit" for 127.0.0.1 at 2017-06-06 15:57:45 +0200
Processing by AchievementsController#edit as HTML
Parameters: {"game_id"=>"1", "id"=>"3"}
Game Load (0.3ms) SELECT `games`.* FROM `games` WHERE `games`.`id` = 1 LIMIT 1
Achievement Load (0.3ms) SELECT `achievements`.* FROM `achievements` WHERE `achievements`.`game_id` = 1 AND `achievements`.`id` = 3 LIMIT 1
Rendering achievements/edit.html.erb within layouts/application
Rendered achievements/_form.html.erb (3.2ms) [cache miss]
Rendered achievements/edit.html.erb within layouts/application (8.2ms)
Completed 200 OK in 28ms (Views: 23.9ms | ActiveRecord: 0.6ms)
My included form _form.html.erb in views/achievements/edit.html.erb looks like:
<%= form_for([@game, @game.achievements.build]) do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :description %><br>
<%= f.text_field :description %>
</p>
<p>
<%= f.label :status %><br>
<%= f.select :status, options_for_select([ 'open', 'claimed' ], 'open') %>
</p>
<p>
<%= f.label :hint %><br>
<%= f.text_area :hint %>
</p>
<p>
<%= f.label :pinned %><br>
<%= f.check_box :hint %>
</p>
<p>
<%= f.label :gs_points %><br>
<%= f.select :gs_points, options_for_select([ 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100 ], 50) %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
On my edit.html.erb, I get the correct achievement information, but don't know why the fields are not getting prefilled. The Submit buttons text is every time "Create achievement"
edit.html.erb
<h1>Edit Achievement: <%= @achievement.title %></h1>
<%= render 'form' %>
<%= link_to 'Back', game_achievements_path(@game) %>
Thanks & Regards, mybecks