Rails showing ActiveRecord_Relation in my view - html

Well my problem is when i call my model Question and show in a .each do, my view show the ActiveRecord but i don't now how to hide this or the right way to show my questions without the ActiveRecord.
In my view the content show like this:
<%= #questions = Question.all.order(:id).reverse_order %>
<% #questions.each do |question| %>
<% if #course.id == 1 %>
<h5><%= link_to question.title, question , class: 'reply text-light text-decoration-none' %></h5>
<% end %>
<% end %>

What is the difference between <%, <%=, <%# and -%> in ERB in Rails?
You could use <% %> instead of <%= %>:
<% #questions = Question.all.order(id: :desc) %>
And it's better to put that to controller:
class QuestionsController
def index
#questions = Question.all.order(id: :desc)
end
end

I found a solution if i want to call a Model in view, just create a input type="hidden" like this:
<input type="hidden" value="<%= #questions = Question.all.order(:id).reverse_order %>">

You should use url_helpers provided by Rails. See Examples provided under link_to docs.
Also I would suggest to follow the recommendation at https://stackoverflow.com/a/66470651/936494 and in your view change following
<h5><%= link_to question.title, question , class: 'reply text-light text-decoration-none' %></h5>
to
<h5><%= link_to question.title, question_path(question), class: 'reply text-light text-decoration-none' %></h5>
And if you don't have url_helpers available, then the 2nd argument should be a URL to a resource in your application.
Hope that helps. Thanks.

Related

Rails simple-form with multiple instances of the same object

I am trying to create a rails form using simple form that uses nested resources. However, I want to be able to submit multiple instances of the associated resource. Example below will probably explain it better.
<div class="tab-pane active" id="reminder">
<%= simple_form_for #collection, html: {multipart: true}, url: collection_index_path do |m| %>
<%= render partial: "collection/tabs/reminder", locals: { :m => m } %>
</div>
-inside partial
<% 9.times do |j|%>
<div class="tab-pane" id="<%= j %>">
<%= m.simple_fields_for :reminder do |p| %>
<%= p.input :heading %>
<%= p.input :message %>
<% end %>
</div>
There is a tabbed pane in which the user can click through 9 tabs to set up to 9 reminders, all should be associated with a collection (collection model accepts nested attributes for reminder). However, the way I have it setup now, the controller only gets what was set in the last reminder in the params. Anyway ideas would be appreciated.
There must be some way to distinguish tabs before submitting to controller. And i think answer might be here.
i.e. it looks like this:
<% 9.times do |j|%>
<div class="tab-pane" id="<%= j %>">
<%= m.simple_fields_for :reminders do |p| %>
<%= p.input :heading %>
<%= p.input :message %>
<% end %>
</div>
<% end %>

Rails 4: Access all CRUD from index.html.erb

