Lotix
Lotix

Reputation: 309

Better way of getting objects id from another controller

I have ideas controller and static_pages controller. The latter has home action which displays all ideas and which i also use as root path.

I want the user be able to Edit the displayed ideas. So far i have this:

<% if @ideas.empty? %>
  <p>Share your ideas! See what people think about it.</p>
<% else %>
  <% @ideas.each do |idea| %>
    <div class="panel panel-default">
      <div class="panel-heading"><%= idea.name %></div>
      <div class="panel-body"><%= idea.description %> <br>
      <%= link_to 'Edit', edit_idea_path(idea.id) %>
      </div>
    </div>
  <% end %>
<% end %>

I had an issue with an empty idea id which i solved by adding idea.id inside edit_idea_path

Now my question is, is that the proper, Rails way of doing it? In what other way can i fetch the idea object from this index page and use it in my ideas controller instead of static_pages controller?

I tried playing around with routing, but I have very vague understanding of it despite reading the guides and others code. I'd appreciate any insight about this matter.

Upvotes: 0

Views: 53

Answers (1)

matias salgado
matias salgado

Reputation: 156

First you need to understand that the requirement of your project defines what you should do in the code, whithout of concerning about the proper way to do something. You just need to follow the rails conventions.

http://guides.rubyonrails.org/active_record_basics.html#convention-over-configuration-in-active-record

Now, back to your question. You just need to create an action (that will handle a view) in your ideas_controller that will manage the edition of the data sended by de static_pages_controller, i will call it (just for example) edit_static_ideas and receive the data with params:

In your ideas_controller : app/controllers/ideas_controller.rb

def edit_static_ideas
   @idea = Idea.find(params[:id])

end

Then you need to create the view in your views->ideas folder. An name it, just to continue my example i'll name it edit_static_idea.html.erb. And set the load of the data you get in @idea as a form or a form_for. Then you can submit that edited data and upload it into other action.

Then you have to configure your routes file and add

config/routes.rb

get 'edit_static_idea/:id', to: 'ideas#edit_static_idea', as: 'edit_ideas'

After that, if you run "rake routes" in your console (inside your rails project), you should see your new route (yay!)

Now you have to take the path in your route and use it in you static_pages_controller's view to redirect it to the edit_idea's view handle it by ideas_controller. And be sure that you also send the id of the selected item.

app/views/static_pages/home.html.erb:

<%= link_to 'Edit Idea', insert_your_edit_idea_obtainedinrakeroutes_path(id: idea.id) %>

At last, you only need to configure the form in your edit_static_idea.html.erb and assign it an upload/save route and redirect it to the view that you want.

for example:

In your routes file: config/routes.rb

 patch 'save_edited_idea', to: 'ideas#save_edited_idea', as: 'save_edited_idea'

In your ideas_controller: app/controllers/ideas_controller.rb

    def save_edited_idea
    @idea = Idea.find(params[:id])
    respond_to do |format|
      if @idea.update(idea_params)
        format.html { redirect_to the_view_that_you_want_path(id: @idea.id), notice: 'Data saved without problems.' }
      else
        flash.now[:alert] = "error"
        format.html { render :offer  }
      end
    end
  end

I didn't wanted to be so detailed, because i wanted to help you to understand what you have to do. I hope it helps :P

Upvotes: 2

Related Questions