Getting this rails error: ActionController::ParameterMissing in PostsController#new - ruby-on-rails

Error: ActionController::ParameterMissing in PostsController#new
param is missing or the value is empty: post
I'm working on a 2 model rails app and when I try to go to the "new post" link to create a post this error comes up.
BUT none of my actual code is highlighted...a blank line is highlighted above my "private, def post-params" code.
def destroy
#post = Post.find(params[:id])
#post.destroy
redirect_to posts_path
end
error code highlights here
private
def post_params
params.require(:post).permit(:name, :photo_url, :description, :resume)
end
form.html.erb
<%= form_for :post, url: posts_path(#post), method: :post do |f| %>
<p><%= f.label :name %><br> <%= f.text_field :name %> </p>
<p> <%= f.label :photo_url %><br> <%= f.text_area :photo_url %> </p>
<p> <%= f.label :description %><br> <%= f.text_area :description %></p>
<p> <%= f.label :resume %><br> <%= f.text_area :resume %> </p>
<p> <%= f.submit %> </p>
<% end %>
I have all of the correct params from my schema listed, so I think it might have to do with syntax or my path?
Nothing I've changed in syntax or path naming has worked so far.

Related

Rails Nested Form - Unpermitted Parameters

I have an Orders model that has many Items and Customers. For some reason, and I have looked high and low for a solution, I am unable to save the attributes for Items and Customers in their respected tables when using a nested form.
I'm relatively new to Rails, so my apologies if I've missed something obvious.
Started POST "/orders" for ::1 at 2017-05-16 16:12:17 -0600 Processing
by OrdersController#create as HTML Parameters: {"utf8"=>"✓",
"authenticity_token"=>"k1QPQ560j44Mx0SNeCMBQAUGLQfEi4g0QpDfHuYeP4Zd7nVgcHJf3qXNsVQv29dV+5G0oJ7wiaoQ3Idw+DP+iw==",
"order"=>{"customer"=>{"name"=>"Test", "email"=>"Test",
"phone"=>"Test"}, "item"=>{"name"=>"Test", "ref_num"=>"Test",
"retail"=>"122"}, "status"=>"Test", "arrival"=>"06/06/19",
"tracking"=>"Test439"}, "commit"=>"Save Order"} Unpermitted
parameters: customer, item
Orders Controller
def create
#order = Order.new(order_params)
#order.items.build
#order.customers.build
#order.save
redirect_to #order
end
private
def order_params
params.require(:order).permit(:status, :arrival, :tracking,
customers_attributes: [:name, :phone, :email],
items_attributes: [:name, :ref_num, :retail])
end
Orders Model
class Order < ApplicationRecord
has_many :items
has_many :customers
accepts_nested_attributes_for :items, :customers
end
Orders Form (New)
<%= form_for :order, url: orders_path do |f| %>
<%# customer parameters%>
<%= f.fields_for :customers do |cu| %>
<p>
<%= cu.label :customer_name %><br>
<%= cu.text_field :name %>
</p>
<p>
<%= cu.label :email %><br>
<%= cu.text_field :email %>
</p>
<p>
<%= cu.label :phone %><br>
<%= cu.text_field :phone %>
</p>
<% end %>
<%# Item parameters %>
<%= f.fields_for :items do |it| %>
<p>
<%= it.label :item_name %><br>
<%= it.text_field :name %>
</p>
<p>
<%= it.label :reference_number %><br>
<%= it.text_field :ref_num %>
</p>
<p>
<%= it.label :retail %><br>
<%= it.text_field :retail%>
</p>
<% end %>
<%# order parameters%>
<p>
<%= f.label :status %><br>
<%= f.text_field :status %>
</p>
<p>
<%= f.label :est_arrival %><br>
<%= f.text_field :arrival %>
</p>
<p>
<%= f.label :tracking %><br>
<%= f.text_field :tracking %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= link_to 'Back', orders_path %>
This is happening because form_for :order doesn't make reference to your model object, it makes a simple :order param, which doesn't takes into consideration your model's accepts_nested_attributes_for, thus not building f.fields_for :customers with _attributes suffix. You should instantiate a Order object for the expected behavior to happen in your form_for #object instead of just referencing a :object which renders only the name of the param.

rails - Getting an id from the url and then passing it as an argument for creating an object

