Trying to render value of input field - ruby-on-rails

Trying to work with a input value present in the source code only when the user has tried to login once (and entered a wrong password).
The scenario is as follows:
First attempt before login, part of the code looks like this:
<div class="form-group">
<label class="control-label col-md-4" for="user_email">Email</label>
<div class="col-md-8">
<input class="text-field form-control" html="{:spellcheck=>"false"}" id="user_email" name="user[email]" type="email" value="" />
</div>
</div>
After a second attempt happens, the email address that was used it's rendered in value:
<div class="form-group">
<label class="control-label col-md-4" for="user_email">Email</label>
<div class="col-md-8">
<input class="text-field form-control" html="{:spellcheck=>"false"}" id="user_email" name="user[email]" type="email" value="heregoestheemail#gmail.com" />
</div>
</div>
How can I render that email address if the user is not logged in yet? I know that once logged I'd do something like <%= current_user.email %> but there is way to get that value attribute?

you should work with form_for
<%= form_for #user do |f| %>
<% f.text_field :email (not sure f.email_field works)
<%= end %>
be sure #user is define in controller (#user = User.new or #user is come back from controller with errors => show #user.errors and contain precedent values)
http://guides.rubyonrails.org/form_helpers.html

Related

What is the right way to print data from my model in Rails?

