Update record error when combined with delete - ruby-on-rails

I have a problem at the moment with my _form.html.erb for editing a game. My code for this class is as followed:
<%= form_for #game, :html => { :multipart => true } do |f| %>
<% if #game.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#game.errors.count, "error") %> prohibited this game from being saved:</h2>
<ul>
<% #game.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<div id="p1"><%= f.label :game_name %> </div>
<%= f.text_field :game_name %>
</div>
<br />
<div class="field">
<div id="p1"><%= f.label :genre %> </div>
<%= f.text_field :genre %>
</div>
<br />
<div class="field">
<div id="p1"><%= f.label :console %> </div>
<%= f.select :console, Game::CONSOLE_OPTIONS, :prompt => 'Select' %>
</div>
<br />
<div class="field">
<div id="p1"><%= f.label :condition %> </div>
<%= f.text_field :condition %>
</div>
<br />
<div class="field">
<div id="p1"><%= f.label :description %> </div>
<%= f.text_area :description, :rows => 6 %>
</div>
<%= f.file_field :photo %>
<br />
<div class="actions">
<%= f.submit %>
<%= button_to 'Destroy', root_url, :confirm => 'Are You Sure?', :method => :delete %>
</div>
<% end %>
The problem I have is as followed. If I click the delete button it will delete the record no problem, but if I click the update button it removes the record also. If I take out the delete command then it updates fine. How can I get both to display together whilst doing their individual duties?

You need extract your destroy button from your form_for of update.

Related

Showing multiple iterations of form fields when in edit file of scaffold

