Rails time_select custom behavior edit - ruby-on-rails

I have a form with a time_select input field. I am manipulating the data in the controller and storing as integer.
In the view
<%= f.label :duration %>
<%= f.time_select :duration, {}, { class: 'form-control time-select' }%>
In the controller
def create
#task = Task.new(task_params)
#hours = (params[:task]['duration(4i)']).to_i
#minutes = (params[:task]['duration(5i)']).to_i
#task.duration = #hours*60 + #minutes
respond_to do |format|
if #task.save
format.html { redirect_to #task, notice: 'Task was successfully created.' }
format.json { render :show, status: :created, location: #task }
else
format.html { render :new }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
def edit
#task = Task.find(params[:id])
#task['duration(4i)'] = (#task.duration / 60).to_s
#task['duration(5i)'] = (#task.duration % 60).to_s
end
It works for the create, but not for the edit. I am getting
can't write unknown attribute duration(4i)

Related

passing array to f.select ruby on rails

On my rails application in the views/cards/_form i have the code for a f.select field:
<%= f.select :tag_speciality, options_for_select(#subdomain.tag_speciality), {}, {class: 'form-control'} %>
That give me the output:
<select class="form-control" name="card[tag_speciality]" id="card_tag_speciality">
<option value="Professor de Yoga">Professor de Yoga</option>
<option value="Professora de Yoga">Professora de Yoga</option>
<option value="Estúdio de Yoga">Estúdio de Yoga</option>
</select>
In my migration file i have:
add_column :cards, :tag_speciality, :string, array: true
When i go to the form and select any option, for example, "Professor de Yoga", and save, i get the result:
[]
instead of:
["Professor de Yoga"]
This is my controller:
def index
#cards = Card.all
end
def create
#card = #user.cards.new card_params
respond_to do |format|
if #card.save
format.html { redirect_to cards_from_subdomain_path(#subdomain.id), notice: 'Card was successfully created.' }
format.json { render :show, status: :created, location: #card }
else
format.html { render :new }
format.json { render json: #card.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #card.update(card_params)
format.html { redirect_to cards_from_subdomain_path(#subdomain.id), notice: 'Card was successfully updated.' }
format.json { render :show, status: :ok, location: #card }
else
format.html { render :edit }
format.json { render json: #card.errors, status: :unprocessable_entity }
end
end
end
# Never trust parameters from the scary internet, only allow the white list through.
def card_params
params.require(:card).permit(
:name,
:phone,
:email,
:cover,
:is_published,
:subdomain_id,
:domain_id,
:profile_id,
:is_solidarity,
:tag_speciality,
:tag_location,
user_id: []
)
end
end
Is there anything that am i missing?
Thanks
Ditto #EJ2015.
Just add
#card.tag_speciality.push(card_params[:tag_speciality])
#card.save
to your create.
respond_to do |format|
if #card.tag_speciality.push(card_params[:tag_speciality]) && #card.save
format.html { redirect_to cards_from_subdomain_path(#subdomain.id), notice: 'Card was successfully created.' }
format.json { render :show, status: :created, location: #card }
else
format.html { render :new }
format.json { render json: #card.errors, status: :unprocessable_entity }
end
end
Since your tag_speciality is an array column, you need to assign the data using array method in your controller.
#card.tag_speciality.push(card_params[:tag_speciality])
#card.save
Of course you need to modify the assignment of other attributes too. By the way, you can see what went wrong in your server log. In this case you should see that the database rejected commits.

Couldn't find Tray with 'id'=

Im creating a garden app that has trays and plants.
tray has_many plants, plant belongs_to tray etc.
Im getting the above error and Im not sure how to assign the tray_id to the new plant being created.
Here is the add plant button in my tray's show view
<%= link_to 'ADD PLANT', new_plant_path(#tray.id), class: "btn btn-raised btn-success hoverable" %>
Here is my plants_controller:
class PlantsController < ApplicationController
before_action :set_plant, only: [:show, :edit, :update, :destroy]
# GET /plants
# GET /plants.json
def index
#plants = Plant.all
end
def show
end
def new
#plant = Plant.new
end
def edit
end
def create
tray = Tray.find(params[:tray_id])
#plant = tray.plants.create(plant_params)
respond_to do |format|
if #plant.save
format.html { redirect_to #plant, notice: 'Plant was successfully created.' }
format.json { render :show, status: :created, location: #plant }
else
format.html { render :new }
format.json { render json: #plant.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #plant.update(plant_params)
format.html { redirect_to #plant, notice: 'Plant was successfully updated.' }
format.json { render :show, status: :ok, location: #plant }
else
format.html { render :edit }
format.json { render json: #plant.errors, status: :unprocessable_entity }
end
end
end
def destroy
#plant.destroy
respond_to do |format|
format.html { redirect_to plants_url, notice: 'Plant was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_plant
#plant = Plant.find(params[:id])
end
def plant_params
params.require(:plant).permit(:title, :notes, :category_id, :tray_id, images_files: [])
end
end
Here is my trays controller
class PlantsController < ApplicationController
before_action :set_plant, only: [:show, :edit, :update, :destroy]
def index
#plants = Plant.all
end
def show
end
def new
#plant = Plant.new
end
def edit
end
def create
tray = Tray.find(params[:tray_id])
#plant = tray.plants.create(plant_params)
respond_to do |format|
if #plant.save
format.html { redirect_to #plant, notice: 'Plant was successfully created.' }
format.json { render :show, status: :created, location: #plant }
else
format.html { render :new }
format.json { render json: #plant.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #plant.update(plant_params)
format.html { redirect_to #plant, notice: 'Plant was successfully updated.' }
format.json { render :show, status: :ok, location: #plant }
else
format.html { render :edit }
format.json { render json: #plant.errors, status: :unprocessable_entity }
end
end
end
def destroy
#plant.destroy
respond_to do |format|
format.html { redirect_to plants_url, notice: 'Plant was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_plant
#plant = Plant.find(params[:id])
end
def plant_params
params.require(:plant).permit(:title, :notes, :category_id, :tray_id, images_files: [])
end
end
Here is my form for creating new plants
<%= form_for(#plant) do |f| %>
<%= f.label 'NAME' %>
<%= f.text_field :title, class: 'form-control', id: 'focusedInput1', placeholder: 'ENTER NAME' %>
etc, etc
<% end %>
What am I doing wrong? Thanks for the help :)
The params[:tray_id] is nil on this line tray = Tray.find(params[:tray_id]) in your Posts controller.
You're also not passing tray_id anywhere in your params. You'll need to properly pass it as a param to your new action:
<%= link_to 'ADD PLANT', new_plant_path(tray_id: #tray.id), class: "btn btn-raised btn-success hoverable" %>
Then add a hidden field to pass :tray_id in your form:
<%= f.hidden_field :tray_id, value: params[:tray_id] %>
Now, you can find tray in your create action using tray = Tray.find(params[:plant][:tray_id]).

Issue with has_and_belongs_to_many association in rails

Can you help a noob, please?
I have 2 models - Player and Poker Tables, which have has_and_belongs_to_many association. When i try create player i catch error
undefined method `poker_table'
respond_to do |format|
**if #player.save**
format.html { redirect_to #player, notice: 'Player was successfully created.' }
format.json { render :show, status: :created, location: #player }
else
I use checkboxes for mark needed poker tables, here form code:
<% #poker_tables = PokerTable.all %>
<% #poker_tables.each do |poker_table| %>
<div>
<%= check_box_tag "player[poker_table_ids][]", poker_table.id %>
<%= poker_table.name %>
<%= poker_table.actual_time %>
</div>
<% end %>
create method and params
def create
#player = Player.new(player_params)
respond_to do |format|
if #player.save
format.html { redirect_to #player, notice: 'Player was successfully created.' }
format.json { render :show, status: :created, location: #player }
else
format.html { render :new }
format.json { render json: #player.errors, status: :unprocessable_entity }
end
end
end
def player_params
params.require(:player).permit(:email, :poker_table_ids => [])
end
I can create poker table, but couldn't create a player with associated poker tables.
I don't really understand what I'm doing wrong. I studied a lot of resources about this theme, but i can't find answer.

Assigning multiple associations with Simple_form

I understand how to implement a single has_many association using simple_form, but how do you assign an additional association from another model object?
In my code, I'm creating model object #opportunity. I'm currently assigning a company_id, but also need to assign a 'user_id.
#opportunity _form.html.erb
<% if user_signed_in? %>
<%= simple_form_for([#company, #company.opportunities.build], html: {class: "form-inline"}) do |f| %>
<%= f.error_notification %>
<%= f.input :description, label: false, placeholder: 'Create an opportunity', input_html: { class: "span4" } %>
<%= f.submit 'Submit', class: 'btn btn-small'%>
<% end %>
<% else %>
<%= link_to "Create an Account", new_user_registration_path %>
to contribute
<% end %>
opportunity_controller.rb
def create
#company = Company.find(params[:company_id])
#opportunity = #company.opportunities.create(params[:opportunity])
respond_to do |format|
if #opportunity.save
format.html { redirect_to company_path(#company), notice: 'Opportunity was successfully created.' }
format.json { render json: #opportunity, status: :created, location: #opportunity }
else
format.html { render action: "new" }
format.json { render json: #opportunity.errors, status: :unprocessable_entity }
end
end
end
Assuming that your user is logged in, you can change your controller action to the following:
def create
#company = Company.find(params[:company_id])
#opportunity = #company.opportunities.new(params[:opportunity]) # new instead of create
#opportunity.user = current_user # new
respond_to do |format|
if #opportunity.save
format.html { redirect_to company_path(#company), notice: 'Opportunity was successfully created.' }
format.json { render json: #opportunity, status: :created, location: #opportunity }
else
format.html { render action: "new" }
format.json { render json: #opportunity.errors, status: :unprocessable_entity }
end
end
end

Assign variable to user input in Rails view

In my view,the user needs input some data using collection_select.
I know the data can be accessed using params[] in the controller.
But how do I access the value the user right after has selected a value?
This is what I am trying to do(doesnt work):
<%= f.collection_select :photo_type, Upload::PHOTOTYPE, :to_s, :to_s, :include_blank => false, :id => "phototype"%>
<%= f.hidden_field :photo_id, :value => Photo.find_by_type(params[:photo_type]).id %>
My question is how do I access the :photo_type in the collection_select?
EDIT
I have tried using jQuery but I don't know how to export the js variable to the view:
<script type="text/javascript">
$("#phototype").change(function() {
var phototype = $('#phototype').val()
});
</script>
UPDATE
I have two tables in my database:
Table 1: photos
id
photo_type_id (refer to id in the photo_types table)
Table 2: photo_types
id
photo_type
User can select photo type from the drop down menu, and I want to find the photo_type_id in the photo_types table by the user input and then insert the photo_type_id into the photos table
According to codeit, I changed my controller like this:
def create
#photo = photo.new(params[:photo])
photo_type_id = PhotoType.find_by_type(params[:photo_type]).id
respond_to do |format|
if #photo.save
format.html { redirect_to #photo, notice: 'photo was successfully created.' }
format.json { render json: #photo, status: :created, location: #photo }
else
format.html { render action: "new" }
format.json { render json: #photo.errors, status: :unprocessable_entity }
end
end
end
I think you are using hidden_field to send value to next action. Why don't you do the same in controller action:
def create
#photo = Photo.new(params[:photo])
#photo.photo_type_id = PhotoType.find_by_type(params[:photo][:photo_type]).id
respond_to do |format|
if #photo.save
format.html { redirect_to #photo, notice: 'photo was successfully created.' }
format.json { render json: #photo, status: :created, location: #photo }
else
format.html { render action: "new" }
format.json { render json: #photo.errors, status: :unprocessable_entity }
end
end
end
View:
<%= f.collection_select :photo_type, Upload::PHOTOTYPE, :to_s, :to_s, :include_blank => false, :id => "phototype"%>
Suggestion: Standard practice is avoiding queries in views.

Resources