Customized links in rails - ruby-on-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}" %>

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

Accessing virtual attribute from params

ruby 2.1.5
rails 4.2.1
I am trying to figure out how to access a virtual attribute from params.
In my books_controller.rb, I have:
wrap_parameters :books_list
def product_params
params.require(:product).permit(:author_name, :book_name, :books_list)
end
Obviously, the books_list is the virtual attribute. I will be saving it to the books_list table
In my new.html.erb,I have:
<% #title = "Books" %>
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<%= form_for(#books, html: { class: 'form-horizontal' }) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.form_group :author_name do |f| %>
<%= f.label :author_name, class: 'control-label col-md-2' %>
<div class='col-md-8'>
<%= f.text_field :author_name, class: 'form-control' %>
<%= f.error_messages %>
</div>
<% end %>
<div class="form-inputs">
<%= f.form_group :book_name do |f| %>
<%= f.label :book_name, class: 'control-label col-md-2' %>
<div class='col-md-8'>
<%= f.text_field :book_name, class: 'form-control' %>
<%= f.error_messages %>
</div>
<% end %>
<%= f.form_group :books_list do |f| %>
<%= f.label :books_list, class: 'control-label col-md-2' %>
<div class='col-md-8'>
<%= f.text_area :books_list, class: 'form-control' %>
<%= f.error_messages %>
</div>
<% end %>
</div>
<div class="form-actions col-md-offset-2 col-md-10">
<%= f.submit class: 'btn btn-primary' %>
<%= link_to "Cancel", test_sets_path, class: 'btn' %>
</div>
<% end %>
</div>
</div>
</div>
When I submit the form, here's what I get for params:
Parameters: {
"utf8"=>"✓",
"authenticity_token"=>"5mxkibQHkwRnzVU31A6pe9uezsmaWeYbCUgU+gUZPLmiAZPnN6si+smdEePU0lfGITh7gmmyChf/bOY+YQQcQg==",
"books"=>{"author_name"=>"Some Author", "book_name"=>"Dwan Of The Dead"},
"books_list"=>{"{:class=>\"form-control\"}"=>"book1\r\nbook2"},
"commit"=>"Create Book List"
}
I am having trouble accessing the books_list in params. Any ideas?
If books_list column is not exist in #book, it raises error.
There is no method f.form_group in rails, I think you use gem that defines form_group method.
Maybe "books_list"=>{"{:class=>\"form-control\"}"=>"book1\r\nbook2"} parameter is made by some bootstrap gem. you check check name attribute in html source.

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.

Rails Display Form Error Below Each Field

I have the following form
<%= form_for(#user) do |f| %>
<% if #user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% #user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= #user.errors.messages[:name] %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><br>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :phone %><br>
<%= f.text_field :phone %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I want to display form error for each field below it instead of everything on the top. Went through rails guide but was not able to figure how to do it.
You can write a helper to return error message for any field of any object
In /app/helpers/application_helper.rb:
def show_errors(object, field_name)
if object.errors.any?
if !object.errors.messages[field_name].blank?
object.errors.messages[field_name].join(", ")
end
end
end
In view file:
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
<p class='error'><%= show_errors(#user, :name) %></p>
</div>

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