First off I would like to ask how to redirect after clicking the submit button in a creation form. I have a form for creating a quiz and after they hit submit I want to redirect them to the link 'quiz/:id/add_question' where :id is the id of the just created quiz, but instead I just get redirected to /quizzes and it leads to a "The connection was reset" error in my browser.
Here is my quiz form:
<div>
<h2>Create Quiz</h2>
<%= form_for(#quiz) do |f| %>
<div>
<%= f.label :name %>
<%= f.text_field :name %> </br>
<%= f.label :subject %>
<%= f.text_field :subject %>
</div>
<%= f.submit "Submit" %>
<% end %>
</div>
And my quizzes controller:
class QuizzesController < ApplicationController
def new
#quiz = Quiz.new
end
def create
#quiz = Quiz.new(quiz_params)
flash[:success] = "Quiz Created successfully" if #quiz.save
redirect_to 'quiz/' + #quiz.id.to_s + '/add_question'
end
private
def quiz_params
params.require(:quiz).permit(:name, :subject)
end
end
The quiz/:id/add_question link leads to a question creation form:
<div>
<%= form_for(#question) do |f| %>
<%= f.label :question %></br>
<%= f.text_field :question %></br>
<%= f.label :answer1 %></br>
<%= f.text_field :answer1 %></br>
<%= f.label :answer2 %></br>
<%= f.text_field :answer2 %></br>
<%= f.label :answer3 %></br>
<%= f.text_field :answer3 %></br>
<%= f.label :answer4 %></br>
<%= f.text_field :answer4 %></br>
<%= f.label :correct_id %></br>
<%= f.text_field :correct_id %></br>
<%= f.submit "Add question" %>
<% end %>
</div>
The other part of my question is how can I pass another argument for the creation of the question object here. I don't want the user to enter that argument because the argument should be the id from the current url (quiz/:id/add_question).
First of all i think you made a spelling error:
redirect_to 'quiz/' + #quiz.id.to_s + '/add_quesiton'
Should be
redirect_to 'quiz/' + #quiz.id.to_s + '/add_question'
But rather you should use the url helper that is available.
redirect_to quiz_add_question_path(#quiz.id)
Assuming thats the name of the route. You can find that name by running rake routes
For the id of the current quiz you can set a hidden attribute for example
<%= f.hidden_field :quiz_id, value: params[:id] %>

undefined method ` ' for #<ContactsController:0x007fc3a821e300>

I have been following the tutorial (http://guides.rubyonrails.org/getting_started.html ) to create my own web app for create a contact manager.
When I want to create a new contact, I have code in my contacts_controller
class ContactsController < ApplicationController
def show
#contact = Contact.find(params[:id])
end
def new
end
def create
#contact = Contact.new(contact_params)
#contact.save
redirect_to #contact
end
private
def contact_params
params.require(:contact).permit(:firstname, :lastname, :email, :phonenumber, :notes)
end
end
In the view I have
<h1>New Contact</h1>
<%= form_for :contact, url: contacts_path do |f| %>
<p>
<%= f.label :firstname %><br>
<%= f.text_field :firstname %>
</p>
<p>
<%= f.label :lastname %><br>
<%= f.text_field :lastname %>
</p>
<p>
<%= f.label :email %><br>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :phonenumber %><br>
<%= f.text_field :phonenumber %>
</p>
<p>
<%= f.label :notes %><br>
<%= f.text_area :notes %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
In routes.rb
Rails.application.routes.draw do
get 'welcome/index'
root 'welcome#index'
resources :contacts
end
However, when I submit the form, I got this error message.
I think I followed the tutorial correctly and have the new method in my class. This seems weird to me and I have no idea to start debugging this app.

Rails params[:email] always returning nil

I have the following simple rails page:
<h1> New User </h1>
<%= form_for :user, url:users_path do |f| %>
<p>
<%= f.label :email %>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %>
<%= f.text_field :password %>
</p>
<%= f.submit %>
<% end %>
The create action gets executed and I want to access the :email attribute.
def create
render text: params[:email].inspect
end
The above always displays nil.
form_for :user will place all parameters beneath a :user key
render text: params[:user][:email].inspect

Point a form to a specific controller action ruby on rails

Does anyone know how to point the following comments form to the hello action in my comments controller below, any advice would be appreciated:
<h2>Add a comment:</h2>
<%= form_for([#venue, #venue.comments.build]) do |f| %>
<p>
<%= f.label :commenter %><br />
<%= f.text_field :commenter %>
</p>
<p>
<%= f.label :body %><br />
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
//////////////////my comments controller:
def hello
#venue = Venue.find(params[:venue_id])
#comment = #venue.comments.create(params[:comment].permit(:commenter, :body))
redirect_to venue_path(#venue)
end
You have to set route properly, for example:
resources :venues do
resources :comments do
collection do
post :hello
end
end
end
and set form url to this action's url:
<%= form_for(#venue.comments.build, url: hello_venue_comments_path(#venue)) do |f| %>

Resources