Passing an ID to controller show method in Rails - ruby-on-rails

I have the following view page in my test_app:
<p id="notice"><%= notice %></p>
<p>
<strong>Box count:</strong>
<%= #job.box_count %>
</p>
<p>
<strong>Install date:</strong>
<%= #job.install_date %>
</p>
<%= link_to "Back", :back %>
I have the following ruby code in my jobs_controller show method:
def show
#job = Job.find(params[:job_id])
end
The parameters being passed through to the view are ("customer_id" and "id" for the job) as follows:
{"customer_id"=>"1", "id"=>"23"}
When I load the view for any job, I get the following error:
Couldn't find Job without an ID
I must have an error in my show method, but I am unsure exactly what I am doing wrong.
Any ideas?
It looks as though the issue is that my form is not saving the data to the table, here is my form:
<%= form_for([#customer, #job]) do |f| %>
<% if #job.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#job.errors.count, "error") %> prohibited this job from being saved:</h2>
<ul>
<% #job.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :box_count %><br>
<%= f.number_field :box_count %>
</div>
<div class="field">
<%= f.label :install_date %><br>
<%= f.text_field :install_date %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Any idea why the data is not being saved?

You should be using :id, not :job_id:
def show
#job = Job.find(params[:id])
end

Related

Missing Parameters in Ruby on Rails

The Seller Profile has one seller. I am trying to post the seller profile details and assigning the seller_id to the current_seller. I am however, running into this error. I don't understand why the error says missing parameter because it seems all the needed params have being provided.
Error is get is ActionController::ParameterMissing (param is missing or the value is empty: seller_profiles
def create
#seller_profile = SellerProfile.new(seller_profile_params)
#seller_profile.seller = current_seller
respond_to do |format|
if #seller_profile.save
format.html { redirect_to root_path, notice: 'Seller profile was successfully created.' }
def seller_profile_params
params.require(:seller_profile).permit(:first_name, :other_name, :last_name, :email)
end
<%= form_tag seller_seller_profiles_path do |form| %>
<% if seller_profile.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(seller_profile.errors.count, "error") %> prohibited this seller_profile from being saved:</h2>
<ul>
<% seller_profile.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= label_tag :first_name %>
<%= text_field_tag :first_name %>
</div>
<div class="field">
<%= label_tag :other_name %>
<%= text_field_tag :other_name %>
</div>
<div class="field">
<%= label_tag :last_name %>
<%= text_field_tag :last_name %>
</div>
<div class="field">
<%= label_tag :email %>
<%= text_field_tag :email %>
</div>
<div class="actions">
<%= submit_tag %>
</div>
<% end %>
resources :sellers, only: [:new, :create, :show, :index, :destroy] do
resources :seller_profiles
end
You should use the form_for or the form_with helpers instead of form_tag. Those helper methods will take care of adding the wrapping seller_profile key.
<%= form_for seller_profile do |form| %>
<% if seller_profile.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(seller_profile.errors.count, "error") %> prohibited this seller_profile from being saved:</h2>
<ul>
<% seller_profile.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :first_name %>
<%= form.text_field :first_name %>
</div>
<div class="field">
<%= form.label :other_name %>
<%= form.text_field :other_name %>
</div>
... replicate the same change for the rest of the fields ...
<div class="actions">
<%= form.submit %>
</div>
<% end %>
If the error is (param is missing or the value is empty: seller_profiles, it's because you require :seller_profile, not :seller_profiles in your params.require

Rails Added column to a scaffold

I successfully added a column "name" to "posts", When I check the post in the rails console it says name is nil even after I added a post to the form.html, this is the form file.
<%= form_with(model: post, local: true) do |form| %>
<% if post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(post.errors.count, "error") %> prohibited this post
from being saved:</h2>
<ul>
<% post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :description %>
<%= form.text_field :description, id: :post_description %>
</div>
<div class="field">
<%= form.label :Name %>
<%= form.text_field :name, id: :post_name %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
Make sure you have added that attribute in your permitted param list in your controller.

undefined method `model_name', Rails

I am trying to implement simple form in home#index page using:
<%= render "forms/form"%>
Form look like this:
<%= form_for(#form) do |f| %>
<% if #form.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#form.errors.count, "error") %> prohibited this form from being saved:</h2>
<ul>
<% #form.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :defect %><br />
<%= f.text_field :defect %>
</div>
<div class="field">
<%= f.label :region %><br />
<%= f.text_field :region %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
But when I access that page, I got this error message:
undefined method `model_name' for NilClass:Class
First of all I though it is because my model name is also Form, but then I checked Rails 3 reserved words, but there wasn't "form" !
Thanks in advance!
In home controller, set the instance variable #form in the index action as:-
def index
#form = Form.new
end
Your #form instance variable's value is nil. You should set it in controller.

Accessing array of parameters inside the params[] value

I have the following Parameters:
Parameters: {"utf8"=>"✓",
"authenticity_token"=>"06Us9R1wCPCJsD06TN7KIV/2ZeH4dJlZVqc12gpKBbo=",
"run"=>{"box_id"=>"1"}, "tapes"=>{"1"=>{"tape_id"=>"1"},
"2"=>{"tape_id"=>"2"}, "3"=>{"tape_id"=>"3"}}, "commit"=>"Create Run"}}
I want to create a new "Run" with the box id of 1 and then associate the tapes 1 2 and 3 to this run with the box id of 1
I am not sure what code needs to go into the controller, i did try:
def create
#run = Run.new(params[:run])
#tape_ids = #run.build_tape(params[:run][:tapes])
#run.save
When i submit the form below, it creates a new Run with the correct box but associates no tapes with it.
<%= form_for(#run) do |f| %>
<% if #run.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#run.errors.count, "error") %> prohibited this tape
from being saved:
</h2>
<ul>
<% #run.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :box_id %>
<br/>
<%= f.text_field :box_id %>
</div>
<% (1..3).each do |index| %>
<%= fields_for :tapes do |ff| %>
<div class="field">
<%= ff.label :tape_id , :index => index%>
<%= ff.text_field :tape_id, :index => index %>
</div>
<% end %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
If you're not doing accepts_nested_attributes_for, then maybe this would work. I don't know what build_tape is supposed to do, but the following will get you an array of tape_ids to work with:
#tape_ids = params[:run][:tapes].map { |k, v| v["tape_id"].to_i }
To complete the association, you could do something like
params[:run][:tapes].each do |k, v|
t = Tape.find(v["tape_id"].to_i)
t.run = #run
t.save
end

Rails Screencast - Completely stuck on "fields_for"

i am following this simple tutorial and i followed all the steps... but the browser simply doesn't show me anything i put inside the fields_for tag.
<%= form_for :activity, :url => activities_path do |f| %>
<% if #activity.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#activity.errors.count, "error") %> prohibited this activity from being saved:</h2>
<ul>
<% #activity.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label "Foto" %><br />
<%= f.text_field :photo %>
</div>
<div class="field">
<%= f.label "Foto per la home" %><br />
<%= f.text_field :photoHome %>
</div>
<% for info in #activity.infos %>
This is rendered<br>
<% fields_for "activity[info_attributes][]", info do |info_form| %>
This is not rendered<br>
<p>
Titolo: <%= info_form.text_field :title %>
</p>
<p>
Descrizione: <%= info_form.text_field :description %>
</p>
<% end %>
<% end %>
<p><%= submit_tag "Create activity" %></p>
<% end %>
The above code result is this:
What am i missing?
Rails is sometimes (well ok, often) a bit confusing in which helpers want a <% vs which helpers want <%=. Try
<%= fields_for "activity[info_attributes][]", info do |info_form| %>
The tutorial you're following doesn't, but that's from 2007, and in Rails 3 this was changed. Current documentation on fields_for is here.

Resources