How to link between two controllers and two views in HTML/erb - ruby

I am trying to make simple webboard application. In my question/show view, I have get method to link for action to answer_controller. But it seems not link to the answer view. The error is Sinatra does not know this path. I want to create the path from question/show.erb view to answer/new.erb via submit answer button. What should I do?
question_controller (Controller folder)
get '/questions/:id/show' do
#question = Question.find_by(id: params[:id])
erb :'question/show'
end
question/show (View folder)
<form method="get" controller="answers" action="/answers/question/<%=#question.id%>/new">
<input type=submit value="Answer"></form>
answer_controller (Controller folder)
get 'answers/question/:q_id/new' do
erb :'answer/new'
end

You just need the leading slash in the url of your answer route:
get '/answers/question/:q_id/new' do
# ...

Related

Why isn't my view redirecting to a controller?

I have a html form inside one of my views that is supposed to get an image the user uploads and save it, the form is below:
<form class="col-lg-12" action="/banner/store/">
The form itself is quite large, but later on there's a to end it. It was supposed to redirect me to the banner/store where my Banner controller have a dd() function so I can confirm everything is okay. But instead, I get a blank white screen and the url goes banner/store/?.
The routes to the BannerController seems to be correct since banner/create works, the problem is when I go from create to store, but anyways, here's how I set the Routes:
Route::resource('banner', 'BannerController');
Any ideas why my banner/store won't work? And why is there a question mark on the end of the url? Sorry if this may be a dumb error, I'm still learning coding.
Your action is is wrong. When you use the resource static method then the store-url is the same as the GET url.
You can achive your goal with:
<form class="col-lg-12" action="{{ route('banner.store') }}" method="POST">
See more information in documentation.
When you updating your banner, you can't use the browser native form with PUT.
See in this document how laravel will handle that for you.

How to get a Sinatra erb to report back its referring page in params

