0

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

mybecks
  • 2,443
  • 8
  • 31
  • 43

1 Answers1

0

What does your routes file look like? You may be able to just do something like this:

<%= form_for @achievement do |f| %>
< your form code >

Based on your update method Rails should be able to find the achievement associated with the specific game. I'm not 100% sure though, let me know if this works for you. Edit: You can probably do something in routes like this:

resources :games do
    resources  :achievements 
end
  • my `routes.rb` file looks exactly like yours. I also tried your form_for variant, but with that, I'm not able to create 'games' ... and if I run that on an existing achievement, then the edit screen didn't change. – mybecks Jun 06 '17 at 18:16
  • @mybecks I think I may have misunderstood your question. Using "@game.achievements.build" creates a new achievement linked to the game. You might be able to try something like this for the form: "<% form_for ([@game, @achievement]) do |f| %>". Check out this answer where someone was having a similar problem to what you're encountering: https://stackoverflow.com/questions/2034700/form-for-with-nested-resources – Christian O'Rourke Jun 07 '17 at 18:25
  • thanks for poinitng out that link. I finally got it working, following line did the trick. `<%= form_for([@game, @achievement||= @game.achievements.build ]) do |f| %>` – mybecks Jun 20 '17 at 06:37
  • @mybecks No problem, I'm glad it helped! Learning RoR can be really annoying when you first start. – Christian O'Rourke Jul 04 '17 at 17:48