rails cant create an object - ruby-on-rails

So I am making a colors palette manager and both tables requested on the form are nested and properly related(I would like to also point out that I am a rookie to rails)
This whole code was working perfectly fine when in line 4 was requesting links instead of just the names and suddenly wont work anymore, thank you for the help in advance
<div class='nav_bar'>#</div>
<% if #projects %>
<% #projects.each do |project| %>
<li><div class='container'><h1><%= project.name project_path(:id => project.id) %></h1>
<ul>
<%= form_for project do %>
<%= fields_for :palette, project.palette do |palette| %>
<%= palette.label 'background_dark_color' %>:
<%= palette.text_field :background_dark_color, placeholder: '#palette.background_dark_color' %><br/>
<%= palette.label 'background_light_color' %>:
<%= palette.text_field :background_light_color, placeholder: "#palette.background_light_color" %><br/>
<%= palette.label 'dark_color1' %>:
<%= palette.text_field :dark_color1, placeholder: "#palette.dark_color1" %><br/>
<%= palette.label 'dark_color2' %>:
<%= palette.text_field :dark_color2, placeholder: "#palette.dark_color2" %><br/>
<%= palette.label 'light_color1' %>:
<%= palette.text_field :light_color1, placeholder: "#palette.light_color1" %><br/>
<%= palette.label 'light_color2' %>:
<%= palette.text_field :light_color2, placeholder: "#palette.light_color2" %><br/>
<%= palette.submit %>
<%= link_to 'Destroy', project, method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>
</div>
</ul>
<% end %></li>
<%end%>
<%end%>
<div class='circle'><%= link_to "+", new_project_path %></div>
<button type="button"><%= link_to "+", new_project_path %></button>

I think you are referring to the following line,
<%= project.name project_path(:id => project.id) %>
There is a mistake in this code. There are two ways to fix this.
Solution 1
If you want to display a link or an anchor tag this use replace this with the following code,
<%= link_to project.name, project_path(:id => project.id) %>
Solution 2
If you don't want a link but, just the name to appear then, do the following,
<%= project.name %>

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

How do I reload my comments partial with ajax?

