Ich versuche, eine has-many Assoziation innerhalb einer Ruby on Rails App zu schaffen, wo ein Benutzer hat viele Ziele und ein Ziel hat viele SchritteRuby on Rails Goal hat viele Schritte Assoziation. Wie erstelle ich Schritte für ein Ziel?
Ich kann nicht scheinen, herauszufinden, wie man die Erstellung verknüpfen eines Schrittes für ein bestimmtes Ziel. Ich spiele schon eine Weile damit herum und schaue mich hier um, habe aber keine Lösung gefunden.
Im Folgenden sind meine Goal_controller, Step_Controller, Step Form und Tor Form
Tor Controller:
class GoalsController < ApplicationController
before_action :set_goal, only: [:show, :edit, :update, :destroy]
before_filter :authorize
# GET /goals
# GET /goals.json
def index
@goals = Goal.all
end
# GET /goals/1
# GET /goals/1.json
def show
@goal = Goal.find(params[:id])
session[:current_goal] = @goal.id
end
# GET /goals/new
def new
@goal = Goal.new
end
# GET /goals/1/edit
def edit
end
# POST /goals
# POST /goals.json
def create
@goal = current_user.goals.new(goal_params)
respond_to do |format|
if @goal.save
format.html { redirect_to @goal, notice: 'Goal was successfully created.' }
format.json { render :show, status: :created, location: @goal }
else
format.html { render :new }
format.json { render json: @goal.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /goals/1
# PATCH/PUT /goals/1.json
def update
respond_to do |format|
if @goal.update(goal_params)
format.html { redirect_to @goal, notice: 'Goal was successfully updated.' }
format.json { render :show, status: :ok, location: @goal }
else
format.html { render :edit }
format.json { render json: @goal.errors, status: :unprocessable_entity }
end
end
end
# DELETE /goals/1
# DELETE /goals/1.json
def destroy
@goal.destroy
respond_to do |format|
format.html { redirect_to goals_url, notice: 'Goal was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_goal
@goal = Goal.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def goal_params
params.require(:goal).permit(:Goal, :Description, :Date, :DueDate, :user_id)
end
end
Step Controller:
class StepsController < ApplicationController
#before_action :set_step, only: [:show, :edit, :update, :destroy]
before_filter :authorize
# GET /steps
# GET /steps.json
def index
@steps = Goal.find(params[:goal_id]).steps.all
end
def new
@step = Goal.find(params[:goal_id]).steps.new
end
# GET /steps/1
# GET /steps/1.json
def show
end
# GET /steps/1/edit
def edit
end
def create
@step = Goal.find(params[:goal_id]).steps.new(step_params)
respond_to do |format|
if @step.save
format.html { redirect_to @step, notice: 'Step was successfully created.' }
format.json { render :show, status: :created, location: @step }
else
format.html { render :new }
format.json { render json: @step.errors, status: :unprocessable_entity }
end
end
redirect_to(goal_steps_url(@goal))
end
def update
@step.update(step_params)
respond_to do |format|
if @step.update(step_params)
format.html { redirect_to @step, notice: 'Step was successfully updated.' }
format.json { render :show, status: :ok, location: @step }
else
format.html { render :edit }
format.json { render json: @step.errors, status: :unprocessable_entity }
end
end
end
# POST /steps
# POST /steps.json
def destroy
@step.destroy
respond_to do |format|
format.html { redirect_to steps_url, notice: 'Step was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_step
@step = Goal.find(params[:goal_id]).Step.find(params[:id])
end
def step_params
params.require(:step).permit(:requirement, :completionTime, :goal_id)
end
end
Schritt Form:
<%= form_for(@step) do |f| %>
<% if @step.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@step.errors.count, "error") %> prohibited this step from being saved:</h2>
<ul>
<% @step.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :requirement %><br>
<%= f.text_field :requirement %>
</div>
<div class="field">
<%= f.label :completionTime %><br>
<%= f.number_field :completionTime %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Tor Form:
<%= form_for(@goal) do |f| %>
<% if @goal.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@goal.errors.count, "error") %> prohibited this goal from being saved:</h2>
<ul>
<% @goal.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :Goal %><br>
<%= f.text_field :Goal %>
</div>
<div class="field">
<%= f.label :Description %><br>
<%= f.text_area :Description %>
</div>
<div class="field">
<%= f.label :Date %><br>
<%= f.date_select :Date %>
</div>
<div class="field">
<%= f.label :DueDate %><br>
<%= f.date_select :DueDate %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Ich habe versucht, es hinzuzufügen, aber jetzt bekomme ich den Fehler "undefined Methode' steps_path 'für # <#: 0x007fcdbd770ac8> "für die Zeile" <% = form_for (@step) do | f | %> "im Schrittformular. –
Frenchy