Accessing array of parameters inside the params[] value - ruby-on-rails

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

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.

Passing an ID to controller show method in 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

How to customise rails validation error messages with a nested form setup

I have a form with a nested object something like this:
<%= form_for(#person) do |f| %>
<% if #person.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#person.errors.count, "error") %> prohibited this record from being saved</h2>
<ul>
<% #person.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= render 'person_fields', f: f, :person => #person %>
<%= f.fields_for :posts do |builder| %>
<%= render 'post_fields', f: builder %>
<% end %>
<br />
<div class="actions">
<%= f.submit %>
</div>
<% end %>
The thing is the validation error messages come up in the format {attribute}{message}, i.e the regular full_messages format. The {attribute} also includes the model name which isn't what I want, I just want to display the attribute and the message.
I'm thinking I can potentially us the rails internationalisation api but could use some guidence; has anyone else managed to do this?
I worked out how to this... just for reference, here's my solution:
<%= form_for(#person) do |f| %>
<% #person.errors.messages.each do |msg| %>
<% msg[1].each do |m| %>
<% if msg[0].to_s.split(".")[-1] == "base" %>
<li><%= m %></li>
<% else %>
<li><%= msg[0].to_s.split(".")[-1].humanize.titlecase %> <%= m %></li>
<% end %>
<% end %>
<% end %>
<%= render 'person_fields', f: f, :person => #person %>
<%= f.fields_for :posts do |builder| %>
<%= render 'post_fields', f: builder %>
<% end %>
<br />
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I've used the custom-err-msg plugin in the past and had a lot of success.

rails Insert meta data from external url into table

I have a table for storing links. I'm trying to use the pismo gem for grabbing meta data from a url, and saving that to the table.
In my link controller I have a before filter :
def pismo_grab_meta_data
doc = Pismo::Document.new("http://google.com/")
#link_title = doc.title
end
Which gets called in the new and create action. In my view I pass the instance variable in a hidden field.
<%= f.hidden_field :name, :value => #link_title %>
This works when hard code the url as a string (like the above google example), but fails when I pass it the url params, like so:
doc = Pismo::Document.new(params[:url])
Error: undefined method `gsub!' for nil:NilClass
Pretty new to rails, so I assume I'm doing something stupid here. Any help is very welcome!
<%= form_for(#link, :html => { "data-behavior" => "submit-form"}) do |f| %>
<% if #link.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#link.errors.count, "error") %>
prohibited this link from being saved:
</h2>
<ul>
<% #link.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.hidden_field :name, :value => #link_title %>
<div class="field urlsaver">
<%= f.label :url %><br />
<% if params[:link].present? %>
<%= f.text_field :url, :value => params[:link] %>
<% else %>
<%= f.text_field :url %>
<% end %>
</div>
<div class="actions">
<%= f.submit 'Save', :class => 'btn' %>
</div>
<% end %>
The value you are looking for should be in params[:link][:url].
Change the line to :
doc = Pismo::Document.new(params[:link][:url])

Resources