Passing a parent_id via a form using the Ancestry gem - ruby-on-rails

I am new to rails, I am using the ancestry gem and I am having trouble passing the parent_id via a form.
<% title "Messages" %>
<% for message in #messages %>
<div class="message">
<div class="created_at"><%= message.created_at.strftime("%B %d, %Y") %></div>
<div class="content">
<%= message.content %>
</div>
<div class="actions">
<%= link_to "Reply", new_message_path(:parent_id => message) %> |
<%= link_to "Destroy", message, :confirm => "Are you sure?", :method => :delete %>
</div>
</div>
<% end %>
<%= semantic_form_for #message do |f| %>
<%= f.error_messages %>
<p><%= f.select :content, [['United States','2'],['England','3'],['London','4']] %></p>
<p><%= link_to "Reply", new_message_path(:parent_id => :content) %></p>
<% end %>
I am trying to assign a number (if the user selects 'United States' the number will be '2') to some variable and pass that variable when the user click reply as <%= link_to "Reply", new_message_path(:parent_id => some_variable) %>

You are doing it wrong. Rethink the problem. You probably want to do something like
<%= semantic_form_for #message do |f| %>
<%= f.error_messages %>
<p><%= f.select :parent_id, [['United States','2'],['England','3'],['London','4']] %></p>
<p><%= f.submit "Reply" %></p>
<% end %>
And define needed routes.

Related

how to Edit a post in rails and display said post in the text_field