For learning purposes, I am developing a Ruby/Sinatra "to do list" program. In views, I have the usual index page, which lists uncompleted tasks, and also a completed page. On both pages (and on many future user-created categories pages), there is a similar table, with similar methods for moving and deleting items. These call the same methods, or I'd like them to. But if they call the same methods, at the end of those methods the user is redirected to the index view. But if users are on the completed page, they want to stay on that page after deleting an item (or whatever).
To solve this problem, I think what I need to do is pass a variable from the erb file to the route block, reporting what view (erb file) the user was most recently on. I thought there would be some built-in Sinatra methods to report this, and I looked hard (e.g., I tried all plausible-sounding ways of accessing the request object), but I couldn't find any. Then I tried using <input type="hidden" name="pg_type" value="completed"> but for reasons totally mysterious to me, this doesn't work (maybe because Sinatra uses type="hidden" to route PUT and DELETE requests?). When I inspect the parameters with p params, I see that the pg_type param was created, but its content is an empty string ("").
An example form from (this is a template used by the other views) task_table.erb is this:
<form method="post" action="/delete/<%= task.id %>">
<label for="delete_button">
<input type="hidden" name="pg_type" value="<% #pg_type %>">
<button type="submit" name="delete">Delete</button></label>
</form>
Here is the route block for that method:
post('/delete/:id') do
store.delete(params[:id].to_i)
session[:message] << " " + "Deleted task!"
redirect "/" if params[:pg_type] == "index"
redirect params[:pg_type] # But params[:pg_type] always == "" :-(
end
So how can a pass info about the referring page from my erb pages to my route blocks? Or, if my strategy for solving the problem of returning the user to the correct page is wrong or could be improved (I wouldn't be surprised), please explain.
I'd love to have any more detailed feedback on this program. Other problems I'm trying to solve are how to add unit tests of put and delete methods and how to add user accounts (not started with that though).
In your code you forgot a '=' before #pg_type:
<input type="hidden" name="pg_type" value="<%= #pg_type %>">

Sinatra not loading layout file when URL has 2 sublevels

I have this code:
# Users page
get '/admin/users' do
#title = "Admin - Users"
erb :admin_users
end
When the view is rendered, I get the HTML in the ERB file but the layout file does not render. If I change the route to just '/users' everything renders fine. What gives? I originally put the users ERB page in a subdirectory 'views/admin' after finding sample code on how to do that and hit this issue. I thought that was the reason but rather it seems its the URL causing the problem. Anyone else hit this or know a work around?
All my other views work fine as well. This is the first view I've tried the URL pattern with. I also tried this but it didn't affect anything.
# Users page
get '/admin/users' do
#title = "Admin - Users"
erb :admin_users, :layout => :layout
end
Any help is appreciated. Thanks.
I've got the same problem, check paths to yours assets in the layout. You should add / before.
<link href='/stylesheets/application.css' rel='stylesheet' type='text/css'> is correct.
If you use a subdirectory for the admin_users view, you should really save it as views/admin/users.erb and then render it with
get '/admin/users' do
erb :'admin/users'
end
Maybe this will already address your issue. Let me know, if you need more help.
I guess you can get into trouble when you set the view directory for the admin views different from the location of the layout.erb file. Have you tried putting a dummy layout.erb in the views/admin/ folder?
You did not showed the exact error your are getting. I assume it is "No such file or directory" for the template "admin_users.erb".
An option is missing on your "erb" call and you should tell Sinatra where to find the template. This has just worked for me:
# Users page
get '/admin/users' do
#title = "Admin - Users"
erb :'admin/admin_users', :layout_options => { :views => 'views/admin' }
end
It renders the template on views/admin/admin_users.erb using the layout from views/admin/layout.erb

Html.RenderAction cannot find my controller

I have a razor masterpage (_Layout.cshtml) where I layout a 3 column website. In one of the side columns I want to display a "Login Control"
From my readings, I can use Html.RenderAction to call my LoginController and it will display the login view in the side column.
But, when I run it and point it to a Controller/View to fill the RenderBody(), the call to Html.RenderAction("Index", "LoginController") fails with this error.
"The controller for path '/[insert path to a Controller/View to fill the
RenderBody()]' was not found or does not implement IController. "
So, what am I doing wrong?
My code really is as simple as:
<div id="Navigation">#{ Html.RenderPartial("Test"); }</div>
<div id="Main">#RenderBody()</div>
<div id="Misc">#{ Html.RenderAction("Index", "LoginController");}</div>
And in my controllers folder, I have the controller for the RenderBody and the LoginController.
When specifying controller names by convention in MVC, you don't include the "Controller" part.
Html.RenderAction("Index", "LoginController")
wouldn't work unless you had a controller named "LoginControllerController"
try
<div id="Misc">#{ Html.RenderAction("Index", "Login");}</div>

Cannot submit form to controller zend framework

I create a from statically in HTML file in view project which is inside the module visit.
I want the controller to handle the request occur when submitting the form
<form action="addVisit" method="post">
<input type="text" name="number"/>
<input type="submit" name="save"/>
The structure of the project is
module visits
controller Visits and it has action addVisit
when submitting the form an error occur, the url becomes like this when submitting the form
http://localhost/zendApps/InspectionSys/public/visits/visits/VisitsController/addVisit
in the controller there is a function action
public function addVisitAction()
{
echo 'here';
}
what should I do ?
First you might reconsider camel caseing your actions addVisitAction() because if you do then you have to deal with view file names like add-visit.phtml I think it will affect urls as well, I find it simpler just to leave action names as lowercase addvisitAction(), this might be part of what's amiss.
Next identify your form actions as /module/controller/action (if you are not using modules you can omit that param), so your action should at least be /visits/visits/addvisit or /visits/visits/add-visit (not quite sure which will work properly) .
also when using html forms that were not generated by Zend_Form you can access the values using $this->getParams() or $this->getParam('paramName') instead of $this->getValues().

Resources