I'm trying to create only a 1 page index site.
I Want to handle creating, deleting, editing all within my 1 index. (very basic)
However, I followed the beginners for getting started:
http://guides.rubyonrails.org/getting_started.html
Example of my article controller:
def index
#articles = Article.all #Create Articles and then add the for loop inside the index.html
#article = Article.new
end
def create
#article = Article.new(article_params)
if #article.save
redirect_to #article
end
end
def new
#article = Article.new
end
...
private
def article_params
params.require(:article).permit(:title, :text)
end
I Use #articles to populate all articles ever added. And then #article to for CRUD. Except I can't get full control to CRUD from the index.html page.
<form role="form" class="form-inline">
<div class="form-group">
<%= form_for #article do |f| %>
<div class="form-group">
<label for="textUserInputDescription"><%= f.label :title %><br/><%= f.text_field :title %>
</label>
<label for="textUserInputDescription"><%= f.label :text %><br/><%= f.text_area :text %> </label>
<button type="submit" class="btn btn-default"><%= f.submit %></button>
</div>
<% end %>
</div>
When I click Submit, It does not add to my database.
My Question is: How do I get access to CRUD all from within the index.html?
Thanks for your time, I'm still learning Rails/CRUD/ActiveRecord and following tutorials.
I changed f.submit to:
<%= button_to "Create", new_article_path(#article[:title], #article[:text]), method: :post %>

How to create useless buttons in rails form_for

I know that if you just type something like <button>Something</button> outside a form_for in rails, it will create a useless button.
But I want to create buttons within this form_for to be handled by JavaScript.
Is there a way to create it?
This will create useless buttons that can be handled by JavaScript.
Plain HTML:
<input type="button" onclick="alert('Hello.')" value="Click Here" />
Rails:
<%= submit_tag "Click Here", :type => 'button', :onclick => 'alert("Hello.")' %>
If you're not looking for Rails to use it, why not just use the plain html inside the form_for?
<%= form_for #record do |f| %>
## inputs ##
<button>Something</button>
<%= f.submit %>
<% end %>
Check out this answer: How do I create multiple submit buttons for the same form in Rails?
<% form_for(something) do |f| %>
..
<%= f.submit 'A' %>
<%= f.submit 'B' %>
..
<% end %>

if else condition using three models rails

I have 3 models
Users: id , name
Jobs: id, user_id, title
Applications: id, job_id, user_id
In Job#show page I am trying to put a button which is visible only to people who haven't applied to the job, haven't created the job and are logged in . I am using devise. I have managed to build correct relationship in these models (thanks to SO) as following.
user.jobs #list all jobs posted by the user
jobs.applicants #list all applicants on the job
Question is how to formulate if else condtion which shows button which submits a form (hidden) to the job#show page and puts job_id and user_id in the application model.
I tried
<% if user_signed_in? %>
<% if job.user_id = current_user.id %>
<div style="text-align:center;" class="widget">
<%= link_to(new_user_session_path, :class => "g-btn type_primary") do %>
<i class="icon-external-link"></i> Apply for the job
<% end %>
</div>
<% end %>
<% end %>
I can't seems to get the idea on how to get around error of object.id nil.
You seem to have missed an = sign.
You can improve your if condition like this
<% if user_signed_in? && job.present? && job.user_id == current_user.id %>
your logic here
<% end %>
how does a user apply for the job? using what controller?
you code has the user logging in again,
does the user need to create an application object and does it require additional information from the user to complete the process, or is more along the lines of send existing information from the user to information stored in the job.
If the latter you can do something like this.
resources :jobs do
member do
post 'apply'
end
end
<% if user_signed_in? %>
<% unless job.user == current_user %>
<div style="text-align:center;" class="widget">
<%= link_to 'Apply', apply_job_path(job), method: :post %>
</div>
<% end %>
<% else %>
<%= link_to 'Sign in to apply', new_user_session_path %>
<% end %>
then in your jobs controller
def apply
if Application.create job_id: params[:job_id], user: current_user
redirect_to jobs_path, notice: "You have applied, good luck!"
else
... do something for failure...
end
end
Try this instead
<% if user_signed_in? %>
<% if job.user_id == current_user.id %>
<div style="text-align:center;" class="widget">
<%= link_to "Apply for the job" new_user_session_path, :class => "g-btn type_primary" %>
</div>
<% end %>
<% end %>
and it might be better to change this line
<div style="text-align:center;" class="widget">
to
<div class="widget center">
and add a class named center to the relevant css sheet

Placing link at top

How do I place a link at the top of my page when the URL that it is pointing to is not determined until later down the page. In this example, I want to move Create and Edit Scenario links to the top of the page, but as you can see Edit Scenario depends on knowing the #scenario_id first.
<%= will_paginate #scens, :next_label => 'Older', :prev_label => 'Newer' %>
<div class="box">
<% for scenario in #scens %>
<% #created = scenario.created_at %>
<% #updated = scenario.updated_at %>
<% #scenario_id = scenario.id %>
<% if scenario.scenario_image.exists? %>
<%= scenario_image_tag(scenario) %>
<% end %>
<%= simple_format(scenario.description) %>
<% end %>
</div>
<% if session[:role_kind] == "controller" %>
<p>
<%= button_to "Create new scenario", :action => "create" %>
<% if #scens.size > 0 %>
<%= button_to "Edit scenario", :action => "edit", :id => #scenario_id %>
<% end %>
</p>
You can add the link at the top but you will need to programmatically access it later and then assign the URL to it. That needs some kind of reference or look-up capability, I'm thinking client-side javascript but that's as I don't know Ruby.
Alternatively you could create the link later when you have the URL and place the link at the top using CSS positioning. The actual position of all the DOM elements on the page need not match the order in which they are rendered.
One way to do this is to use a helper:
In your helper.rb file:
def stack_example(scens, &block)
html = 'Scenario Details'
edit_link = 'Edit Link'
yield html, edit_link
end
Then in your partial you could have something like:
<% stack_example(#scens) do |html, edit_link| %>
<%= edit_link %><br>
<%= html %>
<% end %>
Should output the following:
Edit Link
Scenario Details
I don't get it. Why do you create model in the view layer? Why wouldn't you create the model variables in the controller? Sth like:
class your_controller
def your_method
#scenario_id = ...
end
end
I think that your problem lays in the invalid MVC usage. Don't you think that all the #member #variables should be initialized before the view starts to render?