Cant assign value to input - ruby-on-rails

I have this form:
<%= form_for(Imagedocu.new) do |f| %>
<%= f.text_area :image %>
<%= f.text_field :patient_id, params[:id]%>
<%= f.text_field :type %>
<%= f.submit %>
<% end %>
How you can see i assign a value to one of the inputs:
<%= f.text_field :patient_id, params[:id]%>
I dont know why but now somehow i get the error:
undefined method `merge' for "73539":String
73539 is the params[:id]! What do i wrong?

This is a form that already contains an object (Imagedocu.new), so it "grabs" the value for patient_id directly from this object. If you want to overwrite this value, use this:
<%= f.text_field :patient_id, value: params[:id] %>
OR initialize the Imagedocu.new with a patient_id equal to params[:id]:
<%= form_for( Imagedocu.new(patient_id: params[:id]) ) do |f| %>
<%= f.text_area :image %>
<%= f.text_field :patient_id %>
<%= f.text_field :type %>
<%= f.submit %>
<% end %>
But a cleaner way is to initialize the new Imagedocu object in the controller's action:
# controller
def new
#imagedocu = Imagedocu.new(patient_id: params[:id])
# etc.
# view
<%= form_for( #imagedocu ) do |f| %>
# etc.

Related

Saving checkbox value in erb - rails

I have a nested form that saves information to three different models. One section of the form uses checkboxes and is supposed to save values 1-5. However, even when the boxes are checked the form returns value 0. I have tried several different variations of code for setting the checked value. Any help would be much appreciated. A section of the form code is below:
<%= form_for #newinstructor do |f|%>
<%= f.text_field :first_name %>
<%= f.text_field :last_name %>
<%= f.fields_for :through_ats do |tag_field| %>
<%= label_tag("What categories does your activity fit into?") %><br>
<%= label_tag(:tag, "Cooking") %>
<%= tag_field.check_box(:tag_id, :value => 1) %>
<%= label_tag(:tag, "Art") %>
<%= tag_field.check_box(:tag_id, :value => 2) %>
<%= label_tag(:tag, "Music") %>
<%= tag_field.check_box(:tag_id, :value => 3) %>
<%= label_tag(:tag, "Outdoors") %>
<%= tag_field.check_box(:tag_id, :value => 4) %>
<%= label_tag(:tag, "Food") %>
<%= tag_field.check_box(:tag_id, :value => 5) %>
<% end %>
<%= f.submit %>
<% end %>
Did you permit tag_id in params?
params.require(:instructor).permit(:name,
through_ats_attributes: [:id, :tag_id, :_destroy]
)
To fix the problem I had, I had to uniquely define each of the symbols in tag_field.checkbox, then require/permit them individually in params
so in the form i put:
<%= label_tag("Please check off the categories your activity fits into.") %><br>
<%= label_tag(:tag, "Cooking") %>
<%= tag_field.check_box(:cooking) %>
<%= label_tag(:tag, "Art") %>
<%= tag_field.check_box(:art) %>
instead of :
<%= label_tag("What categories does your activity fit into?") %><br>
<%= label_tag(:tag, "Cooking") %>
<%= tag_field.check_box(:tag_id, :value => 1) %>
<%= label_tag(:tag, "Art") %>
<%= tag_field.check_box(:tag_id, :value => 2) %>
and in the controller:
params.require(:instructor).permit(through_ats: [:cooking, :art, :music, :outdoors, :food])
instead of:
params.require(:instructor).permit(through_ats: [:id, :tag_id])

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] %>

Simple Form simple_fields_for only show last record

I have a nested simple form to edit a user. The user has a profile he/she can update, and a new record is written to the profile table.
simple_fields_for shows all profile records of a user due to the relationship user 1 to many profile records. However, I would like to only show the newest profile record in the form! How can I accomplish that?
<%= simple_form_for(#user) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :lang %>
<%= f.input :firstname %>
<%= f.input :lastname %>
<%= f.input :email %>
<%= f.input :born %>
<%= f.input :gender %>
<%= f.simple_fields_for :profile do |p| %> # the magic needed here
<%= p.input :postal_code %>
<%= p.input :core %>
<%= p.input :daytime %>
<%= p.input :style %>
<% end %>
<% if #is_new %>
<%= f.simple_fields_for :status do |s| %>
<%= s.input :entered, as: :hidden, input_html: { value: Time.current } %>
<% end %>
<% end %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
Little late, but accepted answer is not 100% accurate. (Would just make this a comment, but no rep.) I would do this like so:
Assuming you have
class User < ActiveRecord::Base
has_many :profiles
...
def latest_profile
profiles.order(...) # query to get whatever record it is you want
end
end
your simple_fields_for can just be
<%= f.simple_fields_for :profiles, f.object.latest_profile do |p| %>
Note the pluralization of :profiles, and that we don't need to clutter the controller with an additional instance variable because you can access the object in the form. (Using the singular will work I believe, but you will not get params with a key in the form of :profiles_attributes, meaning even more code to account for unique params.)
The edit action:
class user > ApplicationController
...
def edit
#profile = #user.profile.order("saved DESC").first
end
...
end
The working form:
<%= simple_form_for(#user) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :lang %>
<%= f.input :firstname %>
<%= f.input :lastname %>
<%= f.input :email %>
<%= f.input :born %>
<%= f.input :gender %>
<%= f.simple_fields_for :profile, #profile do |p| %> # the magic needed here
<%= p.input :postal_code %>
<%= p.input :core %>
<%= p.input :daytime %>
<%= p.input :style %>
<% end %>
<% if #is_new %>
<%= f.simple_fields_for :status do |s| %>
<%= s.input :entered, as: :hidden, input_html: { value: Time.current } %>
<% end %>
<% end %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>

Form to assign value from variable

I am trying to get my form to assign values to text fields. I have a helper file that defines current_user. I want the text_field :name to be the current_user.name.
<%= form_for #message, :url => contact_path do |form| %>
<%= form.text_field :name = current_user.name %>
<%= form.text_field :email = current_user.email %>
...
I have tried #{name}, params, how do you assign this value?
You should do that in your controller
for example: (See 3rd line)
def update
#message = Message.find(params[:id])
#message.name = current_user.name ## HERE
#message.update_attributes
end
Unless you mean you want it to be the default value when the page renders, in which case:
<%= form.text_field :name, value: current_user.name %>
or if you like
<%= form.text_field :name, placeholder: current_user.name %>
Add value option to the text_field helper as:
<%= form_for #message, :url => contact_path do |form| %>
<%= form.text_field :name, value: current_user.name %>
<%= form.text_field :email, value: current_user.email %>
...

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

Resources