Showing nested form text_field as text in index page - ruby-on-rails

I'm totally new to Rails, so I'm sorry for the stupid question:
This is the code to generate my form and its nested form:
<%= form_for #job do |form| %>
<div><%= form.label :department %><%= form.text_field :department %></div>
<div><%= form.label :enabled %><%= form.check_box :enabled %></div>
<!-- job description form -->
<%= form.fields_for :job_descriptions do |subform| %>
<div><%= subform.hidden_field :language %></div>
<div><%= subform.object.language %></div>
<div><%= subform.label "Job Name" %><%= subform.text_field :title %></div>
<div><%= subform.label "Header" %><%= subform.text_field :short_text %></div>
<div><%= subform.label "Content" %><%= subform.text_area :text %></div>
<% end %>
<!-- end of job description form -->
<div><%= form.submit %></div>
<% end %>
While this is part of the index.html.erb where I want to display text_fields from my subform such as :title, :short_text and :text as static text (I'm trying with :title in this particular case):
<% #jobs.each do |job| %>
<li>
<%= job.title %>
<%= job.department %>
<%= job.enabled %>
<%= link_to 'Show', job_path(job) %>
<%= link_to 'Delete', job_path(job), method: :delete, data: { confirm: 'Are you sure?' } %>
</li>
<% end %>
The error I get is:
undefined method `title' for #<Job:0x007fcdde2001a8>
My job.rb file looks like this:
class Job < ActiveRecord::Base
has_many :job_descriptions, :dependent => :destroy
accepts_nested_attributes_for :job_descriptions
def self.languages
[:de, :en]
end
end
Thank you very much in advance for any help.

<%= form_for #job do |form| %>
...
<%= form.fields_for :job_descriptions do |subform| %>
<% if subform.object.active? %>
<%= subform.text_field :title %>
<% end %>
<% end %>
...
<% end %>

Related

How to put nested fields_for code directly in parent _form?

How can I rewrite the code in duels/_form so I can put the code from dueler_fields directly in there?
duels/_form
<%= simple_form_for(#duel) do |f| %>
<%= f.text_field :consequence %>
<%= f.text_field :reward %>
<%= f.fields_for :duelers do |dueler| %>
<%= render 'dueler_fields', :f => dueler %>
<% end %>
<%= link_to_add_association f, :duelers do %>
<span class="glyphicon glyphicon-plus"></span> Dueler
<% end %>
<% end %>
_dueler_fields
# I want to place these two lines of code in the _form
<%= f.number_field :user_id, placeholder: "Enter User ID" %>
<%= f.number_field :challenge_id, placeholder: "Enter Challenge ID" %>
duels_controller
def new
#duel = Duel.new
respond_with(#duel)
end
duels has_many duelers.
change f to dueler and you should be good to go. or:
<%= simple_form_for(#duel) do |f| %>
<%= f.text_field :consequence %>
<%= f.text_field :reward %>
<%= f.fields_for :duelers do |dueler_form| %>
<%= dueler_form.number_field :user_id, placeholder: "Enter User ID" %>
<%= dueler_form.number_field :challenge_id, placeholder: "Enter Challenge ID" %>
<% end %>
<% end %>

Gem dragonfly. How to make the list of current files on edit view?

I have model Project and in model have field documents(files). I am using gem dragonfly. When editing a project, how to make the list of current files? At this time on the edit page the list of files fields show: "The file is not selected". I do next: ​
projects_controller:
def edit
end
def update
if #project.update(project_params)
redirect_to #project
else
render :edit
end
end
private
def set_project
#project = Project.find(params[:id])
end
def set_payment_types
#payment_types = PaymentOption.all
end
def project_params
params.require(:project).permit(:title, :description, :price, :location,
:anonymity, :price_category, :category_id, :skill_list, documents_attributes: [:attachment], payment_option_ids: [])
end
edit.html.erb:
<%= form_for #project do |f| %>
<p>
<%= f.label 'Name' %>
<%= f.text_field :title %>
</p>
<P>
<%= f.label 'Budget' %>
<%= f.text_field :price %>
für
<%= f.select :price_category, Project::PRICE_CATEGORIES %>
</P>
<p>
<%= f.label 'Description' %>
<%= f.text_area :description %>
</p>
<p>
<%= f.label 'Category' %>
<%= f.collection_select :category_id, Category.all, :id, :name %>
</p>
<p>
<%= f.label 'Skills Freelancer (15 pieces) *' %>
<%= f.text_field :skill_list %>
</p>
<p>
<%= f.label 'Location' %>
<%= f.text_field :location %>
</p>
<ul>
<% #payment_types.each do |type| %>
<li><%= check_box_tag 'project[payment_option_ids][]', type.id %>
<%= type.name %>
</li>
<% end %>
</ul>
<p>
<b>Anonymity order</b>
</p>
Setup allows for players to remain anonymous. Note that this may reduce the number of responses.
<p>
<%= f.label 'Place Order anonymously' %>
<%= f.check_box :anonymity %>
</p>
<p>
<%= f.fields_for :documents do |d| %>
<%= render 'document_fields', f: d %>
<% end %>
<div class="links">
<%= link_to_add_association 'add file', f, :documents %>
</div>
</p>
<P>
<%= f.submit 'Edit' %>
</P>
<% end %>
​_document_fields.html.erb:
<div class="nested-fields">
<%= f.label :attachment %>
<%= f.file_field :attachment %>
<%= link_to_remove_association "remove file", f %>
</div>
document.rb
class Document < ApplicationRecord
belongs_to :project
dragonfly_accessor :attachment
end
On the edit page the list of files fields show: "The file is not selected", but must be specified ​current files.
file_field is for uploading new files only, not for displaying what is already in the database. For displaying a list of uploaded files, you should just display the name of the file and/or an image tag with the thumbnail and provide a destroy link.
You can also provide a link to create a new attachment.
edit.html.erb
<div>
<ul>
<%= #project.documents.each do |document| %>
<li>
<%= document.attachment.name %>
<%= link_to "Delete", document_path(document), method: :delete, data: { confirm: 'Are you sure you want to delete this?' } %>
</li>
<% end %>
</ul>
<div class="links">
<%= link_to_add_association 'add file', f, :documents %>
</div>
</div>

How to edit model on show view of other model

I have the model Customer, and binded by has_many model Location. This is customer and his locations (adresses). After creating a new customer application redirects on show view of Customer model (shows all data just entered). On show view I have form for location add. There are already entered locations shows on this view and each of them has the link to edit each separate location. I need, when user click on location edit link, he redirects to show view of Customer model, but the form already contains the data of clicked location. Show view code of Customer model:
<p>Customer info:<br>
<strong><%= #customer.name %></strong><br />
<%= #customer.kind.name %><br />
<%= #customer.adres %><br />
<%= #customer.phone %><br />
<%= #customer.comment %><br />
</p>
<h3>Delivery location:</h3>
<% if #locations %>
<ul>
<% #locations.each do |loc| %>
<li>
<%= loc.adres %>
<%= loc.phone %>
<%= loc.comment %>
</li>
<% end %>
</ul>
<% end %>
<%= form_for Location.new do |l| %>
<%= l.text_field :adres %><br>
<%= l.text_field :phone %><br>
<%= l.text_field :comment %><br>
<%= l.text_field :customer_id, type: "hidden", value: #customer.id %><br>
<%= l.submit "Add" %>
<% end %>
<%= link_to "Home", root_path %>
You'd just set #location depending on whether specific params have been sent:
#app/controllers/customers_controller.rb
class CustomersController < ApplicationController
def show
#customer = Customer.find params[:id]
#location = params[:location_id] ? #customer.locations.find(params[:location_id]) : #customer.locations.new
end
end
This would allow you to populate the form as follows:
#app/views/customers/show.html.erb
Customer info:<br>
<%= content_tag :strong, #customer.name %>
<%= #customer.kind.name %>
<%= #customer.adres %>
<%= #customer.phone %>
<%= #customer.comment %>
<h3>Delivery location:</h3>
<% if #customer.locations %>
<ul>
<% #customer.locations.each do |loc| %>
<%= content_tag :li do %>
<%= loc.adres %>
<%= loc.phone %>
<%= loc.comment %>
<%= link_to "Edit", customer_path(#customer, location_id: loc.id) %>
<% end %>
<% end %>
</ul>
<% end %>
<%= form_for #location do |l| %>
<%= l.text_field :adres %><br>
<%= l.text_field :phone %><br>
<%= l.text_field :comment %><br>
<%= l.text_field :customer_id, type: "hidden", value: #customer.id %><br>
<%= l.submit %>
<% end %>
--
To manage the routes (to pass location_id), you'll be best creating a custom route:
#config/routes.rb
resources :customers do
get ":location_id", on: :member, action: :show, as: :location #-> url.com/customers/:id/:location_id
end
This will mean you'll have to reference the other link in your view:
<%= link_to "Edit", customer_location_path(#customer, loc.id) %>

Extend forms without page reload, rails

I'm searching for a solution to extend my form without page reload.
First I tried to render a partial with coffee or javascript, but escape_javascript didnt work.
Here's the view
<%= form_for #recipe = current_user.recipes.build do |f| %>
<%= f.label :name, "Recipe Name" %>
<%= f.text_field :name %>
<%= button_to "Add Ingredient", '#', class: "btn btn-lg btn-primary", id: "add" %>
<p></p>
<%= f.submit class: "btn" %>
<% end %>
The form above should be extented with following partial by every click on the button
<div id="recipe-ingredients">Quantity
<%= f.fields_for :quantities, #recipe.quantities.build do |quantity| %>
<%= render 'quantity_fields', f: quantity %>
<% end %>
</div>
_quantity_fields
<%= f.label :amount, "Amount:" %>
<%= f.text_field :amount %>
<%= f.collection_select(:ingredient_id, Ingredient.all, :id, :name) %>
This approach did not work (recipes.js.erb)
$(document).ready(function() {
$("#add").click(function() {
$("p").append("<%= escape_javascript
render(:partial => 'quantities') %>");
});
});
There's a workaround (see below) but I'm searching for a better solution.
$(document).ready(function() {
$("#workout-week").append(<%= escape_javascript(
Haml::Engine.new(File.read(File.join(Rails.root,'app/views',
'_show_period.html.haml'))).render(Object.new, period: #period)) %>);
});
A second approach is to write following lines in Coffee or JavaScript:
<%= f.fields_for :quantities, #recipe.quantities.build do |quantity| %>
<%= f.label :amount, "Amount:" %>
<%= f.text_field :amount %>
<%= f.collection_select(:ingredient_id, Ingredient.all, :id, :name) %>
<% end %>
I'm a newbie so take this with a grain of salt, but have you tried using render :foo instead of redirect_to :foo in the appropriate controller function?
I solved it with cocoon
<%= form_for #recipe do |f| %>
<div class="field">
<%= f.label :name %>
<br/>
<%= f.text_field :name %>
<%= f.fields_for :quantities do |quantity| %>
<%= render 'quantity_fields', :f => quantity %>
<% end %>
<div class="links">
<%= link_to_add_association 'add', f, :quantities %>
</div>
</div>
<%= f.submit %>
<% end %>

Using form_for with Awesome Nested Set

I have a Comment model with the acts_as_nested_set enabled, but when I try to do something like this (for nested comments), i receive the error "comment_comments_path not found", presumably because the default pathing doesn't work with Awesome Nested Set. How do I get around this?
<%= form_for([#comment, #comment.children.build]) do |f| %>
<%= f.text_area :content, :placeholder=>'What do you think?'%>
<%= f.submit 'Submit Reply'%>
<% end %>
I also tried this:
<%= form_for(#comment) do |f| %>
<% #comment.children.each do |sub| %>
<%= f.fields_for :children, sub do |child| %>
<%= child.text_area :content, :placeholder=>'What do you think?'%>
<%= f.submit 'Submit Reply'%>
<% end %>
<% end %>
<% end %>
but it didn't generate a textbox for me to type in.
You're very close, yeah you have to build it first then have fields for, so this:
<% #comment.children.build %>
<%= form_for([#comment]) do |f| %>
<%= f.fields_for :children do |child| %>
<%= child.text_area :content, :placeholder=>'What do you think?'%>
<% end %>
<%= f.submit 'Submit Reply'%>
<% end %>
<% end %>
This will have a form for all existing children + the new one. If you want only a form for a new child then you'll want this instead:
<%= form_for([#comment]) do |f| %>
<%= f.fields_for #comment.children.build, :children do |child| %>
<%= child.text_area :content, :placeholder=>'What do you think?'%>
<% end %>
<%= f.submit 'Submit Reply'%>
<% end %>
<% end %>

Resources