I am trying to build a page that will host the content of the post from the psql data base and allow user to edit the content of that post inside the text field
I am getting a no method error however message is the correct column in database
any help is very appreciated
<h1>Edit message</h1>
<%= form_with(modle: #post, local: true) do |form| %>
<p>
<%= form.label :message %><br>
<%= form.text_field :message, :value => form.message %>
</p>
<p>
<%= form.submit %>
</p>
<%end%>
<%= link_to 'Back', posts_url %>
<h1>Posts shown below</h1>
<p>To add a new post click link:
<%= link_to new_post_path do %>
New post
<% end %>
</p>
<p>---------------------------------------------</p>
<% #posts.reverse_each do |post| %>
<div id = "<%=post.id %>">
<p><%= post.message %></p>
<p><%= post.created_at.strftime(" Posted at %a %I:%M%p") %></p>
<p><%= link_to 'Destroy', post, :confirm => 'Are you sure?',
:method => :delete %></td>
<p> <%= link_to 'Update', edit_post_path(post) %> </p>
<p>---------------------------------------------</p>
</div>
<% end %>
It should be form.object.message, make changes in this line
<%= form.text_field :message, :value => form.object.message %>
or as max said, just remove the value option all together, it will still work
<%= form.text_field :message %>
Give it a try!
<h1>Edit message</h1>
<%= form_with(model: #post, local: true) do |form| %>
<p>
<%= form.label :message %><br>
<%= form.text_field :message, :value => Post.find(params[:id]).message %>
</p>
<p>
<%= form.submit %>
</p>
<%end%>
<%= link_to 'Back', posts_path %>

Why is post title not showing up?

So here it is:
For some reason this is not showing the title for my post.
However when I press show option and it goes to post's page you can see al the detail.
_form.html.erb
<%= form_with(model: post, local: true) do |form| %>
<% if post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :date %>
<%= form.datetime_select :date, id: :post_date %>
</div>
<div class="field">
<%= form.label :name %>
<%= form.text_area :name, id: :post_name %>
</div>
<div class="field">
<%= form.label :user_id %>
<%= form.number_field :user_id, id: :post_user_id, value: current_user.id %>
</div>
<div class="field">
<%= form.label :description %>
<%= form.text_area :description, id: :post_description %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
#show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Date:</strong>
<%= #post.date %>
</p>
<p>
<strong>Name:</strong>
<%= #post.name %>
</p>
<p>
<strong>User:</strong>
<%= #post.user_id %>
</p>
<p>
<strong>Description:</strong>
<%= #post.description %>
</p>
<% if current_user == #post.user %>
<%= link_to 'Edit', edit_post_path(#post) %> |
<%end%>
<%= link_to 'Back', posts_path %>
<h1>Editing Post</h1>
<%= render 'form', post: #post %>
<%= link_to 'Show', #post %> |
<%= link_to 'Back', posts_path %>
edit.html.erb
<%= render 'form', post: #post %>
<%= link_to 'Show', #post %> |
<%= link_to 'Back', posts_path %>
I've tried changing it <%= form.text_area :name, id: :post_name %>
to string_area :name and also string_field but none of it is working.
In fact when I change it to the last 2, I get a method error if I go on the edit page. I'm still fairly new to rails and to ruby so I would greatly appreciate some help. I did try to google however and was unsuccessful, I guess I didn't know what to search for or maybe I worded it wrong.

Extend forms without page reload, rails

I'm searching for a solution to extend my form without page reload.
First I tried to render a partial with coffee or javascript, but escape_javascript didnt work.
Here's the view
<%= form_for #recipe = current_user.recipes.build do |f| %>
<%= f.label :name, "Recipe Name" %>
<%= f.text_field :name %>
<%= button_to "Add Ingredient", '#', class: "btn btn-lg btn-primary", id: "add" %>
<p></p>
<%= f.submit class: "btn" %>
<% end %>
The form above should be extented with following partial by every click on the button
<div id="recipe-ingredients">Quantity
<%= f.fields_for :quantities, #recipe.quantities.build do |quantity| %>
<%= render 'quantity_fields', f: quantity %>
<% end %>
</div>
_quantity_fields
<%= f.label :amount, "Amount:" %>
<%= f.text_field :amount %>
<%= f.collection_select(:ingredient_id, Ingredient.all, :id, :name) %>
This approach did not work (recipes.js.erb)
$(document).ready(function() {
$("#add").click(function() {
$("p").append("<%= escape_javascript
render(:partial => 'quantities') %>");
});
});
There's a workaround (see below) but I'm searching for a better solution.
$(document).ready(function() {
$("#workout-week").append(<%= escape_javascript(
Haml::Engine.new(File.read(File.join(Rails.root,'app/views',
'_show_period.html.haml'))).render(Object.new, period: #period)) %>);
});
A second approach is to write following lines in Coffee or JavaScript:
<%= f.fields_for :quantities, #recipe.quantities.build do |quantity| %>
<%= f.label :amount, "Amount:" %>
<%= f.text_field :amount %>
<%= f.collection_select(:ingredient_id, Ingredient.all, :id, :name) %>
<% end %>
I'm a newbie so take this with a grain of salt, but have you tried using render :foo instead of redirect_to :foo in the appropriate controller function?
I solved it with cocoon
<%= form_for #recipe do |f| %>
<div class="field">
<%= f.label :name %>
<br/>
<%= f.text_field :name %>
<%= f.fields_for :quantities do |quantity| %>
<%= render 'quantity_fields', :f => quantity %>
<% end %>
<div class="links">
<%= link_to_add_association 'add', f, :quantities %>
</div>
</div>
<%= f.submit %>
<% end %>

Rails Same page with url paramaters only change

Actually this is my view am getting values from mysql database and i showed in browser based on the params page=1 page =2 page =3 and i want to change the params[:page] in form next button click.
Based on pages=1,2,3 questions and answers will vary thats why i need url to be change while button click in same page.! like below URL http://localhost:3000/responses/new?page=1 Thanks in advance.
<%= form_for (#response) do |f| %>
<% #questions.each do |pgquestion| %>
<% if pgquestion.group_id == 0 %>
<%= label :pgquest,pgquestion.description %><br><br>
<% else %>
<%= label :pgquest,pgquestion.description %>
<% (1..pgquestion.question_value.to_i).each do |i| %>
<%= radio_button_tag "ans_value[#{ pgquestion.quest_id }]", i %>
<% end %>
<br><br>
<% if pgquestion.question_type == "textarea" %>
<%= text_area "", "ans_value[#{ pgquestion.quest_id }]" ,size: '80x5' %><br><br>
<% else if pgquestion.question_type == "text" %>
<%= text_field "", "ans_value[#{ pgquestion.quest_id }]" %> <br><br>
<% end %>
<% end %>
<% end %>
<% end %>
<div style="text-align: center">
<%= f.submit 'Prev' ,:class => 'btn btn-primary',:name => "previous_button" %>
<%= f.submit 'Next', :class => 'btn btn-primary' %> <br><br>
<p><%= f.submit "Continue" %></p>
<p><%= f.submit "Back", :name => "previous_button" %></p>
</div>
<% end %>
As far as i understand your problem use the will_paginate gem https://github.com/mislav/will_paginate

adding categories for posts rails

I'm trying to add catagories to my posts with a has_and_belongs_to_many relationship
I"m pretty close but just a little off.
here is my post index.html.erb
<div class="container">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Post Index<small> by title</small></h1>
</div>
</div>
<%= form_tag posts_path, :method => :get do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
<% end %>
<center><%= link_to 'Create New Post', new_post_path %></center>
</p>
<p><% #posts.sort_by(&:comments_count).reverse.each do |post| %></p>
<p><%= image_tag avatar_url(post.user), size: "31x31" %> <%= post.user.name %></p>
<p><strong>Title: </strong><%= post.title %></p>
<p><strong>Summary: </strong><%= post.summary %></p>
<p><%= post.catagories.name %>
<p><%= link_to 'Read Full Post', post %> Total Comments for post . . . (<%= post.comments.count %>)</p>
<p><strong>Posted ON: </strong><%= post.created_at.strftime('%b %d, %Y') %></p>
<br>
<p><% if current_user.admin? %><%= link_to "delete", post, method: :delete, data: { confirm: "You sure" } %>
<% end %>
<% if current_user.admin? %><%= link_to 'Edit', edit_post_path(post) %><% end %></p>
<% end %>
</div>
catagory.rb file
class Catagory < ActiveRecord::Base
has_and_belongs_to_many :posts
end
post.rb file
class Post < ActiveRecord::Base
belongs_to :user
has_many :comments
has_and_belongs_to_many :catagories
mount_uploader :image, ImageUploader
searchable do
text :title, :boost => 5
text :content
end
def comments_count
comments.count
end
end
post _form.html.erb
<%= form_for #post, :html => {:multipart => true} do |f| %>
<% if #post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.file_field :image %>
</div>
<div class="field">
<%= f.label :summary %><br>
<%= f.text_field :summary %>
</div>
<div class="field">
<%= f.label :content %><br>
<%= f.text_area :content %>
</div>
<div class="field">
<% Catagory.all.each do |catagory| %>
<%= check_box_tag catagory.id %>
<%= catagory.name %><br/>
<% end %><br>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
</div>
right now in the new it just says Catagory in stead of one of the three catagories I set in my rails console in my post index.html.erb file
#<ActiveRecord::Relation [#<Catagory id: 1, name: "lolcats", created_at: "2013-11-20 15:17:05", updated_at: "2013-11-20 15:17:05">, #<Catagory id: 2, name: "surfing", created_at: "2013-11-20 15:17:42", updated_at: "2013-11-20 15:17:42">, #<Catagory id: 3, name: "dance", created_at: "2013-11-20 15:18:11", updated_at: "2013-11-20 15:18:11">]>
the check box comes up fine in my post _form though, anyone feel like taking a stab at it?
according to this rails cast you need to change check_box_tag here
<% Catagory.all.each do |catagory| %>
<%= check_box_tag catagory.id %>
<%= catagory.name %><br/>
<% end %><br>
to
<%= check_box_tag "post[catagory_ids][]", catagory.id, #post.catagories.include?(catagory) %> #why catAgory, by the way?

Resources