You can see here that it seems like the raw contents of my DB are being printed to the page. I can't see anywhere in my code why there would be the raw output of the db printed to the view. Here is the code for the index view:
<div class="main">
<div="messages">
<%=#messages.each do |t|%>
<h2 class="subject"><%=t.subject%></h2>
<p class="content"><%=t.content%></p>
<% end %>
<%=link_to "Create Message", edit_path%>
</div>
</div>
The Create Form/View:
<div class="formWrapper">
<%= form_for #messages do |t|%>
<div class ="Inputs">
<%=t.text_field :subject%><br>
<%=t.text_area :content%>
<div class="submit">
<%=t.submit "Submit"%>
</div>
<%end%>
</div>
</div>
The Controller:
class MessagesController < ApplicationController
def index
#messages=Message.all
end
def new
#messages=Message.new
end
def create
#messages = Message.new(message_params)
if #messages.save
redirect_to '/'
else
render 'new'
end
end
private
def message_params
params.require(:message).permit(:content, :subject)
end
end
you don't need the = here: <%=#messages.each do |t|%>, the equals sign is telling erb to show every message on the view.
<% %>
Will execute Ruby code with no effect on the html page being rendered. The output will be thrown away.
<%= %>
Will execute Ruby code and insert the output of that code in place of the <%= %>
example...
<% puts "almost" %> nothing to see here
would render as
nothing to see here
however
<%= puts "almost" %> nothing to see here
would render as
almost nothing to see here
Look at <% %>(without equal) in ruby erb means?
Related
I'm having trouble with using link_to in Ruby on Rails. I'm making a blog like application which displays a feed of all user posts and allows users to click posts to view/edit them.
This is my _feed partial, which is used to render all the posts of a user.
<% if #feed_items.any? %>
<% #feed_items.in_groups_of(3, false).each do |feeds| %>
<div class="row">
<% feeds.each do |feed| %>
<div class="col-md-4">
<ol class="posts">
<%= link_to render feed , edit_post_path(feed) %>
</ol>
</div>
<% end %>
</div>
<% end %>
<% end %>
The line <%= link_to render feed , edit_post_path(feed) %> is what is throwing errors. I'm just not 100% certain how to write a link_to which also renders the feed. I've tried a lot of variations and nothing works. The error I get as it is currently written is: undefined method `keys' for "/posts/160/edit":String
This is my Posts controller, which I wrote after this error occurred in an attempt to fix it. I'm not sure if any of this is even necessary:
class PostsController < ApplicationController
before_action :find_note, only: [:show, :edit]
def edit
end
def show
end
private
def find_note
#post = Post.find(params[:id])
end
I'm sure my problem is super basic but I'm having trouble figuring out how to solve it. Any help is appreciated!
Use block to render a partial inside of a link:
<%= link_to edit_post_path(feed) do %>
<%= render feed %>
<% end %>
I have something like
<div class="userInput">
<%= form_for :scribble do |f| %>
<%= f.text_area :scribble, cols: 65, rows: 4,:maxlength => 255%>
<%= f.submit %>
<% end %>
</div>
1)My Scribble model has min and max character length validation, now how do I print the error messages here. If it is an instance variable I know how to print, but this is a symbol.
2) This code is present in the application.html.erb. I am not able to understand how do I move it into a view of Scribble controller other than appliation. Problem is this form is not independent, it is a part of action index display of controller Scribbles,(and the form should be displayed always) and action index is already doing listing of scribbles.
Controller
def index
#scribbles = Scribble.order("scribbles.scribble DESC").all
end
def show
end
def new
end
def create
#scribble = Scribble.new(profile_params)
#scribble.likes =#scribble.dislikes =#scribble.shares=0;
#scribble.save
#scribbles = Scribble.order("scribbles.scribble DESC").all
render :index
end
Here how i out-put any errors or validation messages:
Controller:
def create
#scribble = Scribble.new(profile_params)
#scribble.likes =#scribble.dislikes =#scribble.shares=0;
if #scribble.save
flash[:notice] = "Scribble is successfully created"
redirect_to root_url
else #
render 'index'
end
end
Views:
Create a partial to show error messages if any e.g _error_messages.html.erb
<% if object.errors.any? %>
<div id="error_explanation">
<div class="alert callout text-center" data-closable>
<p><strong>This form contains <%= pluralize(object.errors.count, 'error') %>.</strong></p>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<button class="close-button" aria-label="Dismiss alert" type="button" data-close>
<span aria-hidden="true">×</span>
</button>
</div>
</div>
<% end %>
Render errors:
Now you can call <%= render 'layouts/error_messages', object: #scribble %> and put it anywhere in your views to render the errors validation. note: the object is passed, so it can be re-use to any form. credits to Hartl Tutorial.
I'm a beginner at rails and thus far interplating data in views has been pretty straight forward. I've been introduced to something slightly new as far as how the controllers are setup and as a result I'm not sure how one would go about getting the data to present in the view.
First controller
class PagesController < ApplicationController
def index
#guestbook_entry = GuestbookEntry.new
render "welcome"
end
end
Second controller
class GuestbookEntriesController < ApplicationController
def create
GuestbookEntry.create(guestbook_entry_params)
redirect_to root_path, notice: "Thank you for your entry."
end
private
def guestbook_entry_params
params.require(:guestbook_entry).permit(:body)
end
end
And here is the welcome.html.erb
<h1>Welcome to My Guestbook</h1>
<br>
<%= image_tag("under_construction.gif") %>
<div id="guestbook-entries">
<p>Guestbook Entries:</p>
<ul>
</ul>
</div>
<%= form_for #guestbook_entry do |f| %>
<%= f.label :body, "Guestbook Entry:" %>
<%= f.text_area :body %>
<%= f.submit "Submit" %>
<% end %>
So it wants me to iterate through all the entries and display them on a welcome page that's located in view/pages/welcome.html.erb.
Up to this point I guess I've only been doing basic simple rails applications where the view corresponded with the controller, and followed the typical CRUD setup, where index would hold the #xxx = Xxxx.all and new/create would handle #xxx = Xxxx.new/create/build. I thought I could simply move the PageController's index action to create/new and do
def index
#guestbook_entry = GuestbookEntry.all
render "welcome"
end
To satisfy the test (it looks for render welcome in the index action)
This seems weird but again I admit, I'm a beginner.
If you want to list all the guest book entries on your root page you would do something like:
class PagesController < ApplicationController
def index
#guestbook_entry = GuestbookEntry.new
#guestbook_entries = GuestbookEntry.limit(10).all
render "welcome"
end
end
And in your view you would list them like:
<% if #guestbook_entries.any? %>
<div id="guestbook-entries">
<p>Guestbook Entries:</p>
<% #guestbook_entries.each do |entry| %>
<ul>
<li class="entry"><%= h(entry.body) %></li>
</ul>
<% end %>
</div>
<% end %>
The rest of you application is correct - you should be creating entries in GuestbookEntriesController#create. In many real life applications then the functionality of the standard new and edit actions can actually be a totally different controller.
I pass the following to the results/index.html.erb view:
def create
#results = Quiz.where(user_id: current_user.id)
redirect_to results_path
end
(I've changed the above action BACK to create).
Then, in my results/index.html.erb view:
<div class="container">
<!-- Example row of columns -->
<div class="row">
<h1>Results page</h1>
<div class="col-md-12">
<p>Eventually a user's results will appear here...</p>
<p><%= #results %></p>
<p><%= #results.inspect %></p>
<p><%= current_user.quiz %></p>
<h4>Quiz Answer</h4>
<p><%= #results.answer1 %></p>
<p><%= #results.answer2 %></p>
<p><%= #results.answer3 %></p>
<p><%= #results.answer4 %></p>
<p><%= #results.answer5 %></p>
<p><%= #results.answer6 %></p>
<p><%= #results.answer7 %></p>
<p><%= #results.answer8 %></p>
</div>
</div>
<footer>
<p><a class="btn btn-default" href="showmetheskills.html" role="button">next</a></p>
</footer>
</div> <!-- /container -->
(I know it's not 'best practise' to have numbered attributes on a Model; ignore that part.)
I get the error:
undefined method 'answer1' for nil:NilClass
Why is #results nil, when I set it IN THE ACTION Rails is asking for?
The Quiz is created in the QuizzesController:
def new
#user = current_user
#quiz = Quiz.create(user_id: current_user.id)
end
Which triggers quizzes/new.html.erb which renders the quiz partial.
What's the problem?
#results is nil because you've done a redirect. Either render the view
def update
#results = Quiz.where(user_id: current_user.id)
render: 'results#index'
end
Or do the redirect and get the results there which would be my preferred option, but I'd question why you're sending them to the update action without updating anything.
def update
redirect_to results_path
end
def ResultsController
def index
#results = Quiz.where(user_id: current_user.id)
end
end
Either way things like #results.answer1 won't work because you have an active record relation. You either need to loop
<% #results.each do |result| %>
<%= result.answer1 %>
<% end %>
Or grab the first result
#results = Quiz.where(user_id: current_user.id).first
In your case #results is an array and you call some answer1, answer2, answer3... methods(that are not defined yet I guess) on array that also does not exists.
Next:
You initialize #results in update action, but expect it to work in resuzlts/index.html.erb.
To show #results in index html you need to load this variable in index action.
Update
If you want your index action to show something, put this line:
#results = Quiz.where(user_id: current_user.id)
into index action
def index
# here
end
and remove answer1, answer2, answer3.... from index.html.erb, they really makes no sense.
Also, index.html.erb should look like this:
<div class="col-md-12">
<p><%= current_user.quiz %></p>
<h4>Quiz Answer</h4>
<% #results.each do |result| %>
<p><%= result.answer %></p>
<% end %>
</div>
this would work only if you have answer defined for result. Hard to say what is what in your app.
I'm working on a reddit mock-up through a tutorial. When on my localhost I am on my new page(submit new link) where I can submit a title and url.
Whenever I submitted the information, I would previously end up on a blank create view.
The tutorial is asking for us to find a way to populate our database and end up on a show view with our submitted information & also have our newly submitted information be available on our index page.
This is my attempt at editing my controller for this, but I've failed miserably:
class LinksController < ApplicationController
def index
#link = Link.all
end
def show
#link = Link.find(params[:id])
end
def new
#link = Link.new
end
def create
#link = Link.new(link_params)
if #link.save
redirect_to #link
else
render action: 'new'
end
end
end
I am getting an error that reads:
undefined local variable or method `link_params'
Any advice on how to fix this?
This is my new view:
This is new view:
<%= form_for(#link) do |f| %>
<% if #link.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#link.errors.count, "error") %> prohibited this link from being saved:</h2>
<ul>
<% #link.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :url %><br />
<%= f.text_field :url %>
</div>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
My show view is currently empty save for some header text:
Also, if someone had some advice on how to go about further improving my controller & views as well to get the desired result that would be appreciated, I am new & trying to learn, thanks.
You need a link_params method in your controller
def link_params
params.require(:link).permit(:url, :title)
end
When you submit a form from browser, it hits the controller action. The form will submit some parameters which needs to be processed. Follow rails logs to see what is being returned, this is the best simple way to know and debug things.
Here in your create action, #link = Link.new(link_params) you are initializing Link object with link_params data, when the form is submitted check what parameter contains the hash. Parse over that hash to pass data in Link class object. You need to define link_params or use something like params[:link] after confirming from the server logs.