Created
December 8, 2014 00:48
-
-
Save px-amaac/cf710084afb136b0e49a to your computer and use it in GitHub Desktop.
I have a partial and it displays a step. The Destroy link is broken giving me the error about not having the tutorial id that is required. The partial is rendered in the show action of another view. How do I get the required field to the partial so it can be submitted as a parameter. The last file is how i manage it in the Create action.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <% if step.title != nil %> | |
| <p> | |
| <%= step.title %> | |
| <%= step.problem %> | |
| <%= step.solution %> | |
| <%= link_to 'Destroy', tutorial_step_path, method: :delete, data: { confirm: 'Are you sure?' } %> | |
| </p> | |
| <% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <p> | |
| <%= form_for([@tutorial, @step]) do |f| %> | |
| <div class="field"> | |
| <%= f.label :title %> | |
| <%= f.text_field :title %> | |
| </div> | |
| <div class="field"> | |
| <%= f.label :problem %> | |
| <%= f.text_area :problem %> | |
| </div> | |
| <div class="field"> | |
| <%= f.label :solution %> | |
| <%= f.text_area :solution %> | |
| </div> | |
| <%= f.submit "Add Step", class: "button success" %> | |
| <% end %> | |
| </p> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Showing /vagrant/cins465-p7/tutorialcreator/app/views/tutorials/_step.html.erb where line #6 raised: | |
| No route matches {:action=>"destroy", :controller=>"steps", :id=>"3"} missing required keys: [:tutorial_id] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <p id="notice"><%= notice %></p> | |
| <p> | |
| <strong>Title:</strong> | |
| <%= @tutorial.title %> | |
| </p> | |
| <p> | |
| <strong>Author:</strong> | |
| <%= @tutorial.author %> | |
| </p> | |
| <p> | |
| <strong>Description:</strong> | |
| <%= @tutorial.description %> | |
| </p> | |
| <%= render partial: 'step', collection: @steps %> | |
| <% if user_signed_in? && @tutorial.user_id == current_user.id %> | |
| <%= render 'steps_form' %> | |
| <% end %> | |
| <%= link_to 'Edit', edit_tutorial_path(@tutorial) %> | | |
| <%= link_to 'Back', tutorials_path %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class StepsController < ApplicationController | |
| before_action :correct_user, only: [:create, :destroy] | |
| def create | |
| @step = @tutorial.steps.create(step_params) | |
| if @step.save | |
| flash[:success] = "Step added" | |
| else | |
| flash[:error] = "Failed to add step" | |
| end | |
| redirect_to @tutorial | |
| end | |
| # DELETE /tutorials/1 | |
| # DELETE /tutorials/1.json | |
| def destroy | |
| @step.destroy | |
| respond_to do |format| | |
| format.html { redirect_to tutorials_url, notice: 'Step was successfully destroyed.' } | |
| format.json { head :no_content } | |
| end | |
| end | |
| private | |
| def correct_user | |
| @tutorial = current_user.tutorials.find_by(id: params[:tutorial_id]) | |
| redirect_to root_url if @tutorial.nil? | |
| end | |
| def step_params | |
| params.require(:step).permit(:title, :problem, :solution) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment