I'm current following the Rails Getting started guide, including creating a blog with post and comments models. In the post show method, there is a form to create a new comment. Like so.
<%= form_for([#post, #post.comments.build]) do |f| %>
<div class="field">
<%= f.label :commenter %><br />
<%= f.text_field :commenter %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
However, I want to prohibit users from posting more than one comment on a particular post. How would I go about doing this? I've been working with validators but I can't seem to wrap my head around adding errors and displaying them on the posts#show page.
The issue was actually in my controller. Since submissions are a nested resource of posts, I need to re-set #post before calling render 'posts/show' in case of an error. This works, however you'll notice that you'll end up on a URL such as /posts/4/submissions/ with the appropriate webpage, rather than /posts/4
Related
I have a Ticket model, which has many Comments. In the edit view, I allow users to add comments, using cocoon. However I want to ensure that previous comments can't be edited. In the view I use a partial called indexlist to render all the previous comments (and delete them). For brevity, I haven't included that code. So in the view I have
<h1>Comments</h1>
<%= render 'comments/indexlist' %>
<br /><br />
<%= f.fields_for :comments do |tc| %>
<%= render partial: 'comment_fields', locals: {f: tc} %>
<% end %>
<div>
<%= link_to_add_association 'Add Comment', f, :comments, :class => "btn btn-primary" %>
</div>
The problem is using f.fields_for :comments do |tc| ... is that it renders each previous comment, but without it, if there are validation errors on the subform all the data is lost during the form round trip.
I'm sure there's an easy solution to this one...
I guess you'll need to have a conditional in the comment_fields
<% if f.object.user_id == current_user.id %>
<div class="field">
<%= f.label :content %>
<%= f.text_field :content %>
</div>
<% else %>
<%= f.object.content %>
<% end %>
That will let you edit your own comments.
The other way you could do it is have one form purely for the ticket and use ajax to add/edit comments rather than use cocoon and nested_attributes which would be the like the way stackoverflow works.
I have started the "Get Started Guide" from the ruby on rails website. Everything works fine, but when I change the order of showing all comments and than display the comments-form in the other way round, than the form_forfunction adds a empty comments model to #post.comments and so, I display one empty comment in the loop.
Here is the view:
<h1><%= #post.name %></h1>
<p><%= #post.text %></p>
<h2>Add comment</h2>
<%= form_for([#post, #post.comments.build]) do |f| %>
<p>
<%= f.label :name %><br>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :email %><br>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<h2>Comments</h2>
<%= render #post.comments %>
The loop display two comments. One, that exists in the db and one, that has just empty attributes. If I delete the form, than all is shown up correct.
You can select only your persisted comments:
<%= render #post.comments.select(&:persisted?) %>
When you do post.comments.build against some post, it will be added to the post.comments collection and will be displayed along with other comments.
You can always use persisted to check if the object is present in database meaning id is assigned to it.
#post.comments.select(&:persisted?)
Note: .present? check donot work here so you have to use .persisted.?
present check only assosiated to the parent.
Rails newb here. I'm trying to use the awesome_nested_fields gem (https://github.com/lailsonbm/awesome_nested_fields) in a simple tasks project. I've followed the instructions found in the link but I can't seem to get the dynamic add or remove item to work as shown in the demo here. I've been looking through the demo source code and I can't seem to figure out what I am doing incorrectly. Updating an already existing task works fine. Please let me know if you need more code my project.
Here is my partial containing the form:
<%= form_for(daily_list) do |f| %>
<div class="items">
<%= f.nested_fields_for :tasks do |f| %>
<fieldset class="item">
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :body %>
<%= f.text_field :body %>
remove
<%= f.hidden_field :id %>
<%= f.hidden_field :_destroy %>
</fieldset>
<% end -%>
</div>
add task
<div class="actions">
<%= f.submit "Update tasks for #{format_date(daily_list.date)}" %>
</div>
The only line I've added to my application.js file is the following (besides the necessary require statements):
$(document).ready(function(e)) {
$('FORM').nestedFields();
}
Help please! Thank you :)
I'm trying to put together a rails site that uses a number of models. One is a model called requests that takes in a couple pieces of information associated with essentially a single purchase. The other is a model that collects a number of the requests together under one "order" id.
The problem is, I want the view associated with request to allow for multiple request entries with one submission, and I can't figure that out. As I've currently got it, someone can submit a single request for a single "cut", but I want them to enter a number of requests for a number of cuts on a single page, with a single submission.
My current _form document associated with my view is:
<%= form_for(#request) do |f*| %>
<div class="field">
<%= f.label :cut %><br />
<%= f.text_field :cut %>
</div>
<div class="field">
<%= f.label :lbs %><br />
<%= f.number_field :lbs %>
</div>
<div class="field">
<%= f.label :notes %><br />
<%= f.text_field :notes %>
</div>
<div class="field">
<%= f.label "Order ID" %><br />
<%= f.number_field :order_id %>
</div>
<div class="field">
<%= f.label :status %><br />
<%= f.number_field :status %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Any idea how I can create a form document that allows me to enter several new 'rows' of requests with one submit? Or how to ensure they all have the same order-id?
Thanks.
Try these screencasts from Ryanb, I think they'll get you a long way.
http://railscasts.com/episodes/196-nested-model-form-part-1
http://railscasts.com/episodes/197-nested-model-form-part-2
I have a basic search form in my rails app that records each search that has been entered. I also want my users to be able to initiate a search with a url in the browser. E.g. http://www.example.com/searches?q=foo
I've played around with the different routing options and the logic in my controller but I can't seem to get this form:
<%= form_for(#search) do |f| %>
<div class="field">
<%= f.label :search %><br />
<%= f.text_field :search %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
To initiate a POST action on the database and submit a new search.
Thanks :)
Change your form to do a GET request instead of a POST then you can use both the form and the url:
<%= form_for(#search), :method => :get do |f| %>