0
votes

~~~ Solution found! ~~~ I changed my def new method in jean_controller.rb to

def new
    @jean = Jean.create(params[:id])
end

and capitalized my Model and Description inside my _form partial (to match the capitalize Model and Description inside my migration file)

Thanks for all the help everyone!

~~~~~~~~~~~~~~~~~

So I have looked extensively for a solution to my problem, but to no avail. It drove me to create an account specifically to ask this question. I am using rails 4.2.0

I am using this tutorial https://mackenziechild.me/12-in-12/4/ to build a Pinterest clone, but I run into this error with my _form.html.haml partial:

ActionController::UrlGenerationError in Jeans#new /app/views/jeans/_form.html.haml where line #1 raised: No route matches {:action=>"show", :controller=>"jeans"} missing required keys: [:id]

I've tried a variety of solutions with input_html:, changing my models, jeans_controller...etc

Here is my partial code: (the first line is indented correctly, but I don't know how to get it to display properly on stackoverflow)

= simple_form_for @jean, html: { multipart: true } do |f|
- if @jean.errors.any?
    #errors
        %h2
            = pluralize(@jean.errors.count, "error")
            prevented this Jean Model from saving
        %ul
            - @jean.errors.full_messages.each do |msg|
                %li= msg

.form-group
    = f.input :model, input_html: { class: 'form-control' }

.form-group
    = f.input :description, input_html: { class: 'form-control' }

.form-group
    = f.input :price, input_html: { class: 'form-control' }

= f.button :submit, class: "btn btn-primary"

and here is my controller code:

class JeansController < ApplicationController

def index
end

def show
    @jean=Jean.find(params[:id])
end

def new
    @jean = Jean.new
end

def create
    @jean = Jean.new(jean_params)
end

private

def jean_params
    params.require(:jean).permit(:model, :description, :price)
end
end

and my routes.rb

Rails.application.routes.draw do
  resources :jeans
  root "jeans#index"
end

Can anyone help me? :(:

1
Do you have any link in your _form partial to your jean? like to show it?Yan Foto
I just have: (ignore the ; it represents the next line) %h1 New Jeans; = render 'form'; = link_to "Back", root_pathnvrpicurnose
I found the solution! I changed my def new method to def new @jean = Jean.create(params[:id]) end and edited my _form to capitalize Model and Description because it is capitalized in my migration file. Thanks for all the help everyone!nvrpicurnose
This solution started to create empty records in my database, how did you avoid it?Rodrigo Ferrari

1 Answers

0
votes

You need to create a show action in your controller to show the newly posted jean, basically add this to your controller :

def show
  @jean=Jean.find(params[:id])
end

You also need to change your create action to

def create
  @jean = Jean.create(jean_params)
end