So I'm a beginner in Rails and I'm trying to reload a partial when I create a Comment.
I am creating a sort of forum where I have Posts and Comments on Posts.
Here is the situation :
/views/posts/show.html.erb
<% #posts.each do |post| %>
<%= post.title.capitalize %>
<%= post.content %>
<%= render 'comments/comment', post: post, comments: post.comments %>
<%= form_tag(category_forum_post_comments_path(#category, #forum, post), :method => :post, class: 'form newtopic') do %>
<%= text_field_tag :content, nil, placeholder: 'Commenter', class: 'form-control' %>
<%= submit_tag "Envoyer", class: "btn btn-primary" %>
<% end %>
<% end %>
<%= form_tag(category_forum_posts_path(#category, #forum), :method => :post, class: 'form newtopic') do %>
<%= text_field_tag :title, nil, placeholder: 'Titre de votre Post', class: 'form-control' %>
<%= text_area_tag :content, nil, placeholder: 'Description', id: 'desc', class: 'form-control' %>
<%= submit_tag "Envoyer", class: "btn btn-primary" %>
<% end %>
and then in /views/comments/_comment.html.erb
<% comments.each do |comment|%>
<%= comment.content %>
<%= link_to 'Supprimer', category_forum_post_comment_path(#category, #forum, post, comment),
method: :delete, class: "btn-danger btn-sm", data: { confirm: 'Etes-vous sûr?' } %>
<% end %>
I would like to reload my comment partial on post.
I tried to understand so many posts on stackoverflow and tutorial that I could find online and still have no idea how to do it.
If someone would be kind enough to help me get through this it would be great.
What's the action that you want to trigger that reload? you are showing forms, links, it's not too clear.
Basically, you need to use remote: true on the form/link, then add a view for that action with .js.erb extension, and, on that view, use javascript to change the content of an specific element.
Something like:
1st, you need to give some unique way to reach the element you want with javacsript:
<% #posts.each do |post| %>
<div id="post_<%= post.id -%>">
<%= post.title.capitalize %>
<%= post.content %>
<div class='comments'>
<%= render 'comments/comment', post: post, comments: post.comments %>
</div>
<%= form_tag(category_forum_post_comments_path(#category, #forum, post), :method => :post, class: 'form newtopic') do %>
<%= text_field_tag :content, nil, placeholder: 'Commenter', class: 'form-control' %>
<%= submit_tag "Envoyer", class: "btn btn-primary" %>
<% end %>
</div>
<% end %>
<%= form_tag(category_forum_posts_path(#category, #forum), :method => :post, class: 'form newtopic') do %>
<%= text_field_tag :title, nil, placeholder: 'Titre de votre Post', class: 'form-control' %>
<%= text_area_tag :content, nil, placeholder: 'Description', id: 'desc', class: 'form-control' %>
<%= submit_tag "Envoyer", class: "btn btn-primary" %>
<% end %>
Note the div with id=post_x and the div with class comment
Now you can locate it using javacsript:
#your_view.js.erb
post = document.getElementById('post_<%= #post.id -%>');
comments = post.querySelector('comments');
And finally, also on your view, render the partial and replace comments' innerHTML
comments.innerHTML = '<%= j(render partial: "comments/comment", post: #post, comments: #post.comments) -%>';
Just a suggestion, use "comments" (in plural) for the name of the partial, since you are rendering all the comments and not just one.

simple form - Reddit-like site

I completed a search and found this topic that is close, How do I make simple form to use, I'm too new to fully understand
what I was looking at.
The answer was as follows:
Step 1
rails generate simple_form:install --bootstrap (which I did after adding the Gem and running Bundle Install)
Step 2
Told me to add wrappers (not sure if I need to complete this step)
Step 3
modify the simple_form and has this example:
<%= simple_form_for(#contact, url: supplier_contacts_path, html: { class:
'form-horizontal' }) do |f| %>
<div class="form-inputs">
<%= f.input :c_regular,wrapper: :horizontal_input_group do %>
<span class="input-group-addon">$</span>
<%= f.input_field :c_regular, :label => "Regular",class: "form-control" %>
<% end %>
</div>
<% end %>
My code is as follows(views/comments/show.html.erb):
<div id="comments">
<%= render :partial => #link.comments %>
</div>
<%= simple_form_for(#link, html {class: 'form-horizontal' }) do |f| %>
<div class="field">
<%= f.text_area :body, size: "60x12" %>
</div>
<%= f.submit "Add Comment", class: 'btn btn-primary" %>
<% end %>
Here's my partial(views/comments/_comments.html.erb):
<%= div_for(comment) do %>
<div class="comments_wrapper clearfix">
<div class="pull-left">
<p class="lead"><%= comment.body %></p>
<p><small>Submitted <strong><%= time_ago_in_words(comment.created_at) %>
ago</strong> by
<%= comment.user.email %></small></p>
</div>
<div class="btn-group pull-right">
<% if comment.user == current_user %>
<%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you
sure?' }, class: "btn btn-sm btn-default" %>
<% end %>
<% end %>
</div>
<% end %>

how can I send a param to maintain some checkboxes

There were some similar topics and I tried them but I couldn't resolve.
code
<td><%= check_box_tag "words_info[]",word.id,false,{id:id} %><label for="<%=id%>" >追加</label></td>
The above code is called by below one which is a search form.
<h2>フラッシュカードを作成してください</h2><br>
<div class="col-md-12">
<h4>検索</h4>
<%= search_form_for #q,url:start_words_path do |f| %>
<%= f.label :title_cont, 'タイトル' %>
<%= f.text_field :title_cont %>
<%= f.label :user_name_cont,"作者"%>
<%= f.text_field :user_name_cont%>
<%= f.label :answer_cont,"答え"%>
<%= f.text_field :answer_cont%>
<%= f.label :question_cont,"問題"%>
<%= f.text_field :question_cont%>
<%= button_tag :type => "submit", :class =>"btn btn-send" do %>
<i class="material-icons left">search</i>検索
<% end %>
<%end%>
<%= form_tag({:action=>"practice"}, {:method=>"get"}) do %>
<%=render "words/words_index"%> #rendered here
<hr>
<%= submit_tag "完了" , class:"btn btn-large btn-send"%>
<% end%>
</div>
Everything that I tried
<td><%= check_box_tag "words_info[]",word.id,params[:words_info]==word.id,{id:id} %><label for="<%=id%>" >追加</label></td>
<td><%= check_box_tag "words_info[]",word.id,params[:words_info].include?(word.id),{id:id} %><label for="<%=id%>" >追加</label></td>
<td><%= check_box_tag "words_info[]",word.id,params[:words_info].to_s==word.id.to_s,{id:id} %><label for="<%=id%>" >追加</label></td>
environment
ruby 2.3
rails 4.2.7
materialize css

Passing a parent_id via a form using the Ancestry gem

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.

Resources