In my controller, I have:
def show
#adjuster = Adjuster.find(params[:id])
end
form_for let's me print the data, but I don't want to use it because it is limited in certain respects. I want to use the regular form method.
I tried this:
<%= form_for (#adjuster) do |f| %>
<div class="row">
<div class="large-4 columns">
<b>Adjuster Name</b></br>
<%= #adjuster.adjuster_name %>
</div>
</div>
I get an error saying:
#adjuster is empty
I just tested out this code (Just copy & paste), it's just completely ok, I don't seem anything wrong, working perfectly. I believe in your code not showing any error, you just make the adjuster_name column has a value, I think the value is empty for adjuster_name.
I just change the column_name such as adjuster_name to my own, and look generated HTML is like this
<form class="edit_adjuster" id="edit_adjuster_20" action="/adjusters/20" accept-charset="UTF-8" method="post">
<input name="utf8" type="hidden" value="✓">
<input type="hidden" name="_method" value="patch">
<input type="hidden" name="authenticity_token" value="q/DPVxBU5gU5Gc1McT3USYRu21P6YBKeTfnwpOILO3xOAxKu25XqWJavy5alRxD1wMSVrtXCvb9l5ttsWzskAw==">
<div class="row">
<div class="large-4 columns">
<b>Adjuster Name</b><br>
Adjuster Name
</div>
</div>
</form>
That means the code is completely ok
<%= form_for(#adjuster) do |f| %> #=> or <%= form_for (#adjuster) do |f| %>
<div class="row">
<div class="large-4 columns">
<b>Adjuster Name</b></br>
<%= #adjuster.adjuster_name %>
</div>
</div>
<% end %>
#=> controller
def show
#adjuster = Adjuster.find(params[:id])
end

Adding a new Favorite: Creating a form with the current_user.id

I'm trying to create a form that will save the current_user.id. Something is funky in my controller. Any thoughts on obvious issues?
View (new.html.erb)
<!-- Label and input for user_id -->
<div class="form-group">
<label for="user_id" class="control-label">
User
</label>
<%= current_user.id %>
<input type="hidden" name="user_id" value="<%= current_user.id%>">
</div>
Controller (favorites_controller.rb)
def create
#favorite = Favorite.new
#favorite.dish_comment = params[:dish_comment]
#favorite.user_id = curent_user.id
d = params[:dish_id]
r = params[:restaurant_id]
problem code:
#favorite.user_id = curent_user.id
I thing you should put an # in front of your current_user to make it accessible in the ERB and in the controller.
Plus, once you save the current_user, you should user params[:user_id] to match the erb (input name is user_id).
Please look at the rails server output, it will tell you what parameters are submitted in the request.
Thanks #jackhaskeyboard. I spelled "current" wrong.
I'm now getting the following errors: Add Favorite Error Messages
1. "User has already been taken"
-A user should be able to post unlimited favorites, so I"m not sure why this is occurring.
"Dishing can't be blank"
-The purpose of this form is to select two variables (dish & restaurant), search the dishing join table to see if that combination exists. If not, create one, if so, grab the dishing_id, and use it to create a new favorite entry (user & dish/restaurant)
Here's my view code:
<div class="row">
<div class="col-md-12">
<form action="/create_favorite" method="post">
<!-- Hidden input for authenticity token to protect from forgery -->
<input name="authenticity_token" type="hidden" value="<%= form_authenticity_token %>">
<!-- Label and input for user_id -->
<div class="form-group">
<% current_user.id %>
<input type="hidden" name="user_id" value="<%= current_user.id%>">
</div>
<!-- Label and input for dishing_id -->
<div class="form-group">
<label for="dishing_id" class="control-label">
Dish
</label>
<%= select_tag(:dish_id, options_from_collection_for_select(Dish.all, 'id', 'dish_name')) %>
</div>
<!-- Label and input for restaurant_id -->
<div class="form-group">
<label for="restaurant_id" class="control-label">
Restaurant
</label>
<%= select_tag(:restaurant_id, options_from_collection_for_select(Restaurant.all, 'id', 'name') ) %>
</div>
<button class="btn btn-success">
Create Favorite
</button>
or
Cancel
</form>
</div>
</div>

rails file_field does not pass parameters to controller

<div class="modal-body">
<%= form_for :#hr_detail_avatar, url: hr_detaila_path, html: {role: "form", class: "form-horizontal", :multipart =>true}, method: :put do |form|%>
<div class="form-group">
<label for="image" class="col-sm-2 control-label">Photo Code</label>
<div class="col-sm-10">
<%= form.file_field :avatar %>
<!--<input type="file" id="user_image" name="image[avatar]" />-->
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10" style="margin-left: 35%;">
<input type="submit" class= "buttons save"value="Save" />
<button type="button" class="buttons cancel" data-dismiss="modal">Close</button>
</div>
</div>
<% end %>
</div>
the file_field does not send the parameters to the controller.
The controller does not receive any parameters from the view and an error is shown stating that the parameters are empty.
You need to pass the :avatar in the strong parameters . Also you need to check this link.
In your controller search where you have defined params.. I mean search for params.require
there inside .permit() include :avatar

I have a customized form but whatever written on the form is not saved in database

I have a form that uses styles of twitter/bootstrap
however the content of the form is not saved.
May I please know what am I missing?
<%= form_for #customer_detail, url: { action: "create" } do |f| %>
<div class="form-group">
<div class="row">
<div class='col-sm-3'>
<label for="Check in">Check in:</label><br>
<div class='input-group date' id='datetimepicker1'>
<input class="form-control" type='text'>
<span class="input-group-addon"><span class=
"glyphicon glyphicon-calendar"></span></span>
</div>
</div><label for="Check out">Check out:</label><br>
<div class='col-sm-3'>
<div class='input-group date' id='datetimepicker2'>
<input class="form-control" type='text'>
<span class="input-group-addon"><span class=
"glyphicon glyphicon-calendar"></span></span>
</div>
</div>
</div>
<% end %>
Seems like you should read this article. It describe to create form use Rails helpers and action for save to database.

Test with Capybara cannot find a checkbox created using Simple Form association

I have a form created using Simple Form, as such
<%= simple_form_for #organisation do |f| %>
<div class="form-inputs">
<%= f.association :causes, as: :check_boxes %>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
The page works fine when I use a browser, but when I try to check this with Capybara, such as:
check('organisation_cause_ids_1')
And have tried many variations of this e.g.
find(:xpath , '//*[#id="organisation_cause_ids_1"]').set(true)
find("organisation_cause_ids_1").check
These always give an error:
Failure/Error: check('organisation_cause_ids_1')
Capybara::ElementNotFound:
Unable to find checkbox "organisation_cause_ids_1"
The HTML generated by Simple Form is:
<div class="input check_boxes optional organisation_causes">
<label class="check_boxes optional">Causes</label>
<span class="checkbox">
<label for="organisation_cause_ids_1" name="organisation[cause_ids]">
<input class="check_boxes optional" id="organisation_cause_ids_1" name="organisation[cause_ids][]" type="checkbox" value="1" />Cause A</label>
</span>
<span class="checkbox">
<label for="organisation_cause_ids_2" name="organisation[cause_ids]">
<input class="check_boxes optional" id="organisation_cause_ids_2" name="organisation[cause_ids][]" type="checkbox" value="2" />Hunger</label>
</span>
...
Edit: The problem was due to the lazy loading of the 'Causes' I created with the factories. They weren't being created so the page had no checkboxes.
Try with this
find_by_id('organisation_cause_ids_1').find("checkbox[value='1']").select_option
or maybe with this
find(:css, ".check_boxes[value='1']").set(true)

Resources