First, I have a model called Answers for my trivia game. It stores the multiple possible answers there are to each trivia question (a quiz has_many answers). I created a form via scaffold to make an easy UI for submitting a question with a set of four answers.
I want to do this from one form. When the user currently hits submit, I can post all four answers -- each with a different answer_id but sharing the same question_id (so I can associate 4 answers with one question) -- successfully, like so:
<%= form_for(#question) do |f| %>
<% if #question.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#question.errors.count, "error") %> prohibited this question from being saved:</h2>
<ul>
<% #question.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<div class="field">
<%= f.label :question_text %><br>
<%= f.text_field :question_text, class: "form-control" %>
</div>
</div>
<div class="form-group">
<div class="field">
<%= f.label :category_id %><br>
<%= f.number_field :category_id, class: "form-control" %>
</div>
</div>
<h2>Answer Options</h2>
<%= f.fields_for :answers do |answer| %>
<div class="form-group">
<div class="answers">
<div class="field">
<%= answer.label :answer_1 %><br>
<%= answer.text_field :answer_text, class: "form-control" %>
</div>
</div>
</div>
<% end %>
<%= f.fields_for :answers do |answer| %>
<div class="form-group">
<div class="answers">
<div class="field">
<%= answer.label :answer_2 %><br>
<%= answer.text_field :answer_text, class: "form-control" %>
</div>
</div>
</div>
<% end %>
<%= f.fields_for :answers do |answer| %>
<div class="form-group">
<div class="answers">
<div class="field">
<%= answer.label :answer_3 %><br>
<%= answer.text_field :answer_text, class: "form-control" %>
</div>
</div>
</div>
<% end %>
<%= f.fields_for :answers do |answer| %>
<div class="form-group">
<div class="answers">
<div class="field">
<%= answer.label :answer_4 %><br>
<%= answer.text_field :answer_text, class: "form-control" %>
</div>
</div>
</div>
<% end %>
If I check in console, it works! I have four different answers (so they are four rows in the table but with the same question_id. What is weird is if I then use the edit route, instead of populating four inputs (like new does), it shows 16 text_field inputs for Answers (labeled Answer 1, Answer 2, Answer 3, Answer 4, Answer 1).
Finally, the other reason I think there could be an issue would be how I am updating the nested Answers attribute through the controller, like so:
def question_params
params.require(:question).permit(:question_text, :category_id, :correct_answer, :answers_attributes => [:id, :answer_text])
end
Am I setting this up incorrectly, such that it would iterate 4x when showing in the Edit file?
I think you need to do little different while rendering the form for edit action:
You can try something like this:
<%= form_for(#question) do |f| %>
<% if #question.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#question.errors.count, "error") %> prohibited this question from being saved:</h2>
<ul>
<% #question.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title, class: "form-control" %>
</div>
</div>
<% unless #question.new_record? %>
<% #question.answers.each_with_index do |ans,i| %>
<%= f.fields_for :answers, ans do |answer| %>
<%= answer.label "answer_#{i+1}" %><br>
<%= answer.text_field :answer_text, class: "form-control" %>
<% end %>
<% end %>
<% else %>
<h2>Answer Options</h2>
<%= f.fields_for :answers, Answer.new do |answer| %>
<%= answer.label :answer_1 %><br>
<%= answer.text_field :answer_text, class: "form-control" %>
<% end %>
<%= f.fields_for :answers,Answer.new do |answer| %>
<%= answer.label :answer_2 %><br>
<%= answer.text_field :answer_text, class: "form-control" %>
<% end %>
<%= f.fields_for :answers,Answer.new do |answer| %>
<%= answer.label :answer_3 %><br>
<%= answer.text_field :answer_text, class: "form-control" %>
<% end %>
<%= f.fields_for :answers,Answer.new do |answer| %>
<%= answer.label :answer_4 %><br>
<%= answer.text_field :answer_text, class: "form-control" %>
<% end %>
<% end %>
<%= f.submit #question.new_record? ? "Add" : "Update" %>
<% end %>

My submit button won't work in rails and I'm not sure why

I'm creating a simple form in rails and the submit button doesn't work. I think I'm missing something obvious here but the html seems to be fine (I'm far froma front end expert). Any advice or pointers?
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h2>Apply</h2>
<hr class="star-primary">
</div>
</div>
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<div class="row control-group">
<% if #user.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(#user.errors.count, "error") %>.
</div>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="col-xs-12 floating-label-form-group controls">
<%= form_for #user, url: users_path(#user), :html => {:method => :post} do |f| %>
<%= f.label :name %>
<%= f.text_field :name, class: "form-control", placeholder: 'Name' %>
<%= f.label :email%>
<%= f.text_field :email, class: "form-control", placeholder: "Email" %>
<%= f.label :address%>
<%= f.text_field :address, class: "form-control", placeholder: "Address" %>
<%= f.label :phone %>
<%= f.text_field :phone, class: "form-control", placeholder: "Phone Number" %>
<%= f.submit "Apply"%>
<%end%>
</div>
</div>
</div>
</div>
</div>
Also, when the submit button fails, nothing happens. No error messages, no console errors. Nothing.
Take method out from the html hash?
form_for #user, url: users_path(#user), :method => :post do |f|
Try this:
<%= form_for #user, :url => {:controller => "your-controller-name", :action => "your-action-name"} do |f| %>
Can you try this one:
<%= form_for #user, url: {action: "create"} do |f| %>
...
...
<%= f.submit "Apply" %>
<% end %>
But I strongly suggest you to use simple_form gem.

Customized links in rails

I would like to create custom links into rails - for example I have my users filling out this form when they create a new pin:
<%= form_for #pin, html: { multipart: true } do |f| %>
<% if #pin.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#pin.errors.count, "error") %> prohibited this pin from being saved:</h2>
<ul>
<% #pin.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :image %>
<%= f.file_field :image, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :Name_of_your_startup %>
<%= f.text_field :startupname, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :City %>
<%= f.text_field :city, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :Tag %>
<%= f.text_field :tag, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :Reward_for_users %>
<%= f.text_field :reward, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :description %>
<%= f.text_field :description, class: "form-control" %>
</div>
<div class="form-group">
<%= f.submit class: "btn btn-primary" %>
</div>
<% end %>
In my the pins views I want to create custom url to tweet: So I have this url:
http://twitter.com/intent/tweet?text="HERE GOES THE CUSTOM PART"
How could I integrate at the end of this url, for example: <%= f.label :description %>
Any ideas on how I could do such a thing ?
The correct syntax should be
<%= link_to "Link text here", "https://twitter.com/intent/tweet?text=#{#pin.description}" %>

undefined local variable or method `f' in Rails and Paperclip

I'm trying to create a new map, and it's not working out so far:
undefined local variable or method `f' for #<#<Class:0x007ff46c0b12c0>:0x007ff46d777ba8>
I'm using Paperclip. The new page form:
<%= form_for #map, :url => maps_path, :html => { :multipart => true } do |form| %>
<% if #map.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#map.errors.count, "error") %> prohibited this map from being saved:</h2>
<ul>
<% #map.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :carname %><br />
<%= f.text_field :carname %>
</div>
<%= form.file_field :map %>
<div class="field">
<%= f.label :criticalcomponentlocations %><br />
<%= f.text_area :criticalcomponentlocations %>
</div>
<div class="field">
<%= f.label :warnings %><br />
<%= f.text_area :warnings %>
</div>
<div class="field">
<%= f.label :additionalinfo %><br />
<%= f.text_area :additionalinfo %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Since you pass form variable to the block passed to form_for method, you should substitute f with form.

Rails: How to make a form post to another controller action

I know you are usually supposed to use the linking between new/create and edit/update in rails, but I have a case where I need something else. Is there anyway I can achieve this same connection?
I have a form for a model and I want it to post the data (similar to how a new view, post to the create action).
Here is my form
<div id="new-job">
<%= form_for(#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 :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :location %><br />
<%= f.text_field :location %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="actions">
<%= f.submit "create job", id: "submit-job", class: "button small radius" %>
<%= link_to "go back", jobs_path, class: "button small radius secondary" %>
<div id="new-job-errors"> </div>
</div>
<% end %>
</div>
Use the :url option.
= form_for #job, :url => company_path, :html => { :method => :post/:put }

Resources